java 多个单选按钮的动作侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17653116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:32:42  来源:igfitidea点击:

Action listener for multiple radio buttons

javaswingnetbeansactionlistenerjradiobutton

提问by MD Yousuf Azad

I intend to write a program where I will give the user a choice to choose from a 8*8 matrix. Because my reputation is below 10 I can not include an image but rest assured its just a normal 8*8 matrix. I plan to visualize it in my Java program with 8*8=64 radio buttons. the user can choose only one radio button at a time so it means all 64 buttons will be of the same button group.

我打算编写一个程序,让用户可以从 8*8 矩阵中进行选择。因为我的声誉低于 10,所以我不能包含图像,但请放心,它只是一个普通的 8*8 矩阵。我计划在我的 Java 程序中使用 8*8=64 单选按钮将其可视化。用户一次只能选择一个单选按钮,这意味着所有 64 个按钮都属于同一个按钮组。

Now, how can I manage the action listeners? it is impossible (really tiresome and boring) to set up 64 individual action listener for each of the 64 radio buttons. since all 64 radio buttons are in the same button group, is there any way I can set up only oneevent listener to check which button is selected?

现在,我如何管理动作监听器?为 64 个单选按钮中的每一个设置 64 个单独的动作侦听器是不可能的(真的很累很无聊)。由于所有 64 个单选按钮都在同一个按钮组中,有什么办法可以设置一个事件侦听器来检查选择了哪个按钮?

If any of my given info is unclear then please let me know :)

如果我提供的任何信息不清楚,请告诉我:)

PS: I am using Netbeans design tools

PS:我正在使用 Netbeans 设计工具

回答by vels4j

Create two dimensional JRadioButtonarray like

创建二维JRadioButton数组,如

        JRadioButton[][] jRadioButtons = new JRadioButton[8][];
        ButtonGroup bg = new ButtonGroup();
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(8, 8));
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                JRadioButton btn = new JRadioButton();
                btn.addActionListener(listener);
                btn.setName("Btn[" + i + "," + j + "]");
                bg.add(btn);
                panel.add(btn);
                // can be used for other operations
                jRadioButtons[i][j] = btn;
            }
        }

Here is single ActionListenerfor all JRadioButtons

这是ActionListener所有 JRadioButtons 的单曲

    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JRadioButton btn = (JRadioButton) e.getSource();
            System.out.println("Selected Button = " + btn.getName());
        }
    };

回答by kiheru

The action listener gets passed an ActionEvent. You can make one listener, bind it to all the buttons, and check the event source with getSource():

动作侦听器被传递一个 ActionEvent。您可以创建一个侦听器,将其绑定到所有按钮,并使用以下命令检查事件源getSource()

void actionPerformed(ActionEvent e) {
   Object source = e.getSource();
   ...
}

回答by Marcel H?ll

I think you are implementing the radio buttons like this:

我认为您正在实现这样的单选按钮:

JRadioButton radioButton = new JRadioButton("TEST");

If you are doing like this you have to set a ActionListener to each of your buttons (for example initialize and set the ActionListener in a for-loop) with this statement:

如果您这样做,您必须使用以下语句为每个按钮设置一个 ActionListener(例如在 for 循环中初始化和设置 ActionListener):

radioButton.addActionListener(this)(if you implement ActionListener in the same class)

radioButton.addActionListener(this)(如果你在同一个类中实现 ActionListener )

At last you can go to your actionPerformed(ActionEvent e)method and get the source with e.getSourceand then do something like if else to get the right RadioButton:

最后,您可以转到您的actionPerformed(ActionEvent e)方法并获取源代码,e.getSource然后执行 if else 之类的操作以获得正确的 RadioButton:

if(e.getSource == radioButton1)
{
  // Action for RadioButton 1
}
else if(e.getSource == radioButton2)
{
  // Action for RadioButton 2
}
...