java控制項,Java控制項是什麼

本文目錄一覽:

java的容器控制項有哪些

1.頂層容器

什麼是頂層容器?當我們使用Java進行圖形編程的時候,圖在哪裡繪製呢?我們需要一個能夠提供圖形繪製的容器,這個容器就被稱為頂層容器,你

也可以把它想像成一個窗口。頂層容器是進行圖形編程的基礎,一切圖形化的東西,都必然包括在頂層容器中。在Swing中,我們有三種可以使用的頂層容器,

它們分別是:

JFrame:用來設計類似於Windows系統中的窗口形式的應用程序。

JDialog:和JFrame類似,只不過JDialog是用來設計對話框。

JApplet:用來設計可以在嵌入在網頁中的Java小程序。

如果需要使用Swing製作一個窗口類程序,我們的代碼看起來應該是這樣:

import javax.swing.*;

public class KyodaiUI

extends JFrame {

……

}

2.控制項

控制項是構成應用程序界面的基本元素,按鈕、文本框、進度條等,這些都是控制項。控制項(這裡我們只討論可視化控制項)又可以分為容器控制項和非容器控制項。從字面

意義上來理解,容器控制項就是能包含其他控制項的特殊控制項,例如,Java中的JPanel控制項就屬於容器型控制項,我們可以在JPanel中放置按鈕、文本框

等非容器控制項,你甚至可以在JPanel中再放置若干個JPanel控制項(值得注意的是,頂層容器也是容器型控制項,每一個窗口應用程序中有且只能有一個頂

層容器控制項,換句話說,頂層容器不能包括在其他的控制項中)。

Java中的容器控制項有很多,除剛才提到的JPanel外,還有

JTabbedPane、JScrollPane等,非容器控制項有JButton、JLabel、JTextField等。如果你需要向某個容器型的控制項

中添加控制項,你可以使用 add(Component comp) 方法來實現,如:

JPanel panel = new JPanel();

JButton button = new JButton();

panel.add(button);

3.邊框

雖然我們使用了不同前景色來區別不同的區域,然而卻沒有層次感,加上邊框一定會漂亮許多。

在Java中,所有以J打頭的Swing控制項都可以使用setBorder方法來為自己設置邊框。邊框有很多種,線型、凸起、凹下、空的,你甚至可以自

什麼是java控制項?怎樣使用java控制項?

import Java.util.Date;

import java.util.Calendar;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.text.ParseException ;

import java.awt.Color ;

import java.awt.Font;

import java.awt.Point ;

import java.awt.Dimension ;

import java.awt.BorderLayout ;

import java.awt.FlowLayout ;

import java.awt.GridLayout ;

import java.awt.Component ;

import java.awt.Cursor ;

import java.awt.Frame ;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

//import javax.swing.JFrame;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JPanel;

import javax.swing.JLabel;

import javax.swing.jspinner ;

import javax.swing.JSpinner.NumberEditor ;

import javax.swing.SpinnerNumberModel;

import javax.swing.SwingUtilities;

import javax.swing.SwingConstants ;

import javax.swing.event.ChangeListener ;

import javax.swing.event.ChangeEvent ;

import javax.swing.border.LineBorder ;

public class DateChooserJButton extends JButton {

private DateChooser dateChooser =null;

private String preLabel =”” ;

public DateChooserJButton() {

this(getNowDate()) ;

}

public DateChooserJButton(SimpleDateFormat df , String dateString) {

this() ;

setText(df,dateString) ;

}

public DateChooserJButton(Date date) {

this(“”,date);

}

public DateChooserJButton(String preLabel , Date date) {

if (preLabel!=null) this.preLabel = preLabel ;

setDate(date) ;

setBorder(null) ;

setCursor(new Cursor(Cursor.HAND_CURSOR)) ;

super.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (dateChooser==null) dateChooser = new DateChooser();

Point p = getLocationOnScreen() ;

p.y = p.y+30 ;

dateChooser.showDateChooser(p) ;

}

}) ;

}

private static Date getNowDate() {

return Calendar.getInstance().getTime() ;

}

private static SimpleDateFormat getDefaultDateFormat() {

return new SimpleDateFormat(“yyyy年MM月dd日HH時”) ;

}

//覆蓋父類的方法

public void setText(String s) {

Date date ;

try {

date = getDefaultDateFormat().parse(s) ;

}catch (ParseException e) {

date = getNowDate() ;

}

setDate(date) ;

}

public void setText(SimpleDateFormat df , String s) {

Date date ;

try {

date = df.parse(s) ;

}catch (ParseException e) {

date = getNowDate() ;

}

setDate(date) ;

}

public void setDate(Date date) {

super.setText(preLabel+getDefaultDateFormat().format(date));

}

public Date getDate() {

String dateString = getText().substring(preLabel.length());

try {

return getDefaultDateFormat().parse(dateString);

} catch (ParseException e) {

return getNowDate() ;

}

}

//覆蓋父類的方法使之無效

public void addActionListener(ActionListener listener ) {

}

private class DateChooser extends JPanel implements ActionListener ,ChangeListener {

int startYear = 1980; //默認【最小】顯示年份

int lastYear = 2050; //默認【最大】顯示年份

int width = 200; //界面寬度

int height = 200; //界面高度

Color backGroundColor = Color.gray; //底色

//月曆表格配色—————-//

Color palletTableColor = Color.white; //日曆表底色

Color todayBackColor = Color.orange; //今天背景色

Color weekFontColor = Color.blue; //星期文字色

Color dateFontColor = Color.black; //日期文字色

Color weekendFontColor = Color.red; //周末文字色

//控制條配色——————//

Color controlLineColor = Color.pink; //控制條底色

Color controlTextColor = Color.white; //控制條標籤文字色

Color rbFontColor = Color.white; //RoundBox文字色

Color rbBorderColor = Color.red; //RoundBox邊框色

Color rbButtonColor = Color.pink; //RoundBox按鈕色

Color rbBTFontColor = Color.red; //RoundBox按鈕文字色

JDialog dialog ;

JSpinner yearSpin ;

JSpinner monthSpin ;

JSpinner hourSpin ;

JButton[][] daysButton = new JButton[6][7] ;

DateChooser() {

setLayout(new BorderLayout());

setBorder(new LineBorder(backGroundColor, 2));

setBackground(backGroundColor);

JPanel topYearAndMonth = createYearAndMonthPanal();

add(topYearAndMonth,BorderLayout.NORTH);

JPanel centerWeekAndDay = createWeekAndDayPanal();

add(centerWeekAndDay,BorderLayout.CENTER);

}

private JPanel createYearAndMonthPanal(){

Calendar c = getCalendar() ;

int currentYear =c.get(Calendar.YEAR);

int currentMonth =c.get(Calendar.MONTH)+1;

int currentHour =c.get(Calendar.HOUR_OF_DAY);

JPanel result = new JPanel();

result.setLayout(new FlowLayout());

result.setBackground(controlLineColor);

yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,startYear,lastYear,1));

yearSpin.setPreferredSize(new Dimension(48,20)) ;

yearSpin.setName(“Year”) ;

yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, “####”)) ;

yearSpin.addChangeListener(this) ;

result.add(yearSpin) ;

JLabel yearLabel = new JLabel(“年”);

yearLabel.setForeground(controlTextColor);

result.add(yearLabel);

monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth,1,12,1));

monthSpin.setPreferredSize(new Dimension(35,20)) ;

monthSpin.setName(“Month”) ;

monthSpin.addChangeListener(this) ;

result.add(monthSpin) ;

JLabel monthLabel = new JLabel(“月”);

monthLabel.setForeground(controlTextColor);

result.add(monthLabel);

hourSpin = new JSpinner(new SpinnerNumberModel(currentHour,0,23,1));

hourSpin.setPreferredSize(new Dimension(35,20)) ;

hourSpin.setName(“Hour”) ;

hourSpin.addChangeListener(this) ;

result.add(hourSpin) ;

JLabel hourLabel = new JLabel(“時”);

hourLabel.setForeground(controlTextColor);

result.add(hourLabel);

return result ;

}

private JPanel createWeekAndDayPanal() {

String colname[] = {“日”,”一”,”二”,”三”,”四”,”五”,”六”};

JPanel result = new JPanel();

//設置固定字體,以免調用環境改變影響界面美觀

result.setFont(new Font(“宋體”, Font.PLAIN, 12));

result.setLayout(new GridLayout(7,7));

result.setBackground(Color.white);

JLabel cell ;

for(int i=0;i7;i++) {

cell = new JLabel(colname[i]);

cell.setHorizontalAlignment(JLabel.RIGHT);

if (i==0 i==6) cell.setForeground(weekendFontColor) ;

else cell.setForeground(weekFontColor) ;

result.add(cell) ;

}

int actionCommandId = 0 ;

for(int i = 0; i 6; i++)

for(int j = 0; j 7; j++) {

JButton numberButton = new JButton();

numberButton.setBorder(null) ;

numberButton.setHorizontalAlignment(SwingConstants.RIGHT);

numberButton.setActionCommand(String.valueOf(actionCommandId)) ;

numberButton.addActionListener(this) ;

numberButton.setBackground(palletTableColor);

numberButton.setForeground(dateFontColor) ;

if (j==0 j==6) numberButton.setForeground(weekendFontColor) ;

else numberButton.setForeground(dateFontColor) ;

daysButton[i][j] = numberButton;

result.add(numberButton) ;

actionCommandId ++ ;

}

return result;

}

private JDialog createDialog(Frame owner ) {

JDialog result = new JDialog(owner,”日期時間選擇”,true) ;

result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

result.getContentPane().add(this,BorderLayout.CENTER) ;

result.pack() ;

result.setSize(width, height);

return result ;

}

void showDateChooser(Point position ) {

Frame owner =(Frame)SwingUtilities.getWindowAncestor(DateChooserJButton.this) ;

if (dialog==null dialog.getOwner()!= owner) dialog = createDialog(owner ) ;

dialog.setLocation(getAppropriateLocation(owner ,position ) ) ;

flushWeekAndDay() ;

dialog.show();

}

Point getAppropriateLocation(Frame owner ,Point position ) {

Point result = new Point(position) ;

Point p = owner.getLocation() ;

int offsetX = (position.x+width) – (p.x + owner.getWidth() ) ;

int offsetY = (position.y+height) – (p.y + owner.getHeight() ) ;

if (offsetX 0 ) {

result.x -= offsetX ;

}

if (offsetY 0 ) {

result.y -= offsetY ;

}

return result ;

}

private Calendar getCalendar() {

Calendar result = Calendar.getInstance();

result.setTime(getDate()) ;

return result ;

}

private int getSelectedYear() {

return ((Integer)yearSpin.getValue()).intValue() ;

}

private int getSelectedMonth() {

return ((Integer)monthSpin.getValue()).intValue() ;

}

private int getSelectedHour() {

return ((Integer)hourSpin.getValue()).intValue() ;

}

private void dayColorUpdate(boolean isOldDay) {

Calendar c = getCalendar() ;

int day = c.get(Calendar.DAY_OF_MONTH);

c.set(Calendar.DAY_OF_MONTH,1);

int actionCommandId =day-2+c.get(Calendar.DAY_OF_WEEK) ;

int i = actionCommandId/7;

int j = actionCommandId%7;

if (isOldDay) daysButton[i][j].setForeground(dateFontColor) ;

else daysButton[i][j].setForeground(todayBackColor) ;

}

private void flushWeekAndDay() {

Calendar c = getCalendar() ;

c.set(Calendar.DAY_OF_MONTH,1);

int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);

int dayNo = 2 – c.get(Calendar.DAY_OF_WEEK) ;

for(int i = 0; i 6; i++) {

for(int j = 0; j 7; j++) {

String s=”” ;

if (dayNo=1 dayNo=maxDayNo ) s = String.valueOf(dayNo) ;

daysButton[i][j].setText(s) ;

dayNo ++ ;

}

}

dayColorUpdate(false) ;

}

public void stateChanged(ChangeEvent e) {

JSpinner source =(JSpinner)e.getSource() ;

Calendar c = getCalendar() ;

if (source.getName().equals(“Hour”)) {

c.set(Calendar.HOUR_OF_DAY, getSelectedHour());

setDate(c.getTime());

return ;

}

dayColorUpdate(true) ;

if (source.getName().equals(“Year”))

c.set(Calendar.YEAR, getSelectedYear());

else

// (source.getName().equals(“Month”))

c.set(Calendar.MONTH, getSelectedMonth()-1);

setDate(c.getTime());

flushWeekAndDay() ;

}

public void actionPerformed(ActionEvent e) {

JButton source =(JButton)e.getSource() ;

if (source.getText().length()==0) return ;

dayColorUpdate(true) ;

source.setForeground(todayBackColor) ;

int newDay = Integer.parseInt(source.getText());

Calendar c = getCalendar() ;

c.set(Calendar.DAY_OF_MONTH,newDay);

setDate(c.getTime());

}

}

}

如何設置java控制項的位置和大小

 Java中通過布局管理器(LayoutManager)來布局控制項。

常見的布局管理器如下:

BorderLayout:Frame和Dialog的默認布局管理器。其把面板分為上北、下南、左西、右東和中間,5個部分,在未指定控制項大小的情況下,被加入的控制項將會隨著內容大小自動伸縮,隨著窗口的放大縮小也相應的產生伸縮。

FlowLayout :按順序布局控制項。所有控制項按照流水一樣排列,若當前行放不下了,則自動排到下一行。按子控制項的大小(getPreferedSize())在當前面板布局。

GridLayout :網格布局。該布局將所有控制項按從左到右,從上到下的形式把控制項以網格的形式排列出來。子控制項會填滿整個面板。

GridBagLayout :網格包布局。能布局複雜界面,但其使用也很複雜,一般不用。

null布局:即將布局管理器設置為null。此時,通過調用子控制項的setBounds方法進行布局。

其他布局管理器。以上是Java內置的常用布局管理器,還有一些非官方的好用布局管理器:XYLayout(按大小和位置布局)、VerticalFlowLayout (縱向流水布局)、TableLayout(表格布局)。

一般的Java程序布局方式:用FlowLayout布局一些按鈕,用BorderLayout嵌套多個面板。

怎樣在Java中美化按鈕控制項顯得美觀?

Java ( Eclipse )環境下, Button 樣式問題解釋如下:\x0d\x0a在jframe中的button樣式是不可以隨意改變的設置好了就是固定的樣式。\x0d\x0a樣式設置如下:\x0d\x0a1、對JButton大小的設置 \x0d\x0a因為JButen是屬於小器件類型的,所以一般的setSize不能對其驚醒大小的設置,所以一般我們用\x0d\x0abutton.setPreferredSize(new Dimension(30,30)); //(30,30) 是你要設置按鈕的大小 \x0d\x0a2、對JButton透明的設置 \x0d\x0a按鈕設置為透明,這樣就不會擋著後面的背景 \x0d\x0abutton.setContentAreaFilled(false); \x0d\x0a3、對JButton去掉按鈕的邊框的設置 \x0d\x0a如果有時候你的按鈕不需要邊框因為邊框影響美觀或者是因為你需要的是點擊之前按鈕呈現 普通圖標形式,點擊之後才有各種效果的話就可以用這種方法去掉邊框 \x0d\x0abutton.setBorderPainted(false); \x0d\x0a4、對JButton添加圖標呢的設置 // 實例化一個圖標對象 \x0d\x0aImageIcon image = new ImageIcon(icons[i]); // 實例化按鈕對象,並且設置按鈕上顯示圖片 \x0d\x0aJButton button = new JButton(image); ——或者 \x0d\x0abutton.setIcon(new ImageIcon(getClass().getResource(“qq.png”))); //qq.png是你要添加的圖片 \x0d\x0a5、讓按鈕隨按鈕上的圖案變化 butten.setMargin(new Insets(0,0,0,0)); \x0d\x0a6、設置凸起來的按鈕,很多其他的swing也可用此方法 \x0d\x0abutten.setBorder(BorderFactory.createRaisedBevelBorder()); \x0d\x0a7、設置凹起來的按鈕,很多其他的swing也可用此方法 \x0d\x0a button.setBorder(BorderFactory.createLoweredBevelBorder()); \x0d\x0a8、設置按鈕的前景色和背景色 \x0d\x0a button .setFont(new java.awt.Font(“華文行楷”, 1, 15)); \x0d\x0a button.setBackground(Color.green); \x0d\x0a9、改變按鈕的樣式 \x0d\x0a UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”); \x0d\x0aJFrame可以理解成一個容器,既然是容器就可以裝東西。JButton就屬於容器裡面的東西,比如說是魚。如果魚離開了水,是無法生存的,這就可以類比JButtion必須放到JFrame上面才能進行添加。\x0d\x0aJAVA Swing中JFrame代表容易,JPane代表面板或者畫布,可以在上面添加按鈕、對話框、輸入框等。

原創文章,作者:WRGO,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/134850.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
WRGO的頭像WRGO
上一篇 2024-10-04 00:08
下一篇 2024-10-04 00:08

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29
  • Java 8 Group By 會影響排序嗎?

    是的,Java 8中的Group By會對排序產生影響。本文將從多個方面探討Group By對排序的影響。 一、Group By的概述 Group By是SQL中的一種常見操作,它…

    編程 2025-04-29

發表回復

登錄後才能評論