Ch14 視窗程式設計(1)

Download Report

Transcript Ch14 視窗程式設計(1)

JAVA程式語言入門(II)


版面配置
事件驅動

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Ch14_01.java
import javax.swing.*;
import java.awt.*;
class Ch14_01
{
public static void main(String [] args)
{
Ch14_Win app = new Ch14_Win("視窗程式Ch14_01");
app.setDefaultCloseOperation(app.EXIT_ON_CLOSE);
app.setSize(200, 300);
app.setVisible(true);
}
}
1.
2.
3.
4.
5.
6.
7.
8.
import javax.swing.*;
import java.awt.*;
class Ch14_Win extends JFrame
{
//建構子
Ch14_Win(String title)
{
super(title);
//Step 1: 取得可放置元件的內容版面
Container c = getContentPane();
9.
10.
17.
//Step 2: 宣告加入視窗的按鈕元件
JButton b1 = new JButton("b1");
18.
//Step 3: 將元件加入面版
19.
c.add(b1);
20.
//Step 4: 處理元件動作
11.
21.
22.
}
}











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));
















//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: 處理元件動作


}



}
















方法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); //重設文字
內容
}
}














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 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 1: 取得可放置元件的內容
版面
Container c = getContentPane();
c.setLayout(new FlowLayout());



}
}
//Step 3: 將元件加入面版
c.add(b1);
c.add(l1);