?? cardtest.java
字號:
package ch02.section03;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class CardPanel
extends Panel {
ActionListener listener;
Panel create(LayoutManager layout) {
Button b = null;
Panel p = new Panel();
//設置布局方式
p.setLayout(layout);
b = new Button("one");
//添加事件監聽
b.addActionListener(listener);
//將b按鈕放在容器的上邊
p.add("North", b);
b = new Button("two");
b.addActionListener(listener);
//將b按鈕放在容器的左邊
p.add("West", b);
b = new Button("three");
b.addActionListener(listener);
//將b按鈕放在容器的下邊
p.add("South", b);
b = new Button("four");
b.addActionListener(listener);
//將b按鈕放在容器的右邊
p.add("East", b);
b = new Button("five");
b.addActionListener(listener);
//將b按鈕放在容器的中間
p.add("Center", b);
b = new Button("six");
b.addActionListener(listener);
p.add("Center", b);
return p;
}
CardPanel(ActionListener actionListener) {
listener = actionListener;
//設置布局方式為CardLayout
setLayout(new CardLayout());
add("one", create(new FlowLayout()));
add("two", create(new BorderLayout()));
add("three", create(new GridLayout(2, 2)));
add("four", create(new BorderLayout(10, 10)));
add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
add("six", create(new GridLayout(2, 2, 10, 10)));
}
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
}
//定義一個Applet
public class CardTest
extends Applet
implements ActionListener,
ItemListener {
CardPanel cards;
public CardTest() {
//設置布局方式為BorderLayout
setLayout(new BorderLayout());
//將一個容器放在中間
add("Center", cards = new CardPanel(this));
}
public void itemStateChanged(ItemEvent e) {
( (CardLayout) cards.getLayout()).show(cards,
(String) (e.getItem()));
}
//定義一個按鈕被點擊的方法
public void actionPerformed(ActionEvent e) {
//獲取事件源,返回一個字符串
String arg = e.getActionCommand();
/*
CardLayout中的第幾容器被返回
CardLayout布局方式會把加入的組件當作“卡片”一樣,
使用者一次只能看到最上面的那張卡片,如果想要看到你所
想要的卡片,則必須把那張卡片放到最上面來。
*/
if ("first".equals(arg)) {
/*
方法first(Container parent)作用是翻到第一張卡片,
parent 指的是有將此CardLayout設為layout manager的容器組件
*/
( (CardLayout) cards.getLayout()).first(cards);
}
else if ("next".equals(arg)) {
/*
方法next(Container parent)作用是翻到下一張卡片,
如果是最后一張,則會翻到第一張
*/
( (CardLayout) cards.getLayout()).next(cards);
}
else if ("previous".equals(arg)) {
/*
方法next(Container parent)作用是翻到前一張卡片,
如果是第一張,則會翻到最后一張
*/
( (CardLayout) cards.getLayout()).previous(cards);
}
else if ("last".equals(arg)) {
/*
方法next(Container parent)作用是翻到最后一張卡片
*/
( (CardLayout) cards.getLayout()).last(cards);
}
else {
( (CardLayout) cards.getLayout()).show(cards, (String) arg);
}
}
public static void main(String args[]) {
Frame f = new Frame("CardTest");
/*
添加關閉事件監聽
*/
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
CardTest cardTest = new CardTest();
//Applet啟動
cardTest.init();
cardTest.start();
//將組件放在Frame的中間
f.add("Center", cardTest);
//設置Frame窗口大小
f.setSize(300, 300);
//顯示Frame
f.show();
}
public String getAppletInfo() {
return "Demonstrates the different types of layout managers.";
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -