ch03 為自己的視窗加上小元件
DESCRIPTION
CH03 為自己的視窗加上小元件. 物件導向系統實務. 選擇前兩次介紹的各種建窗方式 , 建立一個視窗. 事件與傾聽者間的關係. 屬 java.awt.event.* 的 mouseListener 介面,介面中定義了 mouseClick, mousePress… 等方法. [ 按鈕1 ] 委任一個 MouseListener 物件,以接收並回 應 mouseClick 事件源. 事件 : 敲擊滑鼠按鍵 ( mouseClick). . 屬 java.awt.* 的 Button 內的方法. addMouseListener(…). 按鈕 1. - PowerPoint PPT PresentationTRANSCRIPT
CH03 為自己的視窗加上小元件物件導向系統實務
選擇前兩次介紹的各種建窗方式 ,建立一個視窗
112/04/21
2
事件與傾聽者間的關係
112/04/21 3
按鈕 1
事件 :敲擊滑鼠按鍵(mouseClick)
[按鈕1 ]委任一個MouseListener物件,以接收並回應mouseClick事件源
M ouseListener物件 委任一個
addMouseListener(…)
接收及回應mouseClick
屬 java.awt.event.*的 mouseListener介面,介面中定義了mouseClick,
mousePress…等方法
屬 java.awt.*的Button內的方法
加入一個標籤元件
112/04/21
4
標籤 JLABEL
JLabel元件繼承自 JComponent類別,可以顯示一段文字內容或圖示 JLabel label1 = new JLabel(“CLOSE”); JLabel label1 = new Jlabel(new ImageIcon(“red.gif”);
javax.swing class JLabelstatic final int CENTERstatic final int LEFTstatic final int TOPstatic final int BOTTOMstatic final int RIGHTstatic final int NORTHstatic final int EASTstatic final int SOUTHstatic final int WEST JLabel() JLabel(Icon image) JLabel(String text) JLabel(String text, Icon image)String getText()void setText()
112/04/21
5
範例 1 :加上一個標籤1. import javax.swing.*;2. import java.awt.*;3. import java.awt.event.*;
4. class Win6_15. { public static void main(String [] args)6. { MyJFrame6_1 f = new MyJFrame6_1();7. f.setSize(200,300);8. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);9. f.setVisible(true);10. }11. }
12. class MyJFrame6_1 extends JFrame13. { JLabel label1;
14. MyJFrame6_1()15. { super("Win6_1");16. label1 = new JLabel("Hello JAVA World!!");17. add(label1);18. }19. }
112/04/21
6
加入一個按鈕元件
112/04/21
7
按鈕元件 JBUTTON
可以使用滑鼠按一下的按鈕元件,要完成可接收到按鈕指令,需要三步驟:1. 建立 JButton元件外觀2. 接上事件傾聽者3. 處理事件(寫程式,當發生按按鈕時,要做什麼)
JButton類別:
112/04/21
8
javax.swing class JButton JButton() JButton(String text) JButton(ImageIcon image) JButton(String text, ImageIcon image)String getText()void setText(String text)char getMnemonic()void setMnemoic(int key)void addActionListener(ActionListener ac)
java.awt interface ActionListener
void actionPerformed(ActionEvent e)
範例2:將範例1的視窗加上一個按鈕1. import javax.swing.*;2. import java.awt.*;3. import java.awt.event.*;4. class Win6_25. { public static void main(String [] args)6. { MyJFrame6_2 f = new MyJFrame6_2();7. f.setSize(200,300);8. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);9. f.setVisible(true);10. }11. }12. class MyJFrame6_2 extends Jframe implements ActionListener13. { JLabel label1;14. JButton button1;15. MyJFrame6_2()16. { super("Win6_2");17. label1 = new JLabel("Hello JAVA World!!");18. add(label1);19. button1 = new JButton("按我 ");20. button1.addActionListener(this);21. add(button1);22. }
23. public void actionPerformed(ActionEvent e)24. { label1.setText("你按到按鈕了 ");25. }26. }
112/04/21
9
討論:範例2的執行結果怪怪的? 我們需要為視窗作一個版面配置
112/04/21
10
版面配置 LAYOUT MANAGER
版面配置管理員( Layout Manager)可以編排新增的元件
不同的版面配置管理員,擁有不同預設的編排方式,只需依照需求選擇使用的版面配置管理員,就可以編排出漂亮的GUI介面
FlowLayout版面配置
112/04/21
11
java.awt class FlowLayout
static final int LEFT
static final int RIGHT
static final int CENTER
FlowLayout()
FlowLayout(int align)
FlowLayout(int align, int hgap,int vgap)
javax.swing class JFrame
void setLayout(LayoutManager manger)
範例3:為範例2的視窗加上FLOWLAYOUT1. import javax.swing.*;
2. import java.awt.*;
3. import java.awt.event.*;
4. class Win6_3
5. { public static void main(String [] args)
6. { MyJFrame6_3 f = new MyJFrame6_3();
7. f.setSize(200,300);
8. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9. f.setVisible(true);
10. }
11. }
12. class MyJFrame6_3 extends Jframe implements ActionListener
13. { JLabel label1;
14. JButton button1;
15. MyJFrame6_3()
16. { super("Win6_3");
17. this.setLayout(new FlowLayout(FlowLayout.CENTER));
18. label1 = new JLabel("Hello JAVA World!!");
19. add(label1);
20. button1 = new JButton("按我 ");
21. button1.addActionListener(this);
22. add(button1);
23. }
24.
25. public void actionPerformed(ActionEvent e)
26. { label1.setText("你按到按鈕了 ");
27. }
28. }
112/04/21
12
作業修改範例 3 ,使得每按一次按鈕,標籤就會出現“你按了?次按鈕”
112/04/21
13