java中如何确定是哪个对象发出事件 假设有两个按钮,它们的单击事件都由同一个函数来响应处理,那么在这个函数中如何知道是哪个按钮发出的单击事件? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JButton button = (JButton)e.getSource(); e.getSource().equals(button1); public void actionPerformed(ActionEvent e) { String strComm = e.getActionCommand();//得到触发这个事件的控件Name //下面自己equals } 请问YidingHe: 那个e是什么对象,需要事先定义吗?谢谢!请问ty_tarena_pger:(1) public void actionPerformed(ActionEvent e)是一个固定的必须这样命名的函数吗?它是什么函数? (2)必须在public void actionPerformed(ActionEvent e) 这个函数中使用 e.getActionCommand()才能得到触发这个事件的控件Name吗?在其他的自定义的函数内可以用吗?谢谢! e 是 ActionEvent 类型, 这个 e 相当于一个包含事件源的对象,并以之作为参数传递给函数。 在继承监听后重写一下方法时判断public void actionPerformed(ActionEvent e){ String comm = e.getActionCommand();//获得所按下按钮的名字 判断是那个按钮名 if("按钮1名".equals(comm);){ }else{ } } 下面是一个例子:import javax.swing.*;import java.awt.HeadlessException;import java.awt.BorderLayout;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;/** * 如何获取事件的触发控件:Java 管这叫来源(source) */public class TwoButtons extends JFrame { private TwoButtons thisWindow = this; // 事件侦听器。重点在这里。 private ActionListener buttonListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton button = (JButton) e.getSource(); thisWindow.setTitle("你刚才按下了: " + button.getText()); } } }; // 构造方法 public TwoButtons() throws HeadlessException { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(300, 60); this.setLocation(300, 200); this.add(createButton("这是第一个按钮"), BorderLayout.WEST); this.add(createButton("这是第二个按钮"), BorderLayout.EAST); } /** * 创建一个按钮 * * @param text 按钮文字 * * @return 创建好的按钮 */ private JButton createButton(String text) { JButton button = new JButton(text); button.addActionListener(buttonListener); return button; } // 程序入口 public static void main(String[] args) { new TwoButtons().setVisible(true); }} 支持楼上的代码,学习学习! // 事件侦听器。重点在这里。 private ActionListener buttonListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton button = (JButton) e.getSource(); thisWindow.setTitle("你刚才按下了: " + button.getText()); } } }; 求一个文本数据转化为数据库文件的桌面小DEMO Java文件编码 为什么不能使用呢 简单工厂模式可以解释一下吗? Thinking in Java 请问如何设置树中一个结点的图标? 在VJ++6.0中用Applet小程序与用Form Designer设计出的程序有什么优点? 简单问题,关于输入输出 分不是问题,先放50,过来看看 求求大家了,哪位对tree了如指掌的高手帮帮忙吧,说啥也拖不过去了 java抽象类是什么个情况 很简单的问题
public void actionPerformed(ActionEvent e) {
String strComm = e.getActionCommand();//得到触发这个事件的控件Name
//下面自己equals
}
那个e是什么对象,需要事先定义吗?谢谢!
请问ty_tarena_pger:
(1) public void actionPerformed(ActionEvent e)是一个固定的必须这样命名的函数吗?它是什么函数?
(2)必须在public void actionPerformed(ActionEvent e) 这个函数中使用 e.getActionCommand()才能得到触发这个事件的控件Name吗?在其他的自定义的函数内可以用吗?谢谢!
e 是 ActionEvent 类型, 这个 e 相当于一个包含事件源的对象,并以之作为参数传递给函数。
public void actionPerformed(ActionEvent e){
String comm = e.getActionCommand();//获得所按下按钮的名字
判断是那个按钮名
if("按钮1名".equals(comm);){
}else{ }
}
import javax.swing.*;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;/**
* 如何获取事件的触发控件:Java 管这叫来源(source)
*/
public class TwoButtons extends JFrame { private TwoButtons thisWindow = this; // 事件侦听器。重点在这里。
private ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
JButton button = (JButton) e.getSource();
thisWindow.setTitle("你刚才按下了: " + button.getText());
}
}
}; // 构造方法
public TwoButtons() throws HeadlessException {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 60);
this.setLocation(300, 200); this.add(createButton("这是第一个按钮"), BorderLayout.WEST);
this.add(createButton("这是第二个按钮"), BorderLayout.EAST);
} /**
* 创建一个按钮
*
* @param text 按钮文字
*
* @return 创建好的按钮
*/
private JButton createButton(String text) {
JButton button = new JButton(text);
button.addActionListener(buttonListener);
return button;
} // 程序入口
public static void main(String[] args) {
new TwoButtons().setVisible(true);
}
}
private ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
JButton button = (JButton) e.getSource();
thisWindow.setTitle("你刚才按下了: " + button.getText());
}
}
};