从ActionListener获取按钮名称? [英] Get button name from ActionListener?

查看:42
本文介绍了从ActionListener获取按钮名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜寻了互联网,但找不到答案:

I have scoured the internet but can't find an answer to this :

我正在使用for循环创建36个名为a1,a2等的按钮,并同时为每个按钮分配一个唯一的操作命令.

I'm using a for loop to create 36 buttons called a1, a2, etc. and assigning each of them a unique Action Command at the same time.

稍后,我想从actionPerformed(ActionEvent e)方法中获取按钮的名称.

Later on I wanted to get the name of the button from the actionPerformed(ActionEvent e) method.

我可以使ActionCommand非常容易,但是我也需要按钮的名称.

I could get the ActionCommand easy enough, but I need the name of the button as well.

任何帮助,不胜感激!

Any help much appreciated!

这是我正在使用的代码:

Here is the code I'm using:

String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
JButton btn[] = new JButton[35];
int count = 0;


for (int f=1; f < 7;f++){

        for (int i=1; i < 7;i++){
            btn[i] = new JButton(letters[f]+i, cup);
            System.out.println(btn[i]));
            mainGameWindow.add(btn[i]);
            btn[i].addActionListener(this);
            String StringCommand = Integer.toString(randomArrayNum());
            btn[i].setActionCommand(StringCommand);
            count++;
            if(count == 18){
                generateArray();
            }

        }

}

这为6x6网格提供了36个按钮,分别位于a1-6,b1-6,c1-6等

This gives you 36 buttons for a 6x6 grid that go a1-6, b1-6, c1-6 etc

以这种方式创建按钮后,我似乎似乎无法控制按钮,无法分配图标或获得按钮的名称.

I just can't seem to control the buttons once I've created them this way, I can't assign icons or get the name of the button.

预先感谢.

推荐答案

Map

String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
JButton btn;
int count = 0;

HashMap<String,JButton> buttonCache = new HashMap<String,JButton>();


for (int f=1; f < 7;f++){

    for (int i=1; i < 7;i++){
        btn = new JButton(letters[f]+i, cup);
        mainGameWindow.add(btn[i]);
        btn.addActionListener(this);
        String stringCommand = Integer.toString(randomArrayNum());
        btn.setActionCommand(stringCommand);
        buttonMap.put(stringCommand,btn);
        count++;
        if(count == 18){
            generateArray();
        }

    }

} 

然后,在您的ActionListener中,从命令中获取按钮:

Then, in your ActionListener, get the button back from the command :

public void actionPerformed(ActionEvent e) {
    String command = ((JButton) e.getSource()).getActionCommand();
    JButton button = buttonCache.get(command);
    if (null != button) {
        // do something with the button
    }
}


编辑

五年后重新审视此答案,我不知道为什么我建议使用HashMap:P


Edit

Revisiting this answer over five years later, I have no idea why I suggested a HashMap :P

此代码执行的操作完全相同,没有第三方Map:

This code does the exact same thing, no third party Map :

String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
int count = 0;

for (int f=1; f < 7;f++){
    for (int i=1; i < 7;i++) {
        String stringCommand = Integer.toString(randomArrayNum());
        Button btn = new JButton(letters[f]+i, cup);

        btn.setActionCommand(stringCommand);
        btn.addActionListener(this);
        mainGameWindow.add(btn[i]);

        // NOTE : I have no idea what this is for...
        count++;
        if(count == 18){
            generateArray();
        }
    }
}

ActionListener ...

public void actionPerformed(ActionEvent e) {
    JButton button = (JButton) e.getSource();
    String command = button.getActionCommand();

    // do something with the button
    // the command may help identifying the button...
}

这篇关于从ActionListener获取按钮名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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