我怎样才能知道点击了哪个按钮? [英] How can I find out which button was clicked?

查看:40
本文介绍了我怎样才能知道点击了哪个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的按钮工作正常,我是每个按钮的听众,如下所示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆