调度事件后不调用processEvent

时间:2017-01-16 07:54:07

标签: java swing events jbutton

如果我将{action}事件发送给JButton,则不会调用该按钮的processEvent方法。

在下面的示例中,我认为将执行以下步骤

  1. b1将活动发送至b2dispatchEvent被调用)
  2. b2处理事件(processEvent被调用)
  3. b2执行其动作侦听器
  4. 但是第2步和第3步不会被执行。任何人都可以解释原因吗?

    import java.awt.AWTEvent;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JavaApplication {
    
      public static void main(String[] args) {
        final JFrame frame = new JFrame();
        final JPanel panel = new JPanel();
        final JButton b2 = new JButton("b2") {
          @Override
          public void processEvent(AWTEvent evt) {
            System.out.println("action event will be processed" + evt.toString());
            super.processEvent(evt);
          }
        };
    
        b2.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            System.out.println("b2 clicked");
          }
        });
    
        final JButton b1 = new JButton("b1");
    
        b1.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            System.out.println("b1 clicked");
            final ActionEvent e2 = new ActionEvent(e.getSource(), e.getID(), "Redispatch-Event");
            b2.dispatchEvent(e2);
    
          }
        });
    
        panel.add(b1);
        panel.add(b2);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
      }
    }
    

1 个答案:

答案 0 :(得分:0)

您可能希望发送按钮'gotoDate'

MouseEvent