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

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

    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);

    }
});

最佳答案

您应该使用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();
                }
            });
        }

09-15 16:56