代码之家  ›  专栏  ›  技术社区  ›  Joel

如何从按钮组中选择jradiobutton

  •  64
  • Joel  · 技术社区  · 16 年前

    我有一个Swing应用程序,在窗体上包含单选按钮。我有 ButtonGroup 但是,在查看可用的方法时,我似乎无法获取所选的 JRadioButton . 到目前为止,我可以说:

    • 从ButtonGroup,我可以执行 getSelection() 归还 ButtonModel . 从那里,我可以表演 getActionCommand 但这似乎并不总是奏效。我尝试了不同的测试,结果不可预知。

    • 也从 钮扣群 ,我可以从 getElements() . 但是,我必须循环浏览每个按钮,以检查它是否是选中的按钮。

    有没有更容易的方法来找出哪个按钮被选中?我在Java1.3.1和Swing中编程。

    12 回复  |  直到 8 年前
        1
  •  39
  •   Alex Weitz Shailendra Singh    8 年前

    我只要把你的 JRadioButtons 并打电话 isSelected() . 如果你真的想从 ButtonGroup 你只能去看模特儿。您可以将模型与按钮匹配,但是如果您可以访问这些按钮,为什么不直接使用它们呢?

        2
  •  68
  •   Rendicahya    12 年前

    我也遇到了类似的问题,用这个解决了:

    import java.util.Enumeration;
    import javax.swing.AbstractButton;
    import javax.swing.ButtonGroup;
    
    public class GroupButtonUtils {
    
        public String getSelectedButtonText(ButtonGroup buttonGroup) {
            for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
                AbstractButton button = buttons.nextElement();
    
                if (button.isSelected()) {
                    return button.getText();
                }
            }
    
            return null;
        }
    }
    

    它返回所选按钮的文本。

        3
  •  25
  •   Alex Weitz Shailendra Singh    8 年前

    你必须增加 setActionCommand JRadioButton 那么就这样做:

    String entree = entreeGroup.getSelection().getActionCommand();
    

    例子:

    java = new JRadioButton("Java");
    java.setActionCommand("Java");
    c = new JRadioButton("C/C++");
    c.setActionCommand("c");
    System.out.println("Selected Radio Button: " + 
                        buttonGroup.getSelection().getActionCommand());
    
        4
  •  4
  •   Tom Hawtin - tackline    16 年前

    我建议在Swing中直接使用模型方法。在将组件放入面板和布局管理器之后,甚至不需要保留对它的特定引用。

    如果您真的想要这个小部件,那么您可以用 isSelected 或维持 Map<ButtonModel,JRadioButton> .

        5
  •  4
  •   Ale Rojas    8 年前

    您可以将和actioncommand放入每个单选按钮(字符串)。

    this.jButton1.setActionCommand("dog");
    this.jButton2.setActionCommand("cat");
    this.jButton3.setActionCommand("bird");
    

    假设它们已经在button group(本例中为state_group)中,您可以这样获得所选的单选按钮:

    String selection = this.state_group.getSelection().getActionCommand();
    

    希望这有帮助

        6
  •  2
  •   Akki    12 年前

    以下代码显示 jradiobutton从buttonGroup中选择 单击按钮。
    它是通过循环访问特定按钮组中的所有jradiobuttons来完成的。

     JRadioButton firstRadioButton=new JRadioButton("Female",true);  
     JRadioButton secondRadioButton=new JRadioButton("Male");  
    
     //Create a radio button group using ButtonGroup  
     ButtonGroup btngroup=new ButtonGroup();  
    
     btngroup.add(firstRadioButton);  
     btngroup.add(secondRadioButton);  
    
     //Create a button with text ( What i select )  
     JButton button=new JButton("What i select");  
    
     //Add action listener to created button  
     button.addActionListener(this);  
    
     //Get selected JRadioButton from ButtonGroup  
      public void actionPerformed(ActionEvent event)  
      {  
         if(event.getSource()==button)  
         {  
            Enumeration<AbstractButton> allRadioButton=btngroup.getElements();  
            while(allRadioButton.hasMoreElements())  
            {  
               JRadioButton temp=(JRadioButton)allRadioButton.nextElement();  
               if(temp.isSelected())  
               {  
                JOptionPane.showMessageDialog(null,"You select : "+temp.getText());  
               }  
            }            
         }
      }
    
        7
  •  0
  •   Daniel Rikowski    16 年前

    您可以使用itemselevable(buttonmodel的superinterface)的getSelectedObjects(),它返回所选项目的列表。对于单选按钮组,它只能是一个或一个都不能。

        8
  •  0
  •   Spoonaroony    11 年前

    将单选按钮添加到按钮组,然后:

    buttonGroup.getSelection().getActionCommand

        9
  •  0
  •   Backabock    10 年前
    import javax.swing.Action;
    import javax.swing.ButtonGroup;
    import javax.swing.Icon;
    import javax.swing.JRadioButton;
    import javax.swing.JToggleButton;
    
    public class RadioButton extends JRadioButton {
    
        public class RadioButtonModel extends JToggleButton.ToggleButtonModel {
            public Object[] getSelectedObjects() {
                if ( isSelected() ) {
                    return new Object[] { RadioButton.this };
                } else {
                    return new Object[0];
                }
            }
    
            public RadioButton getButton() { return RadioButton.this; }
        }
    
        public RadioButton() { super(); setModel(new RadioButtonModel()); }
        public RadioButton(Action action) { super(action); setModel(new RadioButtonModel()); }
        public RadioButton(Icon icon) { super(icon); setModel(new RadioButtonModel()); }
        public RadioButton(String text) { super(text); setModel(new RadioButtonModel()); }
        public RadioButton(Icon icon, boolean selected) { super(icon, selected); setModel(new RadioButtonModel()); }
        public RadioButton(String text, boolean selected) { super(text, selected); setModel(new RadioButtonModel()); }
        public RadioButton(String text, Icon icon) { super(text, icon); setModel(new RadioButtonModel()); }
        public RadioButton(String text, Icon icon, boolean selected) { super(text, icon, selected); setModel(new RadioButtonModel()); }
    
        public static void main(String[] args) {
            RadioButton b1 = new RadioButton("A");
            RadioButton b2 = new RadioButton("B");
            ButtonGroup group = new ButtonGroup();
            group.add(b1);
            group.add(b2);
            b2.setSelected(true);
            RadioButtonModel model = (RadioButtonModel)group.getSelection();
            System.out.println(model.getButton().getText());
        }
    }
    
        10
  •  0
  •   demongolem    10 年前

    使用 isSelected() 方法。它会告诉你你的无线按钮的状态。将它与一个循环(比如for循环)结合使用,您可以找到已选择的循环。

        11
  •  0
  •   Shanaya    9 年前
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*; 
    public class MyJRadioButton extends JFrame implements ActionListener
    {
        JRadioButton rb1,rb2;  //components
        ButtonGroup bg;
        MyJRadioButton()
    {
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        rb1=new JRadioButton("male");
        rb2=new JRadioButton("female");
    
        //add radio button to button group
        bg=new ButtonGroup();
        bg.add(rb1);
        bg.add(rb2);
    
        //add radio buttons to frame,not button group
        add(rb1);
        add(rb2);
        //add action listener to JRadioButton, not ButtonGroup
        rb1.addActionListener(this);
        rb2.addActionListener(this);
        pack();
        setVisible(true);
    }
    public static void main(String[] args)
    {
        new MyJRadioButton(); //calling constructor
    }
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        System.out.println(((JRadioButton) e.getSource()).getActionCommand());
    }
    

    }

        12
  •  -3
  •   Chobicus    16 年前
    jRadioOne = new javax.swing.JRadioButton();
    jRadioTwo = new javax.swing.JRadioButton();
    jRadioThree = new javax.swing.JRadioButton();
    

    …然后对于每个按钮:

    buttonGroup1.add(jRadioOne);
    jRadioOne.setText("One");
    jRadioOne.setActionCommand(ONE);
    jRadioOne.addActionListener(radioButtonActionListener);
    

    听者

    ActionListener radioButtonActionListener = new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    radioButtonActionPerformed(evt);
                }
            };
    

    …做任何你需要的事情作为对事件的回应

    protected void radioButtonActionPerformed(ActionEvent evt) {            
           System.out.println(evt.getActionCommand());
        }