如何获取JRadioButton的文本值 [英] How to get the text value of JRadioButton
问题描述
我正在用java创建一个项目。我的程序有80个JRadioButtons ....我需要获取它们的文本值..现在这些radiobuttons被添加到ButtonGroup(每个都有4个单选按钮)...
I am creating a project in java. My Program has 80 JRadioButtons .... I need the get the text value of them.. Now these radiobuttons are added to ButtonGroup(each has 4 radio buttons)...
我知道如何通过以下代码从单选按钮获取文本值
I know how to get the text value from the radio button by this following code
radiobutton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String q1=e.getActionCommand();
JOptionPane.showMessageDialog(null, q1);
}
});
现在有什么简单的方法吗?因为我将不得不在上面的代码执行80次(如果我使用上面的方法使用上述方法,则为80个radiobuttons
Now is there any easy way to do this? because i will have to do this above code for 80 times(for eighty radiobuttons if i use the above use the above method
附加信息 - 我有20个ButtonGroups,每个4个单选按钮。所以(80个单选按钮)。
Additional Info- I have Total 20 ButtonGroups each with 4 radio buttons. So(80 radio buttons).
推荐答案
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.
}
这篇关于如何获取JRadioButton的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!