如何验证单选按钮的性别?

时间:2018-08-15 16:57:13

标签: java swing jradiobutton

此代码无法正常工作。当我选择男性时,是否每次都会自动选择女性?

如何验证单选按钮的性别?

    rbtnmale = new JRadioButton("male");
    rbtnmale.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        gender="male";
        rbtnmale.setSelected(true);
        rbtnmale.setSelected(false);
    }
});
rbtnmale.setBounds(116, 127, 58, 23);
frame.getContentPane().add(rbtnmale);

rbtnfemale = new JRadioButton("female");
rbtnfemale.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        gender="female";
        rbtnfemale.setSelected(true);
        rbtnmale.setSelected(false);

    }
});

2 个答案:

答案 0 :(得分:2)

您应该使用ButtonGroup,将所有JRadioButton放入其中,然后遍历buttonGroup.getElements()以查找选择了哪个JRadioButton。这是一个完整的示例:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.Enumeration;
import javax.swing.AbstractButton;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class Example {

    ButtonGroup group = new ButtonGroup();//used to store all radio buttons.

    public Example() {
        initComponents();
        currentSelectedOption();
    }

    private void initComponents() {
        //Radio buttons
        JRadioButton female = new JRadioButton("Female");
        JRadioButton male = new JRadioButton("Male");
        JRadioButton other = new JRadioButton("Other");

        female.setSelected(true);//by default, select female.

        //Add all radio buttons to a group. 
        //It will allow to only have one selected at a time.
        group.add(male);
        group.add(female);
        group.add(other);

        //Add your components to a panel, it's a good practice.
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(female);
        panel.add(male);
        panel.add(other);

        JFrame frame = new JFrame("Example");
        frame.setLayout(new BorderLayout());
        frame.setLocationRelativeTo(null);

        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * Getting the current selected radio button.
     *
     * @return Text of the radio button currently selected.
     */
    public String getSelectedOption() {
        Enumeration<AbstractButton> radioButtons = group.getElements();
        while (radioButtons.hasMoreElements()) {
            AbstractButton currentRadioButton = radioButtons.nextElement();
            if (currentRadioButton.isSelected()) {
                return currentRadioButton.getText();
            }
        }
        return null;
    }

    private void currentSelectedOption() {
        String selected = getSelectedOption();
        if (selected == null) {
            System.out.println("There is something wrong! Nothing is selected");
            return;
        }
        switch (selected.toLowerCase()) {
            case "male":
                System.out.println("male is selected");
                break;
            case "female":
                System.out.println("female is selected");
                break;
            case "other":
                System.out.println("other is selected");
                break;
        }

    }
}

public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Example();
                }
            });
        }

答案 1 :(得分:0)

<a class="btn btn-lg btn-primary" data-toggle="modal"
                                  data-target="#addQuestion">Add Questions</a>

根据您当前需要的代码:

Workbooks("workbook name 1").Sheets("sheet name 1").Range("A1:M10").Copy   
Workbooks("workbook name 2").Sheets("sheet name 2").Range("A1").PasteSpecial xlPasteValues

但是,对于两个单选按钮,您甚至都不需要在ActionListener中使用以上代码。

相反,您应该使用 rbtnmale.setSelected(true); rbtnmale.setSelected(false); ,而 //rbtnmale.setSelected(false); rbtnfemale.setSelected(false); 将为您管理每个单选按钮的选定状态:

ButtonGroup

阅读How Use Buttons的Swing教程中的部分,以获得更多信息