如何取得触发发生者对象 Object event.getSource()可以判断是从哪里来得 event 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 同意楼上~a = event.getSource();if(a instanceof Button)... String getActionCommand()也可以。不过要先用 setActionCommand()设置好。 你的actionlistener中的方法比如public void acitonPerformed(ActionEvent e){ if(e.getActionCommand ==""){ }}通过ActionEvent来获得事件源下面是一个例子import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Frame1 extends JFrame implements ActionListener { JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); public Frame1() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Frame1 frame1 = new Frame1(); frame1.setSize(800,600); frame1.setVisible(true); } private void jbInit() throws Exception { this.getContentPane().setLayout(null); jButton2.addActionListener(this); jButton2.setText("jButton2"); jButton2.setBounds(new Rectangle(177, 135, 56, 21)); jButton3.setText("jButton3"); jButton3.addActionListener(this); jButton3.setBounds(new Rectangle(257, 140, 54, 23)); this.getContentPane().add(jButton2, null); this.getContentPane().add(jButton3, null); } public void actionPerformed(ActionEvent e){ System.out.println(e.getSource()); System.out.println(e.getActionCommand()); }} java 运维 为什么我的这段代码没有输出结果啊 IO删除文本和替换文本名字问题 求一个正则表达式 不能匹配标识符 求编写程序切两个数的最大公约数和最小公倍数 那位能将linux c++程序转换成java程序 怎样设置JTree的某个节点为选中状态? java中调用javacbianyi命令 用过struts的朋友见过这种怪事吗? 在java里如何调用DLL 求救,我怎么在菜单事件(主窗口)取得子窗口的信息(ID、名称)和子窗口上控件的的信息(ID、名称) 一个线程的简单问题
a = event.getSource();
if(a instanceof Button)
...
public void acitonPerformed(ActionEvent e){
if(e.getActionCommand ==""){
}
}
通过ActionEvent来获得事件源
下面是一个例子
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Frame1 extends JFrame implements ActionListener {
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
public Frame1() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(800,600);
frame1.setVisible(true);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jButton2.addActionListener(this);
jButton2.setText("jButton2");
jButton2.setBounds(new Rectangle(177, 135, 56, 21));
jButton3.setText("jButton3");
jButton3.addActionListener(this);
jButton3.setBounds(new Rectangle(257, 140, 54, 23));
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton3, null);
} public void actionPerformed(ActionEvent e){
System.out.println(e.getSource());
System.out.println(e.getActionCommand());
}
}