如何查看是否选择了按钮并将其用作二进制值
我有3个单选按钮b1
,b2
,b3
,它们都是独立的(不在ButtonGroup
中),我需要在用户点击提交时可以保存二进制值(如果按下则为true,如果未按下则为false)保存到变量
//Gets details when submit is pressed
submit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name = t.getText();
String surname = t1.getText();
String studentID = t2.getText();
//Code to place binary values into variables depending on the radio buttons selected
}
});
答案 0 :(得分:2)
如果您正在谈论JRadioButton
,则可以使用isSelected()
方法来了解用户是否进行了检查。
String name = t.getText();
String surname = t1.getText();
String studentID = t2.getText();
boolean checked = myRadioButton.isSelected();
//...
答案 1 :(得分:1)
boolean button1 = b1.isSelected();
boolean button2 = b2.isSelected();
boolean button3 = b3.isSelected();