如何获取JRadioButton的文本值

2023-12-02

我正在用java创建一个项目。我的程序有 80 个 JRadioButtons ....我需要获取它们的文本值..现在这些单选按钮已添加到 ButtonGroup(每个单选按钮有 4 个单选按钮)...

我知道如何通过以下代码从单选按钮获取文本值

radiobutton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String q1=e.getActionCommand();
                JOptionPane.showMessageDialog(null, q1);
            }
        });

现在有什么简单的方法可以做到这一点吗?因为我必须将上面的代码执行 80 次(对于八十个单选按钮,如果我使用上面的方法,请使用上面的方法

附加信息 - 我总共有 20 个按钮组,每个按钮组有 4 个单选按钮。所以(80 个单选按钮)。


I have Total 20 ButtonGroups each with 4 radio buttons. So(80 radio buttons).

那么最简单的方法是

String actionCommand = "";
ButtonModel buttonModel = myButtonGroup.getSelection();
if (buttonModel != null) {
   actionCommand = buttonModel.getActionCommand();
} else {
   // buttonModel is null.
   // this occurs if none of the radio buttons 
   // watched by the ButtonGroup have been selected.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取JRadioButton的文本值 的相关文章

随机推荐