简体   繁体   English

在按钮组中选择了哪个单选按钮

[英]Which radio button is selected in a button group

What can I do to get which radiobutton is selected on a buttongroup without doing this: 我如何做才能在不执行此操作的情况下在按钮组上选择哪个单选按钮:

if (jRadioButton1.isSelected()) {
    //...
}

if (jRadioButton2.isSelected()) {
    //...
}

if (jRadioButton3.isSelected()) {
    //...
}

if (jRadioButton4.isSelected()) {
    //...
}

You can get the ButtonModel for the selected button via the getSelection () method of ButtonGroup. 您可以通过ButtonGroup的getSelection ()方法获取所选按钮的ButtonModel。 I don't know how you can avoid conditionally branching on the selected button though, unless you have some sort of ancillary data structure mapping from ButtonModel to actions to perform, for instance. 我不知道如何避免有条件地在选定按钮上分支,除非您具有从ButtonModel到执行动作的某种辅助数据结构映射。 If you had that, then you could just fire the action based on the returned ButtonModel. 如果有的话,您可以根据返回的ButtonModel触发操作。

I know the question was posted long back. 我知道这个问题早已发布。 Anyway, we can use the setActioncommand function. 无论如何,我们可以使用setActioncommand函数。 while creating the radio button, setActionCommand could be invoked to set the action command value, which could be used to refer to the radio button that was selected. 在创建单选按钮时,可以调用setActionCommand来设置操作命令值,该值可以用来引用所选的单选按钮。

jRadioButton1.setActionCommand("jRadioButton1"); jRadioButton2.setActionCommand("jRadioButton2") . . String button_name = ((JToggleButton.ToggleButtonModel)button_group.getSelection()).getActionCommand();

Darryl的“ 选择按钮组”具有getSelectedButton()方法。

For dealing with a button group bg, you can get the buttons by calling the button group's getElements() method, and using that as the parameter for the Collections.list() method, just save the result in an arraylist. 为了处理按钮组bg,可以通过调用按钮组的getElements()方法并将其用作Collections.list()方法的参数来获取按钮,只需将结果保存在arraylist中即可。 From there it is relatively simple to retrieve the correct button. 从那里检索相对正确的按钮相对简单。

ArrayList<AbstractButton> arl = Collections.list(bg.getElements());
for (AbstractButton ab : arl) {
    JRadioButton jrb = (JRadioButton) ab;
    if (jrb.isSelected()) {
        return jrb;
    }
}

ButtonGroup class does not provide a method to identify the currently selected button (inherited from AbstractButton) in the group if that is your intention. ButtonGroup类没有提供一种方法来标识组中当前选择的按钮(从AbstractButton继承)。 It only has clearSelection() method to clear the selected state of all buttons in the group (with exception for JButton and JMenuItem which don't have select/deselect button state). 它仅具有clearSelection()方法来清除组中所有按钮的选定状态(不具有选择/取消选择按钮状态的JButton和JMenuItem除外)。

One solution I can think of is to use a special variable or field (AbstractButton, JRadioButton or JRadioButtonMenuItem if it is in a menu item) to identify which one is currently selected by updating it inside each AbstractButton's action listener method (make sure to validate user clicks since it can be triggered more than once!). 我能想到的一种解决方案是使用一个特殊的变量或字段(如果在菜单项中,则使用AbstractButton,JRadioButton或JRadioButtonMenuItem),以通过在每个AbstractButton的动作侦听器方法中更新它来确定当前选择了哪个(确保对用户进行验证)点击,因为它可以被多次触发!)。 Use the variable (by typecasting it - for AbstractButton only) in other method(s). 在其他方法中使用变量(通过类型转换-仅用于AbstractButton)。

Other than that, nope...you will need to do conditional branching. 除此之外,不,...您将需要进行条件分支。

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

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