在一个按钮中同时添加了两个ActionListener对象会怎样 rt,两个监听器对象的actionPorfermed方法都会被调用吗?被调用的顺序是未知的吗?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用代码试了下,发现调用的是第一个监听器,不知道是否具有通用性。看来添加多个监听器是没用的吧。下面是我用的测试代码:import java.awt.*;import javax.swing.*;import java.awt.event.*;public class TestButtonMultiListener extends JPanel{ JButton jb=new JButton("点击"); String message=""; public TestButtonMultiListener(){ jb.addActionListener(new MyActionListener1()); jb.addActionListener(new MyActionListener2()); add(jb); } public void paintComponent(Graphics g){ g.drawString(message, 100, 100); } public static void main(String args[]){ JFrame jf=new JFrame(); jf.getContentPane().add(new TestButtonMultiListener()); jf.setSize(300,200); jf.setVisible(true); } class MyActionListener1 implements ActionListener{ public void actionPerformed(ActionEvent e){ message="第一个监听器"; repaint(); } } class MyActionListener2 implements ActionListener{ public void actionPerformed(ActionEvent e){ message="第二个监听器"; repaint(); } }} 可以调用两个监听器的,楼主这么写,后一次监听器方法覆盖了前面的监听器结果,修改一下就看到效果了import java.awt.*;import javax.swing.*;import java.awt.event.*;public class TestButtonMultiListener extends JPanel{ JButton jb=new JButton("点击"); String message=""; public TestButtonMultiListener(){ jb.addActionListener(new MyActionListener1()); jb.addActionListener(new MyActionListener2()); add(jb); } public void paintComponent(Graphics g){ g.drawString(message, 100, 100); } public static void main(String args[]){ JFrame jf=new JFrame(); jf.getContentPane().add(new TestButtonMultiListener()); jf.setSize(300,200); jf.setVisible(true); } class MyActionListener1 implements ActionListener{ public void actionPerformed(ActionEvent e){ message="第一个监听器"; repaint(); System.out.println("first");//添加的代码 } } class MyActionListener2 implements ActionListener{ public void actionPerformed(ActionEvent e){ message="第二个监听器"; repaint(); System.out.println("second");//添加的代码 } }} 为一个组件添加两个ActionListener监听器,这样可以吗?class FirstListener implments ActionListenerclass SecondListener implments ActionListenerpublic class MultiListener{ private Frame f = new Frame("测试"); private TextArea ta = new TextArea(6 , 40); private Button b1 = new Button("按钮一"); public void init() { FirstListener fl = new FirstListener(); b1.addActionListener(fl); b1.addActionListener(new SecondListener()); f.add(ta); Panel p = new Panel(); p.add(b1); f.add(p, BorderLayout.SOUTH); f.pack(); f.setVisible(true); } class FirstListener implements ActionListener { public void actionPerformed(ActionEvent e) { ta.append("第一个事件监听器被触发,事件源是:" +e.getActionCommand() + "\n"); } } class SecondListener implements ActionListener { public void actionPerformed(ActionEvent e) { ta.append("单击了“" + e.getActionCommand() + "”按钮\n"); } } public static void main(String[] args) { new MultiListener().init(); }} 按照6楼添加两行代码后,运行的结果是:窗体上显示:(好象只执行了第一个监听器)第一个监听器可是控制台显示:(好象先执行了第二个监听器,然后执行了第一个监听器):secondfirst按照7楼代码运行结果是:(好象先执行了第一个监听器,然后执行了第二个监听器)第一个事件监听器被触发,事件源是:按钮一单击了“按钮一”按钮我越来越糊涂了,请各位老师讲讲这是怎么回事呀 呵呵,你把这两行的位置换下,再看看结果b1.addActionListener(fl);b1.addActionListener(new SecondListener()); 怎样让一个输入框只能输入数字? (新手)关于StringBuffer出错问题 请问社区里的押宝游戏是怎么给分的 ? tomcat启动时的错误提示 ssh程序 关于org.w3c.dom写入XML文件中字符转译的问题之"&"符号~~~~~~~~~~ java绘图小程序 snmp中的target.loadMibs()参数怎么设置 关于ORACLE存储过程返回可滚动游标的问题,急球相关解决思路? 用Comparable接口实现编程------求救!!! 关于java的抽象类? 关于import后同名的类 PriorityQueue和interface的简单问题
下面是我用的测试代码:import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class TestButtonMultiListener extends JPanel
{
JButton jb=new JButton("点击");
String message="";
public TestButtonMultiListener(){
jb.addActionListener(new MyActionListener1());
jb.addActionListener(new MyActionListener2());
add(jb);
}
public void paintComponent(Graphics g){
g.drawString(message, 100, 100);
} public static void main(String args[]){
JFrame jf=new JFrame();
jf.getContentPane().add(new TestButtonMultiListener());
jf.setSize(300,200);
jf.setVisible(true);
} class MyActionListener1 implements ActionListener{
public void actionPerformed(ActionEvent e){
message="第一个监听器";
repaint();
}
}
class MyActionListener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
message="第二个监听器";
repaint();
}
}
}
可以调用两个监听器的,楼主这么写,后一次监听器方法覆盖了前面的监听器结果,修改一下就看到效果了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class TestButtonMultiListener extends JPanel
{
JButton jb=new JButton("点击");
String message="";
public TestButtonMultiListener(){
jb.addActionListener(new MyActionListener1());
jb.addActionListener(new MyActionListener2());
add(jb);
}
public void paintComponent(Graphics g){
g.drawString(message, 100, 100);
} public static void main(String args[]){
JFrame jf=new JFrame();
jf.getContentPane().add(new TestButtonMultiListener());
jf.setSize(300,200);
jf.setVisible(true);
} class MyActionListener1 implements ActionListener{
public void actionPerformed(ActionEvent e){
message="第一个监听器";
repaint();
System.out.println("first");//添加的代码
}
}
class MyActionListener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
message="第二个监听器";
repaint();
System.out.println("second");//添加的代码
}
}
}
class FirstListener implments ActionListener
class SecondListener implments ActionListenerpublic class MultiListener
{
private Frame f = new Frame("测试");
private TextArea ta = new TextArea(6 , 40);
private Button b1 = new Button("按钮一");
public void init()
{
FirstListener fl = new FirstListener();
b1.addActionListener(fl);
b1.addActionListener(new SecondListener());
f.add(ta);
Panel p = new Panel();
p.add(b1);
f.add(p, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
class FirstListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ta.append("第一个事件监听器被触发,事件源是:" +e.getActionCommand() + "\n");
}
}
class SecondListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ta.append("单击了“" + e.getActionCommand() + "”按钮\n");
}
}
public static void main(String[] args)
{
new MultiListener().init();
}
}
窗体上显示:(好象只执行了第一个监听器)
第一个监听器
可是控制台显示:(好象先执行了第二个监听器,然后执行了第一个监听器):
second
first按照7楼代码运行结果是:(好象先执行了第一个监听器,然后执行了第二个监听器)
第一个事件监听器被触发,事件源是:按钮一
单击了“按钮一”按钮我越来越糊涂了,请各位老师讲讲这是怎么回事呀
b1.addActionListener(new SecondListener());