一、什么是GUI
????????GUI 全稱 Graphical User Interface 圖形用戶界面,指采用圖形方式顯示的計(jì)算機(jī)操作用戶界面。?軟件以圖形界面形式運(yùn)行時(shí),用戶可借助于菜單、按鈕、標(biāo)簽等組件和通過鼠標(biāo)、鍵盤等的操作共同完成對(duì)軟件的應(yīng)用。
????????當(dāng)程序運(yùn)行后,出現(xiàn)的圖形窗口,稱為程序的界面。界面上可能有輸入框、輸入?yún)^(qū)、 菜單項(xiàng)、按鈕等各種圖形組件。
????????當(dāng)用戶通過鍵盤往輸入?yún)^(qū)域輸入內(nèi)容后回車 或 用鼠標(biāo)點(diǎn)擊了一些可以被點(diǎn)擊的組件以后,程序可以產(chǎn)生我們預(yù)期的合理響應(yīng)或結(jié)果,稱為事件處理。
二、Java如何設(shè)計(jì)編寫GUI
? ? ? ? 1.導(dǎo)入相應(yīng)包
? ? ? ? (1)java.awt
頂層容器 | Frame、Dialog、Applet 等 |
中間層容器 | Panel 等 |
普通組件 | Button、TextField、TextArea、Label、List 等 |
????????(2)javax.swing
頂層容器 | JFrame、JApplet、JDialog 等 |
中間層容器 | JPanel、JScrollPane、JSplitPane、JToolBar 等 |
普通組件 | JButton、JList、JTextField 等 |
? ? ? ? 2.界面設(shè)計(jì)
????????一個(gè)GUI界面上可能有輸入框、輸入?yún)^(qū)、菜單項(xiàng)或者按鈕等各種圖形組件。界面設(shè)計(jì)即對(duì)展示給用戶的這些部件的格式,位置,大小進(jìn)行設(shè)計(jì)。
????????界面是需要有載體的。而“容器”是所有組件或容器的載體,是圖形用戶界面設(shè)計(jì)的基礎(chǔ)。圖形用戶界面上所有的元素都要裝載在容器中。
? ? ? ? (1)容器 (Container)
????????容器是 Java 圖形界面開發(fā)包中的系統(tǒng)類,它們只是一類比較特殊的圖形組件。它們?cè)跀?shù)量上基本上就只有幾個(gè)。設(shè)計(jì)界面時(shí),先要定義“容器”對(duì)象作為載體,然后定義按鈕、輸入框之類的普通圖形界面組件對(duì)象,把這些組件對(duì)象添加(調(diào)用容器對(duì)象的 add 方法)到容器上,才能構(gòu)成界面。
????????(2)布局
????????把這些組件放到容器上去之后,按什么樣的格局、什么樣的順序擺放,屬于容器的“布局”問題。?“布局”是對(duì)容器而言的,是對(duì)放置到容器內(nèi)的組件的一種位置約束。給任何一種容器對(duì)象設(shè)置布局的方法都是調(diào)用該容器對(duì)象的 setLayout 方法,該方法需要帶相應(yīng)的布局類對(duì)象作為參數(shù)。
Container.setLayout(布局類對(duì)象)
? ? ? ? 常用布局類對(duì)象:
布局 | 說明 |
FlowLayout (?順序布局\流式布局 ) |
容器上的元素會(huì)按容器顯示時(shí)的大小,按照從左到右、從上到下的一行一行擺放下去。
|
BorderLayout (?邊界布局 ) |
把容器分為東西南北中 5 個(gè)方位,可以指定某個(gè)組件在哪個(gè)方位。
[注意]并非一定要有 5 個(gè)組件擺上去,不足 5 個(gè)也可指定位置擺放
|
GridLayout(?網(wǎng)格布局 ) | 可以指定容器上的元素按幾行幾列的位置順序擺放。 |
CardLayout(?卡片布局 ) |
使用該布局的容器可放置多個(gè)組件,但同一時(shí)刻只能顯示其中一個(gè),就像一疊疊整齊的紙牌中每次只能最上面一張一樣??梢灾付膫€(gè)組件被顯示。
|
null(?空布局 ) |
空布局并非沒有布局,而是容器被設(shè)置為這種布局以后,放到它上面的每個(gè)組件就可以通過 setBounds(int a,int b,int width,int height)來設(shè)置具體的位置。
|
????????(3)頂層容器
????????頂層容器顧名思義,就是最大的用來容納其他組件的組件。只有頂層容器可以獨(dú)立的顯示出來,其他容器需要放在頂層容器中才能顯示出來。
- JFrame (框架) -?構(gòu)造一個(gè)初始不可見的新窗口
? ? ? ?? 部分方法:
setTitle(String title) | 設(shè)置窗口標(biāo)題 |
setLayout(new 布局管理器類型) | 創(chuàng)建布局管理器 |
add(組件名稱) | 添加組件 |
?setBounds(int x , int y , int width , int height) | 設(shè)置窗口顯示:x , y 顯示的位置,width , height 窗體的大小 |
setVisable(Boolean b) | 設(shè)置窗口是否顯示 |
?setDefaultCloseOperation(int operation) | 設(shè)置窗口退出方案 JFrame.DO_NOTHING_ON_CLOSE:不執(zhí)行任何操作 JFrame.HIDE_ON_CLOSE:隱藏該窗體。此時(shí)沒有關(guān)閉程序,只是將程序界面隱藏 JFrame.DISPOSE_ON_CLOSE:隱藏并釋放該窗體。繼續(xù)運(yùn)行應(yīng)用程序,釋放窗體中占用的資源 JFrame.EXIT_ON_CLOSE: 使用 System exit 方法退出應(yīng)用程序 |
????????例:
import javax.swing.*;
/**
* @name 鷾
*/
public class FrameCase {
public static void main(String[] args){
JFrame frame = new JFrame();
// 窗口是否可見
frame.setVisible(true);
// 設(shè)置窗體大小
frame.setSize(400,450);
// 設(shè)置關(guān)閉操作
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 設(shè)置窗體在屏幕上的位置
frame.setLocationRelativeTo(null);
// 設(shè)置窗口標(biāo)題
frame.setTitle("框架標(biāo)題");
}
}
? ? ? ? 效果:
- ?JDialog (對(duì)話框) -?對(duì)話框也是一個(gè)窗體,但沒有最大化、最小化按鈕
JDialog() | 創(chuàng)建一個(gè)沒有標(biāo)題并且沒有指定 Frame 所有者的無模式對(duì)話框 |
JDialog(Dialog owner) | 創(chuàng)建一個(gè)沒有標(biāo)題但將指定的 Dialog 作為其所有者的無模式對(duì)話框 |
JDialog(Dialog owner, boolean modal) | 創(chuàng)建一個(gè)具有指定所有者 Dialog 和模式的對(duì)話框 |
JDialog(Dialog owner, String title) | 創(chuàng)建一個(gè)具有指定標(biāo)題和指定所有者對(duì)話框的無模式對(duì)話框 |
JDialog(Dialog owner, String title, boolean modal) | 創(chuàng)建一個(gè)具有指定標(biāo)題、模式和指定所有者 Dialog 的對(duì)話框 |
JDialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) | 創(chuàng)建一個(gè)具有指定標(biāo)題、所有者Dialog、模式和 GraphicsConfiguration 的對(duì)話框。 |
JDialog(Frame owner) | 創(chuàng)建一個(gè)沒有標(biāo)題但將指定的 Frame 作為其所有者的無模式對(duì)話框 |
JDialog(Frame owner, boolean modal) | 創(chuàng)建一個(gè)具有指定所有者 Frame、模式和空標(biāo)題的對(duì)話框 |
JDialog(Frame owner, String title) | 創(chuàng)建一個(gè)具有指定標(biāo)題和指定所有者窗體的無模式對(duì)話框 |
JDialog(Frame owner, String title, boolean modal) | 創(chuàng)建一個(gè)具有指定標(biāo)題、所有者 Frame 和模式的對(duì)話框 |
JDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc) | 創(chuàng)建一個(gè)具有指定標(biāo)題、所有者 Frame、模式和 GraphicsConfiguration 的對(duì)話框 |
JDialog(Window owner) | 創(chuàng)建一個(gè)具有指定所有者 Window 和空標(biāo)題的無模式對(duì)話框 |
JDialog(Window owner, Dialog.ModalityType modalityType) | 創(chuàng)建一個(gè)具有指定所有者 Window、模式和空標(biāo)題的對(duì)話框 |
JDialog(Window owner, String title) | 創(chuàng)建一個(gè)具有指定標(biāo)題和所有者 Window 的無模式對(duì)話框 |
JDialog(Window owner, String title, Dialog.ModalityType modalityType) | 創(chuàng)建一個(gè)具有指定標(biāo)題、所有者 Window 和模式的對(duì)話框 |
JDialog(Window owner, String title, Dialog.ModalityType modalityType, GraphicsConfiguration gc) | 創(chuàng)建一個(gè)具有指定標(biāo)題、所有者 Window、模式和 GraphicsConfiguration 的對(duì)話框 |
setTitle(String title) | 設(shè)置對(duì)話框標(biāo)題 |
?setLocation(int x , int y , int width , int height) | 設(shè)置窗口顯示:x , y 顯示的位置,width , height 窗體的大小 |
setVisable(Boolean b) | 設(shè)置窗口是否顯示 |
?setDefaultCloseOperation(int operation) | 設(shè)置窗口退出方案 JFrame.DO_NOTHING_ON_CLOSE:不執(zhí)行任何操作 JFrame.HIDE_ON_CLOSE:隱藏該窗體。此時(shí)沒有關(guān)閉程序,只是將程序界面隱藏 JFrame.DISPOSE_ON_CLOSE:隱藏并釋放該窗體。繼續(xù)運(yùn)行應(yīng)用程序,釋放窗體中占用的資源 JFrame.EXIT_ON_CLOSE: 使用 System exit 方法退出應(yīng)用程序 |
import javax.swing.JDialog;
import java.awt.*;
/**
* @name 鷾
*/
public class DialogCase {
public static void main(String[] args){
JDialog dialog = new JDialog((Frame) null, "對(duì)話框名稱");
// 設(shè)置窗體大小
dialog.setSize(300, 150);
// 設(shè)置默認(rèn)關(guān)閉操作為:關(guān)閉時(shí)退出
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// 設(shè)置窗體在屏幕上的位置
dialog.setLocation(100, 100);
// 設(shè)置窗體是否顯示
dialog.setVisible(true);
}
}
????????(4)中間容器
????????中間層容器是一種可以放到頂層容器上的組件,它可以把頂層容器進(jìn)行進(jìn)一步的區(qū)域劃分以設(shè)計(jì)所需格局的界面。中間層容器也必須添加到頂層容器中才會(huì)啟作用,否則顯示不出來。
JPanel?(面板) | 作為最普通的面板,向面板中添加組件。 |
JTabbedPane(拆分面板) | 把面板分為兩部分,可以按左右分割,也可以按上下分割。 |
JSplitPane?(可切換選項(xiàng)卡面板) | 向面板中添加組件時(shí),會(huì)為這個(gè)組件添加選項(xiàng)卡,通過選項(xiàng)卡可切換不同組件. |
JSrollPane(滾動(dòng)面板) | 只可添加一個(gè)組件。和JTextArea配合,作為文本輸入欄的滾動(dòng)條。 |
JSplitPane(拆分面板) | 把面板分為兩部分,可以按左右分割,也可以按上下分割。 |
JLayeredPane(分層面板) | 用于組件重疊的處理,JavaSwing中定義5層,從低到高,依次為 DEFAULT_LAYER、PALETTE_LAYER、MODAL_LAYER、POPUP_LAYER、DRAG_LAYER。對(duì)于同一位置,高層覆蓋底層。對(duì)于同層,后來覆蓋新來。 |
? ? ? ? (5)普通組件
????????普通組件包括按鈕、輸入框、標(biāo)簽等。普通組件在定義之后,需要 container.add() 的方法添加到容器中,這樣在GUI界面中才會(huì)顯示。
Container.add(普通組件名);
JLabel | 標(biāo)簽 |
JButton | 按鈕 |
JRadioButton ? ? ? ?? |
單選按鈕 |
JCheckBox ? ? ? ? |
復(fù)選框 |
JToggleButton ? ? ? ?? |
開關(guān)按鈕 |
JPasswordField ? ? ? ?? |
密碼框 |
JTextField ? ? |
單行文本輸入 |
JTextArea ? ? ? ?? |
多行文本輸入 |
JComboBox ? ? ? ?? |
下拉列表框 |
JList ? ? ? ?? |
列表 |
JProgressBar ? ? ? ?? |
進(jìn)度條 |
JSlider ? ? ? ?? |
滑塊 |
? ? ? ? 例:
package TestCode;
/**
* @name 鷾
*/
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame {
private final Container container = getContentPane();
public Calculator(){
container.setLayout(null);
setContent();
setWindow();
}
private void setWindow(){
this.setVisible(true); //窗口可見
this.setSize(400,450); //窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗口可關(guān)閉
this.setLocationRelativeTo(null); //窗口居中
this.setTitle("簡易計(jì)算器"); //窗口標(biāo)題
}
private void setContent(){
/* 標(biāo)簽 */
JLabel jLabel1 = new JLabel("計(jì)算器");
jLabel1.setBounds(140,30,100,50);
jLabel1.setFont(new Font("楷體",Font.PLAIN,20));
JLabel jLabel2 = new JLabel("運(yùn)算數(shù)一");
jLabel2.setBounds(60,70,100,50);
jLabel2.setFont(new Font("楷體",Font.PLAIN,20));
JLabel jLabel3 = new JLabel("運(yùn)算數(shù)二");
jLabel3.setBounds(60,110,100,50);
jLabel3.setFont(new Font("楷體",Font.PLAIN,20));
JLabel jLabel4 = new JLabel("運(yùn)算結(jié)果");
jLabel4.setBounds(60,150,100,50);
jLabel4.setFont(new Font("楷體",Font.PLAIN,20));
/* 文本域 */
JTextField jTextField1 = new JTextField(20);
jTextField1.setBounds(170,80,150,30);
JTextField jTextField2 = new JTextField(20);
jTextField2.setBounds(170,120,150,30);
JTextField jTextField3 = new JTextField(20);
jTextField3.setBounds(170,160,150,30);
/* 按鈕 */
JButton jButton1 = new JButton("相加");
jButton1.setBounds(80,220,80,40);
jButton1.setFont(new Font("楷體",Font.PLAIN,20));
JButton jButton2 = new JButton("相減");
jButton2.setBounds(220,220,80,40);
jButton2.setFont(new Font("楷體",Font.PLAIN,20));
JButton jButton3 = new JButton("清零");
jButton3.setBounds(120,300,140,40);
jButton3.setFont(new Font("楷體",Font.PLAIN,20));
container.add(jLabel1);
container.add(jLabel2);
container.add(jLabel3);
container.add(jLabel4);
container.add(jTextField1);
container.add(jTextField2);
container.add(jTextField3);
container.add(jButton1);
container.add(jButton2);
container.add(jButton3);
}
public static void main(String[] args){
new Calculator();
}
}
? ? ? ? 效果:
????????(6)界面設(shè)計(jì)步驟
- 定義頂層容器對(duì)象
- 調(diào)用容器對(duì)象的 setLayout 方法給容器設(shè)置一種布局
- 定義普通圖形組件
- 調(diào)用容器對(duì)象?add 方法將普通圖形組件添加到容器上
? ? ? ? 2.事件處理
? ? ? ? (1)事件處理
? ? ? ? 上面的界面設(shè)計(jì)后,鼠標(biāo)點(diǎn)擊按鈕或者是在文本框中輸入內(nèi)容,界面是沒有任何反應(yīng)的。這是因?yàn)槲覀儍H僅完成了界面的外表,還有界面的內(nèi)核即事件處理需要我們編寫。
? ? ? ? (2)Java 事件處理機(jī)制
????????監(jiān)聽器實(shí)際上是接口,在接口中定義了事件發(fā)生時(shí)可調(diào)用的、必須要實(shí)現(xiàn)的方法。?每一種事件類,對(duì)應(yīng)要實(shí)現(xiàn)的接口。?每個(gè)接口,又有一個(gè)或多個(gè)方法必須要被實(shí)現(xiàn)(即接口被implements 時(shí)的語法要求)。這些方法中的代碼,就是對(duì)應(yīng)的事件的具體的處理代碼。
????????組件通過addActionListener的方法添加需要的監(jiān)聽器。
組件名.addActionListener(監(jiān)聽器對(duì)象);
如:jButton.addActionListener(new Event());
事件類
|
可關(guān)聯(lián)的組件 | 要實(shí)現(xiàn)的接口 | 接口中必須實(shí)現(xiàn)的方法 |
ActionEvent
|
Button,List, MenuItem,TextField
|
ActionListener
|
ActionPerformed(Actionevente)
|
ItemEvent
|
Checkbox,Choice,List,MenuItem
|
ItemListener
|
ItemStateChanged(ItemEvent e)
|
ContainerEvent
|
Container,Dialog,Frame,Panel,
ScrollPane,Window
|
ContainerListener
|
componentAdded(ContainerEvent
e)
componentRemoved(…)
|
MouseMotionEvent
|
Button,Canvas,Checkbox,choice,
Component
Container,Dialog,Frame,
Label,List,Panel,Scrollbar,ScrollPane,
TextArea,TextField,Window
|
MouseMotionListener
|
mouseGragged(MouseEvent e)
mouseMoved(MouseEvent e)
|
MouseEvent
|
同
MouseMontionEvent
|
MouseListener
|
mousePressed(MouseEvent e)
mouseReleased(…)
mouseEntered(…)
mouseExited(…)
mouseClicked(…)
|
KeyEvent
|
同
MouseMontionEvent
|
KeyEvent
|
keyPressed(KeyEvent e)
keyReleased(…)
keyTyped()
|
FocusEvent
|
同
MouseMontionEvent
|
FocusListener
|
focusGained(FocusEvent e)
focusLost(FocusEvent e)
|
AdjustmentEvent
|
Scrollbar
|
AdjustmentListener
|
adjustmentValueChanged(AdjustmentEvent? e)
|
ComponentEvent
|
同
MouseMontionEvent
|
ComponentListener
|
ComponentMoved(ComponentEvent e)
ComponentHidden(…)
ComponentResized(…)
ComponentShown(…)
|
TextEvent |
TextField,TextArea
|
TextListener
|
textValueChanged(TextEvent e)
|
WindowEvent
|
Dialog,Frame,Window
|
WindowListener
|
WindowClosing(WindowEvent e)
WindowOpened(…)
WindowIconified(…)
WindowClosed(…)
WindowDeiconfied(…)
WindowActivated(…)
WindowDeactivated(…)
|
ComponentAdapter | 組件適配器 |
ContainerAdapter | 容器適配器 |
FocusAdapter | 焦點(diǎn)適配器 |
KeyAdapter | 鍵盤適配器 |
MouseAdapter | 鼠標(biāo)適配器 |
MouseMotionAdapter | 鼠標(biāo)移動(dòng)適配器 |
WindowAdapter | 窗口適配器 |
????????(3)事務(wù)處理步驟
- 注冊(cè)監(jiān)聽器接口,讓可能產(chǎn)生事件的組件對(duì)事件進(jìn)行監(jiān)聽
- 實(shí)現(xiàn)接口中所有的方法(這些方法就是具體進(jìn)行事件處理的代碼)
????????(4)事務(wù)處理案例
import java.awt.event.*;
import javax.swing.*;
public class Case_1 {
JFrame jFrame = new JFrame("按鈕事件處理");
JButton jButton = new JButton("測試按鈕");
public Case_1() {
// addActionListener,對(duì)其可能發(fā)生的點(diǎn)擊事件(EventListener類型對(duì)象表示)進(jìn)行監(jiān)聽
// 調(diào)用其監(jiān)聽方法
jButton.addActionListener(new EventListener());
// 添加組件
jFrame.add(jButton);
// 容器jFrame設(shè)置為可見
jFrame.setVisible(true);
// 設(shè)置窗體大小
jFrame.setSize(400, 450);
// 設(shè)置窗體在屏幕上的位置
jFrame.setLocationRelativeTo(null);
//窗口可關(guān)閉
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new Case_1();
}
}
class EventListener implements ActionListener {
/* 實(shí)現(xiàn)ActionListener接口的actionPerformed(ActionEvent e)方法,由參數(shù)e接收事件信息,然后對(duì)傳入的事件信息進(jìn)行處理。*/
public void actionPerformed(ActionEvent e) {
System.out.println("按鈕事件處理的測試"); // 點(diǎn)擊一次按鈕后,操作臺(tái)輸出 “按鈕事件處理的測試” 的信息
}
}
? ? ? ?
import java.awt.event.*;
import javax.swing.*;
public class Case_2 {
//在定義類的同時(shí),就implements監(jiān)聽器接口
JFrame jFrame = new JFrame("按鈕事件處理");
JButton jButton = new JButton("測試按鈕");
public Case_2() {
// addActionListener,對(duì)其可能發(fā)生的點(diǎn)擊事件(EventListener類型對(duì)象表示)進(jìn)行監(jiān)聽
//對(duì)按鈕myButton,調(diào)用其監(jiān)聽方法
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按鈕事件處理的測試"); // 點(diǎn)擊一次按鈕后,操作臺(tái)輸出 “按鈕事件處理的測試” 的信息
}
});
// 添加組件
jFrame.add(jButton);
// 容器jFrame設(shè)置為可見
jFrame.setVisible(true);
// 設(shè)置窗體大小
jFrame.setSize(400, 450);
// 設(shè)置窗體在屏幕上的位置
jFrame.setLocationRelativeTo(null);
//窗口可關(guān)閉
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new Case_2();
}
}
? ? ? ? Case_3:
????????案例3和案例1功能上是一樣的,但是采用匿名new ActionListener()的方法。
import java.awt.event.*;
import javax.swing.*;
public class Case_3 {
JFrame jFrame = new JFrame("按鈕事件處理");
JButton jButton = new JButton("測試按鈕");
public Case_3() {
// addActionListener,對(duì)其可能發(fā)生的點(diǎn)擊事件(EventListener類型對(duì)象表示)進(jìn)行監(jiān)聽
// 采用匿名new ActionListener()的方法
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按鈕事件處理的測試"); // 點(diǎn)擊一次按鈕后,操作臺(tái)輸出 “按鈕事件處理的測試” 的信息
}
});
// 添加組件
jFrame.add(jButton);
// 容器jFrame設(shè)置為可見
jFrame.setVisible(true);
// 設(shè)置窗體大小
jFrame.setSize(400, 450);
// 設(shè)置窗體在屏幕上的位置
jFrame.setLocationRelativeTo(null);
//窗口可關(guān)閉
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Case_3();
}
}
? ? ? ? Case_4:
?????????產(chǎn)生GUI界面后,每點(diǎn)擊一次按鈕后,文本框類顯示“你點(diǎn)擊了這個(gè)按鈕·-·”的字樣。
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class Case_4 {
JFrame jFrame = new JFrame("一個(gè)按鈕事件處理的例子");
JButton jButton = new JButton("請(qǐng)點(diǎn)擊我");
JTextField jTextField = new JTextField(10);
public Case_4() {
//對(duì)按鈕myButton,調(diào)用其監(jiān)聽方法
jButton.addActionListener(new MyEvent(jTextField));
jFrame.setLayout(new FlowLayout());
// 添加組件
jFrame.add(jButton);
jFrame.add(jTextField);
// 容器jFrame設(shè)置為可見
jFrame.setVisible(true);
//讓容器jFrame上的組件在程序運(yùn)行之后就直接全部顯示出來
jFrame.pack();
// 設(shè)置窗體在屏幕上的位置
jFrame.setLocationRelativeTo(null);
//窗口可關(guān)閉
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Case_4(); //產(chǎn)生一個(gè)匿名對(duì)象,讓程序運(yùn)行起來。
}
}
class MyEvent implements ActionListener {
JTextField jTextField;
public MyEvent(JTextField jTextField) {
this.jTextField = jTextField;
}
public void actionPerformed(ActionEvent e) {
jTextField.setText("你點(diǎn)擊了這個(gè)按鈕·-·");
}
}
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class Case_5 implements ActionListener{
JFrame jFrame;
JTextField jTextField1, jTextField2;
public Case_5() {
jTextField1 = new JTextField(20);
jTextField2 = new JTextField(20);
// 將jTextField2對(duì)象設(shè)置為不可編輯
jTextField2.setEditable(false);
// 調(diào)用其監(jiān)聽方法
jTextField1.addActionListener(this);
jFrame = new JFrame();
// 添加組件
jFrame.add(jTextField1);
jFrame.add(jTextField2);
// 設(shè)置布局
jFrame.setLayout(new FlowLayout());
// 設(shè)置為可見
jFrame.setVisible(true);
// 設(shè)置窗體大小
jFrame.setSize(400, 100);
// 設(shè)置窗體在屏幕上的位置
jFrame.setLocationRelativeTo(null);
// 設(shè)置窗口標(biāo)題
jFrame.setTitle("學(xué)生姓名查詢");
//窗口可關(guān)閉
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 實(shí)現(xiàn)接口中的方法,進(jìn)行具體的事件處理
public void actionPerformed(ActionEvent evt) {
/* 當(dāng)用戶往 jTextField1 中輸入 black 并且按回車*/
if ((evt.getSource() == jTextField1) && (jTextField1.getText().equals("001"))) {
jTextField1.setText(""); // 把第一個(gè)輸入框清空
jTextField2.setText("Tom"); // 第二個(gè)輸入框顯示black的電話號(hào)碼
}
/* 當(dāng)用戶往 jTextField1 中輸入 smith 并且按回車*/
else if ((evt.getSource() == jTextField1) && (jTextField1.getText().equals("002"))) {
jTextField1.setText("");
jTextField2.setText("Tony");
}
else if ((evt.getSource() == jTextField1) && (jTextField1.getText().equals("003"))) {
jTextField1.setText("");
jTextField2.setText("Janny");
}
/* 當(dāng)輸入的不是其中任何一個(gè)人時(shí) */
else {
jTextField1.setText("");
jTextField2.setText("Can’t find his(her)");
}
}
public static void main(String[] args) {
new Case_5();
}
}
三、綜合案例
? ? ? ? 案例1.簡易計(jì)算器
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame {
private final Container container = getContentPane();
public Calculator(){
container.setLayout(null);
setContent();
setWindow();
}
private void setWindow(){
this.setVisible(true); //窗口可見
this.setSize(400,450); //窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗口可關(guān)閉
this.setLocationRelativeTo(null); //窗口居中
this.setTitle("簡易計(jì)算器"); //窗口標(biāo)題
}
private void setContent(){
/* 標(biāo)簽 */
JLabel jLabel1 = new JLabel("計(jì)算器");
jLabel1.setBounds(140,30,100,50);
jLabel1.setFont(new Font("楷體",Font.PLAIN,20));
JLabel jLabel2 = new JLabel("運(yùn)算數(shù)一");
jLabel2.setBounds(60,70,100,50);
jLabel2.setFont(new Font("楷體",Font.PLAIN,20));
JLabel jLabel3 = new JLabel("運(yùn)算數(shù)二");
jLabel3.setBounds(60,110,100,50);
jLabel3.setFont(new Font("楷體",Font.PLAIN,20));
JLabel jLabel4 = new JLabel("運(yùn)算結(jié)果");
jLabel4.setBounds(60,150,100,50);
jLabel4.setFont(new Font("楷體",Font.PLAIN,20));
/* 文本域 */
JTextField jTextField1 = new JTextField(20);
jTextField1.setBounds(170,80,150,30);
JTextField jTextField2 = new JTextField(20);
jTextField2.setBounds(170,120,150,30);
JTextField jTextField3 = new JTextField(20);
jTextField3.setBounds(170,160,150,30);
/* 按鈕 */
JButton jButton1 = new JButton("相加");
jButton1.setBounds(80,220,80,40);
jButton1.setFont(new Font("楷體",Font.PLAIN,20));
JButton jButton2 = new JButton("相減");
jButton2.setBounds(220,220,80,40);
jButton2.setFont(new Font("楷體",Font.PLAIN,20));
JButton jButton3 = new JButton("清零");
jButton3.setBounds(120,300,140,40);
jButton3.setFont(new Font("楷體",Font.PLAIN,20));
/* 相加 按鈕的監(jiān)聽事件 */
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text1 = jTextField1.getText();
if (text1.equals("")){
new ErrorPrompt();
return;
}
int num1 = Integer.parseInt(text1);
String text2 = jTextField2.getText();
if (text2.equals("")){
new ErrorPrompt();
return;
}
int num2 = Integer.parseInt(text2);
jTextField3.setText("" + (num1+num2));
}catch (NumberFormatException a){
new ErrorPrompt();
}
}
});
/* 相減 按鈕的監(jiān)聽事件 */
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text1 = jTextField1.getText();
if (text1.equals("")){
new ErrorPrompt();
return;
}
int num1 = Integer.parseInt(text1);
String text2 = jTextField2.getText();
if (text2.equals("")){
new ErrorPrompt();
return;
}
int num2 = Integer.parseInt(text2);
jTextField3.setText("" + (num1-num2));
}catch (NumberFormatException a){
new ErrorPrompt();
}
}
});
/* 全部清零 按鈕的監(jiān)聽事件 */
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField1.setText(""); //文本域全部清零
jTextField2.setText("");
jTextField3.setText("");
}
});
container.add(jLabel1);
container.add(jLabel2);
container.add(jLabel3);
container.add(jLabel4);
container.add(jTextField1);
container.add(jTextField2);
container.add(jTextField3);
container.add(jButton1);
container.add(jButton2);
container.add(jButton3);
}
}
class ErrorPrompt extends JFrame {
Container container = getContentPane();
public ErrorPrompt(){
container.setLayout(null);
setWindow();
setContent();
}
private void setWindow(){
this.setVisible(true);
this.setSize(300,200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //關(guān)閉當(dāng)前窗口
this.setTitle("錯(cuò)誤提示");
}
private void setContent(){
JLabel jLabel = new JLabel("請(qǐng)輸入正整數(shù)!");
jLabel.setBounds(70,30,140,80);
jLabel.setFont(new Font("楷體",Font.PLAIN,20));
jLabel.setForeground(Color.red); //設(shè)置字體為紅色
container.add(jLabel);
}
}
? ? ? ??Main.java
public class Main {
public static void main(String[] args) {
new Calculator();
}
}
? ? ? ? UML圖文章來源:http://www.zghlxwxcb.cn/news/detail-481908.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-481908.html
到了這里,關(guān)于Java | GUI 圖形用戶界面的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!