Ch02 作一個自己的視窗類別

Download Report

Transcript Ch02 作一個自己的視窗類別

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