友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
一世书城 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

Java编程思想第4版[中文版](PDF格式)-第142章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




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〃);  



                                                                                          430 


…………………………………………………………Page 432……………………………………………………………

      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);  

  }  

} ///:~  

  

如果所有的改变是这样的容易那将有多棒,但至少它们已足够容易,并且我们的代码已受益于这改进的可读 

性上。  



13。16。5 动态绑定事件  



新AWT 事件模型给我们带来的一个好处就是灵活性。在老的模型中我们被迫为我们的程序动作艰难地编写代 

码。但新的模型我们可以用单一方法调用增加和删除事件动作。下面的例子证明了这一点:  

  

//: DynamicEvents。java  

// The new Java 1。1 event model allows you to  

// change event behavior dynamically。 Also  

// demonstrates multiple actions for an event。  

import java。awt。*;  

import java。awt。event。*;  

import java。util。*;  

  

public class DynamicEvents extends Frame {  

  Vector v = new Vector();  

  int i = 0;  

  Button   

    b1 = new Button(〃Button 1〃);   

    b2 = new Button(〃Button 2〃);  

  public DynamicEvents() {  

    setLayout(new FlowLayout());  

    b1。addActionListener(new B());  

    b1。addActionListener(new B1());  

    b2。addActionListener(new B());  

    b2。addActionListener(new B2());  



                                                                                          431 


…………………………………………………………Page 433……………………………………………………………

    add(b1);  

    add(b2);  

  }  

  class B implements ActionListener {  

    public void actionPerformed(ActionEvent e) {  

      System。out。println(〃A button was pressed〃);  

    }  

  }  

  class CountListener implements ActionListener {  

    int index;  

    public CountListener(int i) { index = i; }  

    public void actionPerformed(ActionEvent e) {  

      System。out。println(  

        〃Counted Listener 〃 + index);  

    }  

  }      

  class B1 implements ActionListener {  

    public void actionPerformed(ActionEvent e) {  

      System。out。println(〃Button 1 pressed〃);  

      ActionListener a = new CountListener(i++);  

      v。addElement(a);  

      b2。addActionListener(a);  

    }  

  }  

  class B2 implements ActionListener {  

    public void actionPerformed(ActionEvent e) {  

      System。out。println(〃Button 2 pressed〃);  

      int end = v。size() …1;  

      if(end 》= 0) {  

        b2。removeActionListener(  

          (ActionListener)v。elementAt(end));  

        v。removeElementAt(end);  

      }  

    }  

  }  

  public static void main(String'' args) {  

    Frame f = new DynamicEvents();  

    f。addWindowListener(  

      new WindowAdapter() {  

        public void windowClosing(WindowEvent e){  

          System。exit(0);  

        }  

      });  

    f。setSize(300;200);  

    f。show();  

  }  

} ///:~  

  

这个例子采取的新手法包括:  

(1) 在每个按钮上附着不少于一个的接收器。通常,组件把事件作为多造型处理,这意味着我们可以为单个 

事件注册许多接收器。当在特殊的组件中一个事件作为单一造型被处理时,我们会得到 

TooManyListenersException (即太多接收器异常)。  



                                                                                          432 


…………………………………………………………Page 434……………………………………………………………

(2) 程序执行期间,接收器动态地被从按钮B2 中增加和删除。增加用我们前面见到过的方法完成,但每个组 

件同样有一个removeXXXListener() (删除XXX 接收器)方法来删除各种类型的接收器。  

  

这种灵活性为我们的编程提供了更强大的能力。  

我们注意到事件接收器不能保证在命令他们被增加时可被调用(虽然事实上大部分的执行工作都是用这种方 

法完成的)。  



13。16。6 将事务逻辑与 UI 逻辑区分开  



一般而言,我们需要设计我们的类如此以至于每一类做“一件事”。当涉及用户接口代码时就更显得尤为重 

要,因为它很容易地封装“您要做什么”和“怎样显示它”。这种有效的配合防止了代码的重复使用。更不 

用说它令人满意的从GUI 中区分出我们的“事物逻辑”。使用这种方法,我们可以不仅仅更容易地重复使用 

事物逻辑,它同样可以更容易地重复使用 GUI。  

其它的争议是“动作对象”存在的完成分离机器的多层次系统。动作主要的定位规则允许所有新事件修改后 

立刻生效,并且这是如此一个引人注目的设置系统的方法。但是这些动作对象可以被在一些不同的应用程序 

使用并且因此不会被一些特殊的显示模式所约束。它们会合理地执行动作操作并且没有多余的事件。  

下面的例子演示了从GUI 代码中多么地轻松的区分事物逻辑:  

  

//: Separation。java  

// Separating GUI logic and business objects  

import java。awt。*;  

import java。awt。event。*;  

import java。applet。*;  

  

class BusinessLogic {  

  private int modifier;  

  BusinessLogic(int mod) {  

    modifier = mod;  

  }  

  public void setModifier(int mod) {  

    modifier = mod;  

  }  

  public int getModifier() {  

    return modifier;  

  }  

  // Some business operations:  

  public int calculation1(int arg) {  

    return arg * modifier;  

  }  

  public int calculation2(int arg) {  

    return arg + modifier;  

  }  

}  

  

public class Separation extends Applet {  

  TextField   

    t = new TextField(20);  

    mod = new TextField(20);  

  BusinessLogic bl = new BusinessLogic(2);  

  Button  

    calc1 = new Button(〃Calculation 1〃);  

    calc2 = new Button(〃Calculation 2〃);  

  public void init() {  



                                                                                           433 


…………………………………………………………Page 435……………………………………………………………

    add(t);  

    calc1。addActionListener(new Calc1L());  

    calc2。addActionListener(new Calc2L());  

    add(calc1); add(ca lc2);  

    mod。addTextListener(new ModL());  

    add(new Label(〃Modifier:〃));  

    add(mod);  

  }  

  static int getValue(TextField tf) {  

    try {  

  
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!