简体   繁体   English

Java事件监听器

[英]Java Event Listeners

I'm trying to separate components of my Java desktop app into different classes. 我正在尝试将Java桌面应用程序的组件分成不同的类。 For example, I have a MenuBar class, called from the MainClass, that creates the JMenuBar. 例如,我有一个从MainClass调用的MenuBar类,它创建了JMenuBar。

Normally, I would implement ActionListener in the MenuBar class and override actionPerformed() to keep everything organized. 通常,我会在MenuBar类中实现ActionListener并覆盖actionPerformed()以保持一切有条理。 But if I do that, how can I let the MainClass know what was clicked? 但是,如果我这样做,我怎么能让MainClass知道被点击的内容?

I tried implementing my own ActionListener, but I couldn't come up with a solution that was capable of dispatching events to other classes. 我尝试实现自己的ActionListener,但我无法想出一个能够将事件分派给其他类的解决方案。

MainClass.java MainClass.java

public class MainClass extends JFrame {

     private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(false);
        JFrame frame = new JFrame("Main Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MenuBar menuBar = new MenuBar();
        JMenuBar mb = menuBar.createMenu();
        frame.setJMenuBar(mb);

        frame.setSize(400,400);
        frame.setVisible(true);

     }

     public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           createAndShowGUI();
        }
      });
    }

}

MenuBar.java MenuBar.java

public class MenuBar implements ActionListener {

     public JMenuBar createMenu() {

         JMenu menu;
         JMenuItem item;

         JMenuBar menuBar = new JMenuBar();

         menu = new JMenu("Main");
         menuBar.add(menu);

         item = new JMenuItem("New");
         menu.add(item);

         return menuBar;

   }

   @Override
   public void actionPerformed(ActionEvent e) {
         JMenuItem source = (JMenuItem)(e.getSource());
         System.out.println("Action triggered on: "+source.getText());
         // *** Let MainClass know what was clicked ??
   }

}

EDIT 编辑

I realized I can just create an ActionListener in the MainClass and pass that to the MenuBar class, like this: 我意识到我可以在MainClass中创建一个ActionListener并将其传递给MenuBar类,如下所示:

Amended MainClass.java 修改后的MainClass.java

ActionListener l = new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
     System.out.println(ae.getActionCommand());
   }
};

MenuBar menuBar = new MenuBar();
frame.setJMenuBar(menuBar.createMenu(l));

And then, in MenuBar, I just apply the ActionListener to each of the menu items. 然后,在MenuBar中,我只是将ActionListener应用于每个菜单项。

Amended MenuBar.java 修改后的MenuBar.java

public JMenuBar createMenu(ActionListener l) {

    item = new JMenuItem("Hide When Minimized");
    item.addActionListener(l);
    menu.add(item);

}

One possible solution is MainClass implements ActionListener and pass its instance to MenuBar.createMenu(): 一种可能的解决方案是MainClass implements ActionListener并将其实例传递给MenuBar.createMenu():

public class MenuBar {

   public JMenuBar createMenu( ActionListener l ) {
      ...
      menuItem.addActionListener( l );
   }
   ...
}

MainClass side: MainClass方面:

public class MainClass extends JFrame {

   @Override
   public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      System.out.println( "Action triggered by: " + source );
   }

   private static void createAndShowGUI() {
      ...
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      MenuBar menuBar = new MenuBar();
      JMenuBar mb = menuBar.createMenu( frame );
      frame.setJMenuBar( mb );
      ...
   }
}

Another way is to use java.beans.PropertyChangeListener and java.beans.PropertyChangeSupport 另一种方法是使用java.beans.PropertyChangeListenerjava.beans.PropertyChangeSupport

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM