本文目錄一覽:
- 1、如何設置java控件的位置和大小
- 2、什麼是java控件?怎樣使用java控件?
- 3、JAVA中組建與控件的區別是什麼?
- 4、java的容器控件有哪些
- 5、怎樣自己做一個java控件
- 6、怎樣在Java中美化按鈕控件顯得美觀?
如何設置java控件的位置和大小
Java中通過布局管理器(LayoutManager)來布局控件。
常見的布局管理器如下:
BorderLayout:Frame和Dialog的默認布局管理器。其把面板分為上北、下南、左西、右東和中間,5個部分,在未指定控件大小的情況下,被加入的控件將會隨着內容大小自動伸縮,隨着窗口的放大縮小也相應的產生伸縮。
FlowLayout :按順序布局控件。所有控件按照流水一樣排列,若當前行放不下了,則自動排到下一行。按子控件的大小(getPreferedSize())在當前面板布局。
GridLayout :網格布局。該布局將所有控件按從左到右,從上到下的形式把控件以網格的形式排列出來。子控件會填滿整個面板。
GridBagLayout :網格包布局。能布局複雜界面,但其使用也很複雜,一般不用。
null布局:即將布局管理器設置為null。此時,通過調用子控件的setBounds方法進行布局。
其他布局管理器。以上是Java內置的常用布局管理器,還有一些非官方的好用布局管理器:XYLayout(按大小和位置布局)、VerticalFlowLayout (縱向流水布局)、TableLayout(表格布局)。
一般的Java程序布局方式:用FlowLayout布局一些按鈕,用BorderLayout嵌套多個面板。
什麼是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中組建與控件的區別是什麼?
一般把Control翻譯成控件,把Component翻譯成組件。
控件就是具有用戶界面的組件。要說的具體一點,就得回顧早期 Windows 的歷史根源,當時控件指任何子窗口——按鈕、列表框、編輯框或者某個對話框中的靜態文本。從概念上講,這些窗口——控件——類似用來操作收音機或小電器的旋鈕和按鈕。隨着控件數量的增加(組合框、日期時間控件等等),控件逐漸成為子窗口的代名詞,無論是用在對話框中還是用在其它種類的主窗口中。沒過多久 BASIC 程序員開始編寫他們自己專用的控件,自然而然地人們便想到共享這些控件。共享代碼的方法之一是通過磁盤拷貝,但那樣顯然效率低下。必須要有一種機制使開發者建立的控件能夠在其它程序員的應用中輕而易舉地插入,這便是VBA控件,OLE控件,OCX和最後ActiveX 控件的動機。
這就是控件和組件之間產生混淆之所在。因為為了解決控件的可復用問題,所有這些技術必須首先解決更為一般的組件重用問題。(COM,如果你還記得它的話,意思是組件對象模型)。在軟件行話中,組件這個術語指任何可復用的對象或任何可與其它對象交互的代碼體。子程序的發明,曾經一度成為程序員趨之若鶩的軟件工程聖杯:一種統一的編程理論,它使程序員從基本構建塊——也就是用所選語言編寫的各種組件建立大型系統。從子程序演變到OOP,到DLLs,再到COM,再到.NET框架的每一種新的編程範例都代表了一種不同的提供可重用性的方案。VBX使用DLLs的固化名稱。COM使用接口和IUnknown。.NET框架使用微軟的中間語言(MSIL)層和公共語言運行時(CLR)來提供統一的粘合。
因此,控件是組件的一個主要樣本(並且歷史上曾驅動着組件的開發),控件又不僅僅是唯一的一種組件。組件不需要顯示任何信息或用戶界面。組件可能實現科學計算,收集性能數據,計算1971年1月1日到現在的毫秒數,仰或是讀取布什總統競選活動保險箱里的美金數。
控件和組件
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控件
就寫一個沒有主函數的類不就可以了,一個實現顯示文字的方法,一個實現顯示圖像的方法,然後在另一個類中調用,可以創建一個button,然後實現它的監聽,然後執行那個類中的某個方法不就可以了,awt和swing沒怎麼學,寫不出來
!!
怎樣在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代表面板或者畫布,可以在上面添加按鈕、對話框、輸入框等。
原創文章,作者:BFLH,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/146267.html