简体   繁体   English

从按钮组中获取所选单选按钮的名称

[英]get the name of the selected radio button from button group

I want to return selected radio button name from button group in java : 我想从java中的按钮组返回选定的单选按钮名称:

ButtonGroup bg = new ButtonGroup();
bg.add(radiobutton1);
bg.add(radiobutton2);
bg.add(radiobutton3);

I think you can loop through the radio buttons, for each button if isSelected() is true then call button.getText() to get the name. 我想你可以遍历单选按钮,如果isSelected()为true,则为每个按钮循环,然后调用button.getText()来获取名称。

for (AbstractButton button : bg.getElements())
    if (button.isSelected())
        return button.getText();

Why do you want the button name? 为什么要按钮名称? It isn't of much use. 它用处不大。 You can, however, get the all of the button references using bg.getElements() , and you can get the text of each of those references with button.getText() , both of which are of much more use than the name you gave the button. 你可以,但是,使用获得的所有按钮引用bg.getElements()你可以得到每一个与引用的文本button.getText()这两者都是比你给的名字更多的使用按钮。 If you REALLY want the name, you can create the buttons and then call radiobutton1.setName("radiobutton1") and then use getName() on the reference. 如果您真的想要名称,可以创建按钮,然后调用radiobutton1.setName("radiobutton1") ,然后在引用上使用getName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM