Welcome

首页 / 软件开发 / JAVA / 再探早期示例

再探早期示例2007-05-29 yycnet.yeah.net yyc译为注意到一些利用新事件模型的例子和为学习程序从老到新事件模型改变的方法,下面的例子回到在本章第一部分利用事件模型来证明的一些争议。另外,每个程序包括程序片和应用程序现在都可以借助或不借助浏览器来运行。

1. 文本字段
这个例子同TextField1.java相似,但它增加了显然额外的行为:
//: TextNew.java// Text fields with Java 1.1 eventsimport 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);}} elset1.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.1import 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() + "
");}}class B2L implements ActionListener {public void actionPerformed(ActionEvent e) {t2.setText("Inserted by Button 2");t2.append(": " + t1.getText());t5.append(t2.getText() + "
");}}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.1import 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.1import 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 useimport 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] + "
");}}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.1import 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!");elset.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 componentsimport 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 highint turn = XX; // Start with x"s turnpublic 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(Graphicsg) {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);} elsestate = (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 boxesimport 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 directoryd.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);}} ///:~
如果所有的改变是这样的容易那将有多棒,但至少它们已足够容易,并且我们的代码已受益于这改进的可读性上。