当前位置: 首页 > 面试题库 >

从ActionListener获取按钮名称?

富锦
2023-03-14
问题内容

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

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

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

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

任何帮助,不胜感激!

编辑:

这是我正在使用的代码:

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等

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

提前致谢。


问题答案:

保留按钮中的参考 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,从命令中获取按钮:

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

这段代码做的完全一样,没有第三方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...
}


 类似资料:
  • 现在,我有两个名称为和名称为的cookie。 我想让这个getter只从义务cookie中获取值。 我该怎么做?因此将数据拆分为单独的值并将其放入数组中。

  • 我的网页上的表格中有很多按钮。 每个按钮都有相同的名称,但值集不同。 当我想提取按钮值时,使用下面的按钮值不意味着我没有得到唯一的按钮值——实际单击的按钮值吗? 我怎么才能绕过它? 谢谢

  • 参考问题JavaFX TableView自定义单元格渲染拆分菜单按钮,我能够在每一行中渲染拆分菜单按钮。我已经按照James_D的建议和Keyur Bhanderi的回答更新了我的代码。这个问题是关于获取拆分菜单所在行的值,而无需在单击之前选择行。 更新:添加图像以查看输出 下面的图像显示输出,我点击的每个按钮。 更新SplitMenuCellFactory.java 更新,添加缺少的类 Spli

  • 我有一个二维数组(矩阵),尺寸为50x50。在这些矩阵中,每个位置的值为 0 或 1。此矩阵由具有 50x50 按钮的网格布局呈现,如果值为 0 或 1,则这些按钮为白色或黑色。如果我按下一个按钮,矩阵中的相关位置应该将值更改为1。为了实现这一点,我为每个矩阵位置创建了一个按钮的网格,由for循环执行。我还为此 for 循环中的每个按钮实现了一个 ActionListener。我尝试使用 Acti

  • 我到处寻找,并尽一切努力从SQLQueriesRadion按钮获取选定的mySQL值。 这是我的密码 谢啦

  • 问题内容: 我有一个多线程应用程序,并且通过属性为每个线程分配了唯一的名称。现在,我希望功能可以使用相应的名称直接访问线程。 类似于以下功能的东西: 上面的函数检查所有正在运行的线程,然后从正在运行的线程集中返回所需的线程。也许我想要的线程被中断了,那么上面的功能将无法正常工作。关于如何整合该功能的任何想法? 问题答案: 您可以使用ThreadGroup查找所有 活动 线程: 获取当前线程的组 通