final JRadioButton sex_male = new JRadioButton();
buttonGroup.add(sex_male);
sex_male.setText("男");
sex_male.setBounds(104, 151, 42, 26);
getContentPane().add(sex_male); final JRadioButton sex_female = new JRadioButton();
buttonGroup.add(sex_female);
sex_female.setText("女");
sex_female.setBounds(157, 151, 47, 26);
getContentPane().add(sex_female);
//确认注册信息中需要sex_
if(buttonGroup.getSelection() == sex_male.getModel()){
sex_ = "男";
}
else{
sex_ = "女";
}貌似实现不了啊
buttonGroup.add(sex_male);
sex_male.setText("男");
sex_male.setBounds(104, 151, 42, 26);
getContentPane().add(sex_male); final JRadioButton sex_female = new JRadioButton();
buttonGroup.add(sex_female);
sex_female.setText("女");
sex_female.setBounds(157, 151, 47, 26);
getContentPane().add(sex_female);
//确认注册信息中需要sex_
if(buttonGroup.getSelection() == sex_male.getModel()){
sex_ = "男";
}
else{
sex_ = "女";
}貌似实现不了啊
一般情况下很少用ButtonGroup的getSelection()来判断
只是用它来约束一组按钮只能选择一个的情况。
sex_ = "女"同理
import javax.swing.JRadioButton;
import java.awt.Rectangle;
import javax.swing.ButtonGroup;public class Frame1 extends JFrame {
public Frame1() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(null);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(93, 99, 76, 24));
this.getContentPane().add(jRadioButton1);
this.getContentPane().add(jRadioButton2);
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("女");
String dr;
if(jRadioButton2.isSelected()){
dr="女";
}else{
dr="男";
}
jRadioButton2.setBounds(new Rectangle(171, 98, 80, 25));
this.setVisible(true);
} JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
ButtonGroup buttonGroup1 = new ButtonGroup();
public static void main(String[] a){
new Frame1();
}
}