gpt4 book ai didi

java - 如何验证性别单选按钮?

转载 作者:行者123 更新时间:2023-12-02 01:59:25 24 4
gpt4 key购买 nike

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

如何验证性别单选按钮?

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

关于java - 如何验证性别单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51863144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com