我怎样才能找出哪个按钮被点击? [英] How can I find out which button was clicked?
本文介绍了我怎样才能找出哪个按钮被点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的按钮工作的权利,我就是一个监听器,这样每个按钮:
I've got my buttons working right, and I'm a listener to each button like this:
for(int i = 0; i <= 25; ++i) {
buttons[i] = new Button(Character.toString(letters[i]));
buttons[i].addActionListener(actionListener);
panel1.add(buttons[i]);
}
在这里,你可以看到监听器被调用,我想找出我点击了哪个按钮。有没有办法做到这一点?
Here as you can see the listener is called, and I want to find out which button I'm clicking. Is there a way to do that?
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getSource());
}
};
我需要一些方法来找到阵列中的按钮。
I need some way to find the button in the array.
推荐答案
试试这个
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getActionCommand());
}
};
这篇关于我怎样才能找出哪个按钮被点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文