為注意到一些利用新事件模型的例子和為學習程序從老到新事件模型改變的方法,下面的例子回到在本章第一部分利用事件模型來證明的一些爭議。另外,每個程序包括程序片和應用程序現在都可以借助或不借助浏覽器來運行。
1. 文本字段
這個例子同TextField1.java相似,但它增加了顯然額外的行為:
//: TextNew.java
// Text fields with Java 1.1 events
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TextNew extends Applet {
Button
b1 = new Button("Get Text"),
b2 = new Button("Set Text");
TextField
t1 = new TextField(30),
t2 = new TextField(30),
t3 = new TextField(30);
String s = new String();
public void init() {
b1.addActionListener(new B1());
b2.addActionListener(new B2());
t1.addTextListener(new T1());
t1.addActionListener(new T1A());
t1.addKeyListener(new T1K());
add(b1);
add(b2);
add(t1);
add(t2);
add(t3);
}
class T1 implements TextListener {
public void textValueChanged(TextEvent e) {
t2.setText(t1.getText());
}
}
class T1A implements ActionListener {
private int count = 0;
public void actionPerformed(ActionEvent e) {
t3.setText("t1 Action Event " + count++);
}
}
class T1K extends KeyAdapter {
public void keyTyped(KeyEvent e) {
String ts = t1.getText();
if(e.getKeyChar() ==
KeyEvent.VK_BACK_SPACE) {
// Ensure it's not empty:
if( ts.length() > 0) {
ts = ts.substring(0, ts.length() - 1);
t1.setText(ts);
}
}
else
t1.setText(
t1.getText() +
Character.toUpperCase(
e.getKeyChar()));
t1.setCaretPosition(
t1.getText().length());
// Stop regular character from appearing:
e.consume();
}
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
s = t1.getSelectedText();
if(s.length() == 0) s = t1.getText();
t1.setEditable(true);
}
}
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t1.setText("Inserted by Button 2: " + s);
t1.setEditable(false);
}
}
public static void main(String[] args) {
TextNew applet = new TextNew();
Frame aFrame = new Frame("TextNew");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
} ///:~
當TextField t1的動作接收器被激活時,TextField t3就是一個需要報告的場所。我們注意到僅當我們按下“enter”鍵時,動作接收器才會為“TextField”所激活。
TextField t1附有幾個接收器。T1接收器從t1復制所有文字到t2,強制所有字符串轉換成大寫。我們會發現這兩個工作同是進行的,並且如果我們增加T1K接收器後我們再增加T1接收器,它就不那麼重要:在文字字段內的所有的字符串將一直被強制變為大寫。這看起來鍵盤事件一直在文字組件事件前被激活,並且如果我們需要保留t2的字符串原來輸入時的樣子,我們就必須做一些特別的工作。
T1K有著其它的一些有趣的活動。我們必須測試backspace(因為我們現在控制著每一個事件)並執行刪除。caret必須被明確地設置到字段的結尾;否則它不會像我們希望的運行。最後,為了防止原來的字符串被默認的機制所處理,事件必須利用為事件對象而存在的consume()方法所“耗盡”。這會通知系統停止激活其余特殊事件的事件處理器。
這個例子同樣無聲地證明了設計內部類的帶來的諸多優點。注意下面的內部類:
class T1 implements TextListener {
public void textValueChanged(TextEvent e) {
t2.setText(t1.getText());
}
}
t1和t2不屬於T1的一部分,並且到目前為止它們都是很容易理解的,沒有任何的特殊限制。這是因為一個內部類的對象能自動地捕捉一個句柄到外部的創建它的對象那裡,因此我們可以處理封裝類對象的方法和內容。正像我們看到的,這十分方便(注釋⑥)。
⑥:它也解決了“回調”的問題,不必為Java加入任何令人惱火的“方法指針”特性。
2. 文本區域
Java 1.1版中Text Area最重要的改變就滾動條。對於TextArea的構建器而言,我們可以立即控制TextArea是否會擁有滾動條:水平的,垂直的,兩者都有或者都沒有。這個例子更正了前面Java 1.0版TextArea1.java程序片,演示了Java 1.1版的滾動條構建器:
//: TextAreaNew.java
// Controlling scrollbars with the TextArea
// component in Java 1.1
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TextAreaNew extends Applet {
Button b1 = new Button("Text Area 1");
Button b2 = new Button("Text Area 2");
Button b3 = new Button("Replace Text");
Button b4 = new Button("Insert Text");
TextArea t1 = new TextArea("t1", 1, 30);
TextArea t2 = new TextArea("t2", 4, 30);
TextArea t3 = new TextArea("t3", 1, 30,
TextArea.SCROLLBARS_NONE);
TextArea t4 = new TextArea("t4", 10, 10,
TextArea.SCROLLBARS_VERTICAL_ONLY);
TextArea t5 = new TextArea("t5", 4, 30,
TextArea.SCROLLBARS_HORIZONTAL_ONLY);
TextArea t6 = new TextArea("t6", 10, 10,
TextArea.SCROLLBARS_BOTH);
public void init() {
b1.addActionListener(new B1L());
add(b1);
add(t1);
b2.addActionListener(new B2L());
add(b2);
add(t2);
b3.addActionListener(new B3L());
add(b3);
b4.addActionListener(new B4L());
add(b4);
add(t3); add(t4); add(t5); add(t6);
}
class B1L implements ActionListener {
public void actionPerformed(ActionEvent e) {
t5.append(t1.getText() + "\n");
}
}
class B2L implements ActionListener {
public void actionPerformed(ActionEvent e) {
t2.setText("Inserted by Button 2");
t2.append(": " + t1.getText());
t5.append(t2.getText() + "\n");
}
}
class B3L implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = " Replacement ";
t2.replaceRange(s, 3, 3 + s.length());
}
}
class B4L implements ActionListener {
public void actionPerformed(ActionEvent e) {
t2.insert(" Inserted ", 10);
}
}
public static void main(String[] args) {
TextAreaNew applet = new TextAreaNew();
Frame aFrame = new Frame("TextAreaNew");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,725);
applet.init();
applet.start();
aFrame.setVisible(true);
}
} ///:~
我們發現只能在構造TextArea時能夠控制滾動條。同樣,即使TE AR沒有滾動條,我們滾動光標也將被制止(可通過運行這個例子中驗證這種行為)。
3. 復選框和單選鈕
正如早先指出的那樣,復選框和單選鈕都是同一個類建立的。單選鈕和復選框略有不同,它是復選框安置到CheckboxGroup中構成的。在其中任一種情況下,有趣的ItemEvent事件為我們創建一個ItemListener項目接收器。
當處理一組復選框或者單選鈕時,我們有一個不錯的選擇。我們可以創建一個新的內部類去為每個復選框處理事件,或者創建一個內部類判斷哪個復選框被單擊並注冊一個內部類單獨的對象為每個復選對象。下面的例子演示了兩種方法:
//: RadioCheckNew.java
// Radio buttons and Check Boxes in Java 1.1
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class RadioCheckNew extends Applet {
TextField t = new TextField(30);
Checkbox[] cb = {
new Checkbox("Check Box 1"),
new Checkbox("Check Box 2"),
new Checkbox("Check Box 3") };
CheckboxGroup g = new CheckboxGroup();
Checkbox
cb4 = new Checkbox("four", g, false),
cb5 = new Checkbox("five", g, true),
cb6 = new Checkbox("six", g, false);
public void init() {
t.setEditable(false);
add(t);
ILCheck il = new ILCheck();
for(int i = 0; i < cb.length; i++) {
cb[i].addItemListener(il);
add(cb[i]);
}
cb4.addItemListener(new IL4());
cb5.addItemListener(new IL5());
cb6.addItemListener(new IL6());
add(cb4); add(cb5); add(cb6);
}
// Checking the source:
class ILCheck implements ItemListener {
public void itemStateChanged(ItemEvent e) {
for(int i = 0; i < cb.length; i++) {
if(e.getSource().equals(cb[i])) {
t.setText("Check box " + (i + 1));
return;
}
}
}
}
// vs. an individual class for each item:
class IL4 implements ItemListener {
public void itemStateChanged(ItemEvent e) {
t.setText("Radio button four");
}
}
class IL5 implements ItemListener {
public void itemStateChanged(ItemEvent e) {
t.setText("Radio button five");
}
}
class IL6 implements ItemListener {
public void itemStateChanged(ItemEvent e) {
t.setText("Radio button six");
}
}
public static void main(String[] args) {
RadioCheckNew applet = new RadioCheckNew();
Frame aFrame = new Frame("RadioCheckNew");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
} ///:~
ILCheck擁有當我們增加或者減少復選框時自動調整的優點。當然,我們對單選鈕使用這種方法也同樣的好。但是,它僅當我們的邏輯足以普遍的支持這種方法時才會被使用。如果聲明一個確定的信號——我們將重復利用獨立的接收器類,否則我們將結束一串條件語句。
4. 下拉列表
下拉列表在Java 1.1版中當一個選擇被改變時同樣使用ItemListener去告知我們:
//: ChoiceNew.java
// Drop-down lists with Java 1.1
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ChoiceNew extends Applet {
String[] description = { "Ebullient", "Obtuse",
"Recalcitrant", "Brilliant", "Somnescent",
"Timorous", "Florid", "Putrescent" };
TextField t = new TextField(100);
Choice c = new Choice();
Button b = new Button("Add items");
int count = 0;
public void init() {
t.setEditable(false);
for(int i = 0; i < 4; i++)
c.addItem(description[count++]);
add(t);
add(c);
add(b);
c.addItemListener(new CL());
b.addActionListener(new BL());
}
class CL implements ItemListener {
public void itemStateChanged(ItemEvent e) {
t.setText("index: " + c.getSelectedIndex()
+ " " + e.toString());
}
}
class BL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(count < description.length)
c.addItem(description[count++]);
}
}
public static void main(String[] args) {
ChoiceNew applet = new ChoiceNew();
Frame aFrame = new Frame("ChoiceNew");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(750,100);
applet.init();
applet.start();
aFrame.setVisible(true);
}
} ///:~
這個程序中沒什麼特別新穎的東西(除了Java 1.1版的UI類裡少數幾個值得關注的缺陷)。
5. 列表
我們消除了Java 1.0中List設計的一個缺陷,就是List不能像我們希望的那樣工作:它會與單擊在一個列表元素上發生沖突。
//: ListNew.java
// Java 1.1 Lists are easier to use
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ListNew extends Applet {
String[] flavors = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip",
"Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie" };
// Show 6 items, allow multiple selection:
List lst = new List(6, true);
TextArea t = new TextArea(flavors.length, 30);
Button b = new Button("test");
int count = 0;
public void init() {
t.setEditable(false);
for(int i = 0; i < 4; i++)
lst.addItem(flavors[count++]);
add(t);
add(lst);
add(b);
lst.addItemListener(new LL());
b.addActionListener(new BL());
}
class LL implements ItemListener {
public void itemStateChanged(ItemEvent e) {
t.setText("");
String[] items = lst.getSelectedItems();
for(int i = 0; i < items.length; i++)
t.append(items[i] + "\n");
}
}
class BL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(count < flavors.length)
lst.addItem(flavors[count++], 0);
}
}
public static void main(String[] args) {
ListNew applet = new ListNew();
Frame aFrame = new Frame("ListNew");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
} ///:~
我們可以注意到在列表項中無需特別的邏輯需要去支持一個單擊動作。我們正好像我們在其它地方所做的那樣附加上一個接收器。
6. 菜單
為菜單處理事件看起來受益於Java 1.1版的事件模型,但Java生成菜單的方法常常麻煩並且需要一些手工編寫代碼。生成菜單的正確方法看起來像資源而不是一些代碼。請牢牢記住編程工具會廣泛地為我們處理創建的菜單,因此這可以減少我們的痛苦(只要它們會同樣處理維護任務!)。另外,我們將發現菜單不支持並且將導致混亂的事件:菜單項使用ActionListeners(動作接收器),但復選框菜單項使用ItemListeners(項目接收器)。菜單對象同樣能支持ActionListeners(動作接收器),但通常不那麼有用。一般來說,我們會附加接收器到每個菜單項或復選框菜單項,但下面的例子(對先前例子的修改)演示了一個聯合捕捉多個菜單組件到一個單獨的接收器類的方法。正像我們將看到的,它或許不值得為這而激烈地爭論。
//: MenuNew.java
// Menus in Java 1.1
import java.awt.*;
import java.awt.event.*;
public class MenuNew extends Frame {
String[] flavors = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip",
"Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie" };
TextField t = new TextField("No flavor", 30);
MenuBar mb1 = new MenuBar();
Menu f = new Menu("File");
Menu m = new Menu("Flavors");
Menu s = new Menu("Safety");
// Alternative approach:
CheckboxMenuItem[] safety = {
new CheckboxMenuItem("Guard"),
new CheckboxMenuItem("Hide")
};
MenuItem[] file = {
// No menu shortcut:
new MenuItem("Open"),
// Adding a menu shortcut is very simple:
new MenuItem("Exit",
new MenuShortcut(KeyEvent.VK_E))
};
// A second menu bar to swap to:
MenuBar mb2 = new MenuBar();
Menu fooBar = new Menu("fooBar");
MenuItem[] other = {
new MenuItem("Foo"),
new MenuItem("Bar"),
new MenuItem("Baz"),
};
// Initialization code:
{
ML ml = new ML();
CMIL cmil = new CMIL();
safety[0].setActionCommand("Guard");
safety[0].addItemListener(cmil);
safety[1].setActionCommand("Hide");
safety[1].addItemListener(cmil);
file[0].setActionCommand("Open");
file[0].addActionListener(ml);
file[1].setActionCommand("Exit");
file[1].addActionListener(ml);
other[0].addActionListener(new FooL());
other[1].addActionListener(new BarL());
other[2].addActionListener(new BazL());
}
Button b = new Button("Swap Menus");
public MenuNew() {
FL fl = new FL();
for(int i = 0; i < flavors.length; i++) {
MenuItem mi = new MenuItem(flavors[i]);
mi.addActionListener(fl);
m.add(mi);
// Add separators at intervals:
if((i+1) % 3 == 0)
m.addSeparator();
}
for(int i = 0; i < safety.length; i++)
s.add(safety[i]);
f.add(s);
for(int i = 0; i < file.length; i++)
f.add(file[i]);
mb1.add(f);
mb1.add(m);
setMenuBar(mb1);
t.setEditable(false);
add(t, BorderLayout.CENTER);
// Set up the system for swapping menus:
b.addActionListener(new BL());
add(b, BorderLayout.NORTH);
for(int i = 0; i < other.length; i++)
fooBar.add(other[i]);
mb2.add(fooBar);
}
class BL implements ActionListener {
public void actionPerformed(ActionEvent e) {
MenuBar m = getMenuBar();
if(m == mb1) setMenuBar(mb2);
else if (m == mb2) setMenuBar(mb1);
}
}
class ML implements ActionListener {
public void actionPerformed(ActionEvent e) {
MenuItem target = (MenuItem)e.getSource();
String actionCommand =
target.getActionCommand();
if(actionCommand.equals("Open")) {
String s = t.getText();
boolean chosen = false;
for(int i = 0; i < flavors.length; i++)
if(s.equals(flavors[i])) chosen = true;
if(!chosen)
t.setText("Choose a flavor first!");
else
t.setText("Opening "+ s +". Mmm, mm!");
} else if(actionCommand.equals("Exit")) {
dispatchEvent(
new WindowEvent(MenuNew.this,
WindowEvent.WINDOW_CLOSING));
}
}
}
class FL implements ActionListener {
public void actionPerformed(ActionEvent e) {
MenuItem target = (MenuItem)e.getSource();
t.setText(target.getLabel());
}
}
// Alternatively, you can create a different
// class for each different MenuItem. Then you
// Don't have to figure out which one it is:
class FooL implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText("Foo selected");
}
}
class BarL implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText("Bar selected");
}
}
class BazL implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText("Baz selected");
}
}
class CMIL implements ItemListener {
public void itemStateChanged(ItemEvent e) {
CheckboxMenuItem target =
(CheckboxMenuItem)e.getSource();
String actionCommand =
target.getActionCommand();
if(actionCommand.equals("Guard"))
t.setText("Guard the Ice Cream! " +
"Guarding is " + target.getState());
else if(actionCommand.equals("Hide"))
t.setText("Hide the Ice Cream! " +
"Is it cold? " + target.getState());
}
}
public static void main(String[] args) {
MenuNew f = new MenuNew();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(300,200);
f.setVisible(true);
}
} ///:~
在我們開始初始化節(由注解“Initialization code:”後的右大括號指明)的前面部分的代碼同先前(Java 1.0版)版本相同。這裡我們可以注意到項目接收器和動作接收器被附加在不同的菜單組件上。
Java 1.1支持“菜單快捷鍵”,因此我們可以選擇一個菜單項目利用鍵盤替代鼠標。這十分的簡單;我們只要使用過載菜單項構建器設置第二個自變量為一個MenuShortcut(菜單快捷鍵事件)對象即可。菜單快捷鍵構建器設置重要的方法,當它按下時不可思議地顯示在菜單項上。上面的例子增加了Control-E到“Exit”
菜單項中。
我們同樣會注意setActionCommand()的使用。這看似一點陌生因為在各種情況下“action command”完全同菜單組件上的標簽一樣。為什麼不正好使用標簽代替可選擇的字符串呢?這個難題是國際化的。如果我們重新用其它語言寫這個程序,我們只需要改變菜單中的標簽,並不審查代碼中可能包含新錯誤的所有邏輯。因此使這對檢查文字字符串聯合菜單組件的代碼而言變得簡單容易,當菜單標簽能改變時“動作指令”可以不作任何的改變。所有這些代碼同“動作指令”一同工作,因此它不會受改變菜單標簽的影響。注意在這個程序中,不是所有的菜單組件都被它們的動作指令所審查,因此這些組件都沒有它們的動作指令集。
大多數的構建器同前面的一樣,將幾個調用的異常增加到接收器中。大量的工作發生在接收器裡。在前面例子的BL中,菜單交替發生。在ML中,“尋找ring”方法被作為動作事件(ActionEvent)的資源並對它進行造型送入菜單項,然後得到動作指令字符串,再通過它去貫穿串聯組,當然條件是對它進行聲明。這些大多數同前面的一樣,但請注意如果“Exit”被選中,通過進入封裝類對象的句柄(MenuNew.this)並創建一個WINDOW_CLOSING事件,一個新的窗口事件就被創建了。新的事件被分配到封裝類對象的dispatchEvent()方法,然後結束調用windowsClosing()內部幀的窗口接收器(這個接收器作為一個內部類被創建在main()裡),似乎這是“正常”產生消息的方法。通過這種機制,我們可以在任何情況下迅速處理任何的信息,因此,它非常的強大。
FL接收器是很簡單盡管它能處理特殊菜單的所有不同的特色。如果我們的邏輯十分的簡單明了,這種方法對我們就很有用處,但通常,我們使用這種方法時需要與FooL,BarL和BazL一道使用,它們每個都附加到一個單獨的菜單組件上,因此必然無需測試邏輯,並且使我們正確地辨識出誰調用了接收器。這種方法產生了大量的類,內部代碼趨向於變得小巧和處理起來簡單、安全。
7. 對話框
在這個例子裡直接重寫了早期的ToeTest.java程序。在這個新的版本裡,任何事件都被安放進一個內部類中。雖然這完全消除了需要記錄產生的任何類的麻煩,作為ToeTest.java的一個例子,它能使內部類的概念變得不那遙遠。在這點,內嵌類被嵌套達四層之深!我們需要的這種設計決定了內部類的優點是否值得增加更加復雜的事物。另外,當我們創建一個非靜態的內部類時,我們將捆綁非靜態類到它周圍的類上。有時,單獨的類可以更容易地被復用。
//: ToeTestNew.java
// Demonstration of dialog boxes
// and creating your own components
import java.awt.*;
import java.awt.event.*;
public class ToeTestNew extends Frame {
TextField rows = new TextField("3");
TextField cols = new TextField("3");
public ToeTestNew() {
setTitle("Toe Test");
Panel p = new Panel();
p.setLayout(new GridLayout(2,2));
p.add(new Label("Rows", Label.CENTER));
p.add(rows);
p.add(new Label("Columns", Label.CENTER));
p.add(cols);
add(p, BorderLayout.NORTH);
Button b = new Button("go");
b.addActionListener(new BL());
add(b, BorderLayout.SOUTH);
}
static final int BLANK = 0;
static final int XX = 1;
static final int OO = 2;
class ToeDialog extends Dialog {
// w = number of cells wide
// h = number of cells high
int turn = XX; // Start with x's turn
public ToeDialog(int w, int h) {
super(ToeTestNew.this,
"The game itself", false);
setLayout(new GridLayout(w, h));
for(int i = 0; i < w * h; i++)
add(new ToeButton());
setSize(w * 50, h * 50);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
}
});
}
class ToeButton extends Canvas {
int state = BLANK;
ToeButton() {
addMouseListener(new ML());
}
public void paint(Graphics g) {
int x1 = 0;
int y1 = 0;
int x2 = getSize().width - 1;
int y2 = getSize().height - 1;
g.drawRect(x1, y1, x2, y2);
x1 = x2/4;
y1 = y2/4;
int wide = x2/2;
int high = y2/2;
if(state == XX) {
g.drawLine(x1, y1,
x1 + wide, y1 + high);
g.drawLine(x1, y1 + high,
x1 + wide, y1);
}
if(state == OO) {
g.drawOval(x1, y1,
x1 + wide/2, y1 + high/2);
}
}
class ML extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if(state == BLANK) {
state = turn;
turn = (turn == XX ? OO : XX);
}
else
state = (state == XX ? OO : XX);
repaint();
}
}
}
}
class BL implements ActionListener {
public void actionPerformed(ActionEvent e) {
Dialog d = new ToeDialog(
Integer.parseInt(rows.getText()),
Integer.parseInt(cols.getText()));
d.show();
}
}
public static void main(String[] args) {
Frame f = new ToeTestNew();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(200,100);
f.setVisible(true);
}
} ///:~
由於“靜態”的東西只能位於類的外部一級,所以內部類不可能擁有靜態數據或者靜態內部類。
8. 文件對話框
這個例子是直接用新事件模型對FileDialogTest.java修改而來。
//: FileDialogNew.java
// Demonstration of File dialog boxes
import java.awt.*;
import java.awt.event.*;
public class FileDialogNew extends Frame {
TextField filename = new TextField();
TextField directory = new TextField();
Button open = new Button("Open");
Button save = new Button("Save");
public FileDialogNew() {
setTitle("File Dialog Test");
Panel p = new Panel();
p.setLayout(new FlowLayout());
open.addActionListener(new OpenL());
p.add(open);
save.addActionListener(new SaveL());
p.add(save);
add(p, BorderLayout.SOUTH);
directory.setEditable(false);
filename.setEditable(false);
p = new Panel();
p.setLayout(new GridLayout(2,1));
p.add(filename);
p.add(directory);
add(p, BorderLayout.NORTH);
}
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Two arguments, defaults to open file:
FileDialog d = new FileDialog(
FileDialogNew.this,
"What file do you want to open?");
d.setFile("*.java");
d.setDirectory("."); // Current directory
d.show();
String yourFile = "*.*";
if((yourFile = d.getFile()) != null) {
filename.setText(yourFile);
directory.setText(d.getDirectory());
} else {
filename.setText("You pressed cancel");
directory.setText("");
}
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
FileDialog d = new FileDialog(
FileDialogNew.this,
"What file do you want to save?",
FileDialog.SAVE);
d.setFile("*.java");
d.setDirectory(".");
d.show();
String saveFile;
if((saveFile = d.getFile()) != null) {
filename.setText(saveFile);
directory.setText(d.getDirectory());
} else {
filename.setText("You pressed cancel");
directory.setText("");
}
}
}
public static void main(String[] args) {
Frame f = new FileDialogNew();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(250,110);
f.setVisible(true);
}
} ///:~
如果所有的改變是這樣的容易那將有多棒,但至少它們已足夠容易,並且我們的代碼已受益於這改進的可讀性上。