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

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

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




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



                                                                                          424 


…………………………………………………………Page 426……………………………………………………………

    CMIL cmil = new CMIL();  

    safety'0'。setActionmand(〃Guard〃);  

    safety'0'。addItemListener(cmil);  

    safety'1'。setActionmand(〃Hide〃);  

    safety'1'。addItemListener(cmil);  

    file'0'。setActionmand(〃Open〃);  

    file'0'。addActionListener(ml);  

    file'1'。setActionmand(〃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 actionmand =   



                                                                                        425 


…………………………………………………………Page 427……………………………………………………………

        target。getActionmand();  

      if(actionmand。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(actionmand。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 actionmand =   

        target。getActionmand();  

      if(actionmand。equals(〃Guard〃))  

        t。setText(〃Guard the Ice Cream! 〃 +  

          〃Guarding is 〃 + target。getState());  

      else if(actionmand。equals(〃Hide〃))  

        t。setText(〃Hide the Ice Cream! 〃 +  



                                                                                        426 


…………………………………………………………Page 428……………………………………………………………

          〃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 ”  

菜单项中。  

我们同样会注意 setActionmand()的使用。这看似一点陌生因为在各种情况下“action mand”完全同 

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