Download presentation
Presentation is loading. Please wait.
Published byPhillip Millsap Modified over 9 years ago
1
JAVA 程式語言入門 (II)
2
版面配置 事件驅動
3
Ch14_01.java 1. import javax.swing.*; 2. import java.awt.*; 3. class Ch14_01 4. { 5. public static void main(String [] args) 6. { 7. Ch14_Win app = new Ch14_Win(" 視窗程式 Ch14_01"); 8. app.setDefaultCloseOperation(app.EXIT_ON_CLOSE); 9. app.setSize(200, 300); 10. app.setVisible(true); 11. } 12. }
4
1. import javax.swing.*; 2. import java.awt.*; 3. class Ch14_Win extends JFrame 4. { 5. // 建構子 6. Ch14_Win(String title) 7. { 8. super(title); 9. //Step 1: 取得可放置元件的內容版面 10. Container c = getContentPane(); 11. //Step 2: 宣告加入視窗的按鈕元件 17. JButton b1 = new JButton("b1"); 18. //Step 3: 將元件加入面版 19. c.add(b1); 20. //Step 4: 處理元件動作 21. } 22. }
5
import javax.swing.*; import java.awt.*; class Ch14_Win extends JFrame { // 建構子 Ch14_Win(String title) { super(title); //Step 1: 取得可放置元件的 內容版面 Container c = getContentPane(); c.setLayout(new FlowLayout(FlowLayout.LEFT)); //Step 1.1: 加入佈局管理員 //(1) 外框佈局 BorderLayout JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout(5,10)); //(2) 循序佈局 FlowLayout JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout(FlowLayout.LEFT, 5,10)); //(3) 格狀佈局 GridLayout JPanel p3 = new JPanel(); p3.setLayout(new GridLayout(2,3,5,10));
6
//Step 2: 宣告加入視窗的按鈕元件 JButton b1_1 = new JButton("b1_1"); JButton b1_2 = new JButton("b1_2"); JButton b1_3 = new JButton("b1_3"); JButton b1_4 = new JButton("b1_4"); JButton b1_5 = new JButton("b1_5"); JButton b2_1 = new JButton("b2_1"); JButton b2_2 = new JButton("b2_2"); JButton b2_3 = new JButton("b2_3"); JButton b2_4 = new JButton("b2_4"); JButton b2_5 = new JButton("b2_5"); JButton b3_1 = new JButton("b3_1"); JButton b3_2 = new JButton("b3_2"); JButton b3_3 = new JButton("b3_3"); JButton b3_4 = new JButton("b3_4"); JButton b3_5 = new JButton("b3_5"); //Step 3: 將元件加入面版 p1.add(b1_1, BorderLayout.EAST); p1.add(b1_2, BorderLayout.WEST); p1.add(b1_3, BorderLayout.NORTH); p1.add(b1_4, BorderLayout.SOUTH); p1.add(b1_5, BorderLayout.CENTER); p2.add(b2_1); p2.add(b2_2); p2.add(b2_3); p2.add(b2_4); p2.add(b2_5); p3.add(b3_1); p3.add(b3_2); p3.add(b3_3); p3.add(b3_4); p3.add(b3_5); c.add(p1); c.add(p2); c.add(p3); //Step 4: 處理元件動作 } }
7
方法 1 import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14_Win extends JFrame implements ActionListener { int num = 0; String s1 = " 你按了幾次 : " ; JLabel l1 = new JLabel(s1); // 建構子 Ch14_Win(String title) { super(title); //Step 1: 取得可放置元件的內容版 面 Container c = getContentPane(); c.setLayout(new FlowLayout()); //Step 2: 宣告加入視窗的按鈕元件 JButton b1 = new JButton("b1"); b1.addActionListener(this); //Step 3: 將元件加入面版 c.add(b1); c.add(l1); } //Step 4: 處理元件動作 public void actionPerformed(ActionEvent evt) { num++; l1.setText(s1 + num); // 重設文字 內容 } }
8
import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14_Win extends JFrame { int num = 0; String s1 = " 你按了幾次 : " ; JLabel l1 = new JLabel(s1); // 建構子 Ch14_Win(String title) { super(title); //Step 1: 取得可放置元件的內容 版面 Container c = getContentPane(); c.setLayout(new FlowLayout()); //Step 2: 宣告加入視窗的按鈕元 件 +Step 4: 處理元件動作 JButton b1 = new JButton("b1"); ActionListener a1 = new ActionListener() { public void actionPerformed(ActionEvent e) { num++; l1.setText(s1 + num); // 重設文字內容 } }; b1.addActionListener(a1); //Step 3: 將元件加入面版 c.add(b1); c.add(l1); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.