如果我将{action}事件发送给JButton
,则不会调用该按钮的processEvent
方法。
在下面的示例中,我认为将执行以下步骤
b1
将活动发送至b2
(dispatchEvent
被调用)b2
处理事件(processEvent
被调用)b2
执行其动作侦听器但是第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);
}
}
答案 0 :(得分:0)
您可能希望发送按钮'gotoDate'
:
MouseEvent