日期:2014-05-20  浏览次数:20805 次

SWING JBUTTON 监听
有两个按钮 不添加TEXT的属性(因为我要在按钮上添加图片,设置了setIcon属性,并且不希望JBUTTON上显示文字)
 JButton jb1 = new JButton()
 JButton jb2 = new JButton()
实现了一个按钮监听器
package com.ms.listener.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;


public class MainBtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JButton){
String command = e.getActionCommand();
System.out.println(command);
}
}
}

我在添加了JB1和JB2的JPanel中添加了对这两个按钮的监听,现在的问题是,两个按钮取到的command都为空。

在一个监听器的情况下,能不能区分这两个按钮的值,能的话,怎么区分?望高手指教。。

------解决方案--------------------
String command = e.getActionCommand();获取的是你创建Button的时候传的值。或者是用JB1.setActionCommand("OneTest");方法设置的值


在一个监听器的情况下,能不能区分这两个按钮的值,能的话,怎么区分?
能,比如

JB1.setActionCommand("JB1");
JB2.setActionCommand("JB2");

不知道能否解决你的问题!