获取被点击按钮的文本,我只会以下面方式实现
JButton button = new JButton("按钮");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String text = button.getText(); // 按钮
}
});
但既然Listener是绑定到button上的,能否直接在actionPerformed中直接获取button呢?
什么叫“直接在actionPerformed中直接获取button呢”?你现在不就是直接获取的么?
e.getSource()
就是获取点击源啊,代码就这样了,没有其他招数了^^