当前位置: 技术问答>java相关
*** 难题, 关于触发事件, 请高手回答 ***
来源: 互联网 发布时间:2015-06-29
本文导语: 我希望使用dispatchEvent触发一个事件, 发现如果使用AWT控件就可以, 使用Swing控件就不行, 请问是为什么? 下面是代码, 我希望在点击button1的时候, 同时触发button2的Click事件, 如果button用AWT中的Button就可以...
我希望使用dispatchEvent触发一个事件, 发现如果使用AWT控件就可以, 使用Swing控件就不行, 请问是为什么?
下面是代码, 我希望在点击button1的时候, 同时触发button2的Click事件, 如果button用AWT中的Button就可以实现, 将Button换成Swing中的JButton, 就不能成功了, 请问是为什么?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
public class Test extends JFrame implements ActionListener {
private Container container = null;
private Button button1 = null;
private Button button2 = null;
public static void main(String args[]) {
Test test = new Test();
test.show();
}
public Test() {
super();
container = this.getContentPane();
button1 = new Button("1");
button1.addActionListener(this);
button2 = new Button("2");
button2.addActionListener(this);
container.add(button1, BorderLayout.NORTH);
container.add(button2, BorderLayout.SOUTH);
this.setBounds(0, 0, 400, 300);
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == button1) {
System.out.println("button 1 clicked");
button1.dispatchEvent(new ActionEvent(button2, ae.getID(), ae.getActionCommand()));
// button1.dispatchEvent(ae);
}
if (source == button2) {
System.out.println("button 2 clicked");
}
}
}
请帮助, 一定给分!
下面是代码, 我希望在点击button1的时候, 同时触发button2的Click事件, 如果button用AWT中的Button就可以实现, 将Button换成Swing中的JButton, 就不能成功了, 请问是为什么?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
public class Test extends JFrame implements ActionListener {
private Container container = null;
private Button button1 = null;
private Button button2 = null;
public static void main(String args[]) {
Test test = new Test();
test.show();
}
public Test() {
super();
container = this.getContentPane();
button1 = new Button("1");
button1.addActionListener(this);
button2 = new Button("2");
button2.addActionListener(this);
container.add(button1, BorderLayout.NORTH);
container.add(button2, BorderLayout.SOUTH);
this.setBounds(0, 0, 400, 300);
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == button1) {
System.out.println("button 1 clicked");
button1.dispatchEvent(new ActionEvent(button2, ae.getID(), ae.getActionCommand()));
// button1.dispatchEvent(ae);
}
if (source == button2) {
System.out.println("button 2 clicked");
}
}
}
请帮助, 一定给分!
|
jdk 1.4.0-b92 下没有问题。
package csdn.zte.vc.ws;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
public class MyClass11 extends JFrame implements ActionListener {
private Container container = null;
private Button button1 = null;
private Button button2 = null;
private JButton button3 = null;
private JButton button4 = null;
public static void main(String args[]) {
MyClass11 test = new MyClass11();
test.show();
}
public MyClass11() {
super();
container = this.getContentPane();
button1 = new Button("1");
button1.addActionListener(this);
button2 = new Button("2");
button2.addActionListener(this);
button3 = new JButton("3");
button3.addActionListener(this);
button4 = new JButton("4");
button4.addActionListener(this);
container.add(button1, BorderLayout.NORTH);
container.add(button2, BorderLayout.SOUTH);
container.add(button3, BorderLayout.WEST );
container.add(button4, BorderLayout.EAST );
this.setBounds(0, 0, 400, 300);
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == button1) {
System.out.println("button 1 clicked");
button1.dispatchEvent(new ActionEvent(button2, ae.getID(), ae.getActionCommand()));
// button1.dispatchEvent(ae);
}
if (source == button2) {
System.out.println("button 2 clicked");
}
if (source == button3) {
System.out.println("button 3 clicked");
button1.dispatchEvent(new ActionEvent(button4, ae.getID(), ae.getActionCommand()));
// button1.dispatchEvent(ae);
}
if (source == button4) {
System.out.println("button 4 clicked");
}
}
}
package csdn.zte.vc.ws;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
public class MyClass11 extends JFrame implements ActionListener {
private Container container = null;
private Button button1 = null;
private Button button2 = null;
private JButton button3 = null;
private JButton button4 = null;
public static void main(String args[]) {
MyClass11 test = new MyClass11();
test.show();
}
public MyClass11() {
super();
container = this.getContentPane();
button1 = new Button("1");
button1.addActionListener(this);
button2 = new Button("2");
button2.addActionListener(this);
button3 = new JButton("3");
button3.addActionListener(this);
button4 = new JButton("4");
button4.addActionListener(this);
container.add(button1, BorderLayout.NORTH);
container.add(button2, BorderLayout.SOUTH);
container.add(button3, BorderLayout.WEST );
container.add(button4, BorderLayout.EAST );
this.setBounds(0, 0, 400, 300);
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == button1) {
System.out.println("button 1 clicked");
button1.dispatchEvent(new ActionEvent(button2, ae.getID(), ae.getActionCommand()));
// button1.dispatchEvent(ae);
}
if (source == button2) {
System.out.println("button 2 clicked");
}
if (source == button3) {
System.out.println("button 3 clicked");
button1.dispatchEvent(new ActionEvent(button4, ae.getID(), ae.getActionCommand()));
// button1.dispatchEvent(ae);
}
if (source == button4) {
System.out.println("button 4 clicked");
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。