我可以在Java中为单选按钮设置一个值吗?
问题描述:
可以在Java中为无线电设置一个值吗?如果选择收音机,我可以获得它们的价值。我可以在Java中为单选按钮设置一个值吗?
编辑:值是一些like:radio1 =值10,radio2,值= 15,radio3 =值30等,如果它被选中或不是。
答
要设置值:
JRadioButton rb = new JRadioButton(Integer.toString(10));
要获取的设定值:
rb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.out.println("Selected value = " + e.getActionCommand());
}
});
编辑
如果您选择当执行相同的操作,多个单选按钮,我建议所有的人都注册相同的ActionListener
:
private class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
System.out.println("Selected value = " + e.getActionCommand());
}
}
答
从代码中选择,你可以使用
JRadioButton rb1 = new JRadioButton("Select Me");
rb1.setSelected(true);
,并获得选择
boolean selected = rb1.isSelected();
是的,但具体将取决于您正在使用哪个Java Radio Button类。 – Quentin 2011-05-06 14:41:07
[电台](http://teesdiary.files.wordpress.com/2010/05/old-school-radio-image.jpg)?另外,您有没有想过看[The Java Tutorials](http://download.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton)? – mre 2011-05-06 14:45:48
单选按钮;) – 2011-05-06 15:16:04