首页 文章

来自另一个类的JButton Action Listener

提问于
浏览
0

我正在使用JButton事件 . 我有一个JPanel类,让我们调用Panel1,包含一个公共JButton,我们称之为Button1 . 单击此按钮时:

//Inside Panel1
Button1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
      System.out.println("1")
   }
});

从另一个JPanel类,我们称之为Panel2,包含Panel1,我必须处理事件“Button1 Pressed” .

//Inside Panel2
Panel1.Button1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
      System.out.println("2")
   }
});

获得的结果是:

2
1

但我有兴趣:

1
2

有什么建议吗?

1 回答

  • 1

    如果将ActionListeners添加到JButton,则无法保证它们将触发的顺序,并且知道添加顺序无法保证可以提供帮助 . 解决此问题的一种方法是使用ActionListener来更改对象的状态,然后监听它 . 这将保证ActionListener首先触发 .

    例如,使用PropertyChangeListener作为第二个侦听器:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    import javax.swing.*;
    
    public class ActionOrder extends JPanel {
       ButtonPanel buttonPanel = new ButtonPanel();
       OtherPanel otherPanel = new OtherPanel();
    
       public ActionOrder() {
          add(buttonPanel);
          add(otherPanel);
    
          buttonPanel.addPropertyChangeListener(ButtonPanel.PRESSED, new PropertyChangeListener() {
    
             @Override
             public void propertyChange(PropertyChangeEvent evt) {
                otherPanel.appendText("Button 1 Pressed");
             }
          });
       }
    
       private static void createAndShowGui() {
          ActionOrder mainPanel = new ActionOrder();
    
          JFrame frame = new JFrame("ActionOrder");
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    
    class ButtonPanel extends JPanel {
       public static final String PRESSED = "pressed";
       private JButton button1 = new JButton("Button 1");
    
       public ButtonPanel() {
          add(button1);
          button1.addActionListener(new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent e) {
                System.out.println("1");
                firePropertyChange(PRESSED, null, PRESSED);
             }
          });
    
          setBorder(BorderFactory.createTitledBorder("Button Panel"));
       }
    }
    
    class OtherPanel extends JPanel {
       private JTextArea textArea = new JTextArea(10, 20);
    
       public OtherPanel() {
          add(new JScrollPane(textArea));
          setBorder(BorderFactory.createTitledBorder("Other Panel"));
       }
    
       public void appendText(String text) {
          textArea.append(text + "\n");
          System.out.println("2");
          System.out.println();
       }
    }
    

相关问题