Java如何获取RadioButton那个按钮被选择的信息?
ButtonGroupgroupsex=newButtonGroup();JRadioButtonmale=newJRadioButton("男",true);male....
ButtonGroup groupsex = new ButtonGroup();
JRadioButton male = new JRadioButton("男",true);
male.setBounds(298, 50, 42, 23);
jf.getContentPane().add(male);
JRadioButton female = new JRadioButton("女",false);
female.setBounds(342, 50, 42, 23);
jf.getContentPane().add(female);
groupsex.add(male);
groupsex.add(female);
if (female.isSelected()) {
sex = "女";
} else {
sex = "男";
}
这样不对是吧? 展开
JRadioButton male = new JRadioButton("男",true);
male.setBounds(298, 50, 42, 23);
jf.getContentPane().add(male);
JRadioButton female = new JRadioButton("女",false);
female.setBounds(342, 50, 42, 23);
jf.getContentPane().add(female);
groupsex.add(male);
groupsex.add(female);
if (female.isSelected()) {
sex = "女";
} else {
sex = "男";
}
这样不对是吧? 展开
2个回答
2019-03-28
展开全部
JRadioButton rb1=new JRadioButton("1年"); //创建JRadioButton对象
JRadioButton rb2=new JRadioButton("3年"); //创建JRadioButton对象
JRadioButton rb3=new JRadioButton("5年"); //创建JRadioButton对象
ButtonGroup group=new ButtonGroup();
//添加JRadioButton到ButtonGroup中
group.add(rb1);
group.add(rb2);
group.add(rb3);
rb1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(rb1.isSelected())
System.out.println("刚解决的");
}
}) ;
rb2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(rb2.isSelected())
System.out.println("你试试");
}
}) ;
rb3.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(rb3.isSelected())
System.out.println("ahhahhaha");
}
}) ;
JRadioButton rb2=new JRadioButton("3年"); //创建JRadioButton对象
JRadioButton rb3=new JRadioButton("5年"); //创建JRadioButton对象
ButtonGroup group=new ButtonGroup();
//添加JRadioButton到ButtonGroup中
group.add(rb1);
group.add(rb2);
group.add(rb3);
rb1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(rb1.isSelected())
System.out.println("刚解决的");
}
}) ;
rb2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(rb2.isSelected())
System.out.println("你试试");
}
}) ;
rb3.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(rb3.isSelected())
System.out.println("ahhahhaha");
}
}) ;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String selected = null;
if (female.isSelected()) {
selected = "female";
} else if (male.isSelected()) {
selected = "male";
}
System.out.println(selected);
}
}
female.addActionListener(new Listener());
male.addActionListener(new Listener());
最后
ButtonGroup bg=new ButtonGroup();
bg.add(female);
bg.add(male);
//让两个按钮互斥
public void actionPerformed(ActionEvent e) {
String selected = null;
if (female.isSelected()) {
selected = "female";
} else if (male.isSelected()) {
selected = "male";
}
System.out.println(selected);
}
}
female.addActionListener(new Listener());
male.addActionListener(new Listener());
最后
ButtonGroup bg=new ButtonGroup();
bg.add(female);
bg.add(male);
//让两个按钮互斥
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询