从ActionListener获取按钮名称?

Jam*_* MV 6 java swing jbutton

我在互联网上搜索过但无法找到答案:

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

后来我想从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();
            }

        }

}
Run Code Online (Sandbox Code Playgroud)

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

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

提前致谢.

小智 16

JButton btnClear = new JButton("clear");
btnClear.addActionListener(this);
btnClear.setName("clear");

//..............
//..............

public void actionPerformed(ActionEvent e) {
   JButton o = (JButton)e.getSource();
   String name = o.getName();
   if (name == "clear")
   {
       euroMillText.setText("");
   }
   else if (name == "eumill")
   {
       getLottoNumbers();
   }
   //JOptionPane.showMessageDialog(null,name);      
}   
Run Code Online (Sandbox Code Playgroud)

  • 是不是='字符串'坏'?(.等于) ? (4认同)

Yan*_*hon 10

保留一个按钮的参考 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();
        }

    }

} 
Run Code Online (Sandbox Code Playgroud)

然后,在您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
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

五年后重新回答这个答案,我不知道为什么我建议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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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...
}
Run Code Online (Sandbox Code Playgroud)


Sea*_*oyd 8

String buttonText = ((JButton) e.getSource()).getText()
Run Code Online (Sandbox Code Playgroud)