我的问题是我不知道如何检查单选按钮是否被选中,然后根据我选择的单选按钮选择不同的输出。基本上,正如我的代码现在一样,一旦选择了无线电按钮,就会永远选择它。如何根据选择的按钮修复我的代码以发送不同的输出? 这是我的代码......
JRadioButton radioButton1;
JRadioButton radioButton2;
JRadioButton radioButton3;
JRadioButton radioButton4;
int button = 1;
...
private void createCourses(){
JPanel eastPanel = new JPanel(new GridLayout(5, 1, 10, 10));
eastPanel.setBounds(250, 50, 150, 120);
eastPanel.setBorder(raisedetched);
ButtonGroup radio = new ButtonGroup();
JLabel label1 = new JLabel("Course offerings");
radioButton1 = new JRadioButton();
radioButton2 = new JRadioButton();
radioButton3 = new JRadioButton();
radioButton4 = new JRadioButton();
eastPanel.add(label1);
eastPanel.add(radioButton1);
eastPanel.add(radioButton2);
eastPanel.add(radioButton3);
eastPanel.add(radioButton4);
radio.add(radioButton1);
radio.add(radioButton2);
radio.add(radioButton3);
radio.add(radioButton4);
radioButton1.addItemListener(this);
radioButton2.addItemListener(this);
radioButton3.addItemListener(this);
radioButton4.addItemListener(this);
radioButton1.setSelected(true);
radio.getSelection();
contentPane.add(eastPanel);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
if(radioButton1.isSelected() == true){
button = 1;
myStats.setCourseOfferings(button);
}
else{
radioButton1.setSelected(false);
}
if(radioButton2.isSelected() == true){
button = 2;
myStats.setCourseOfferings(button);
}
if(radioButton3.isSelected() == true){
button = 3;
myStats.setCourseOfferings(button);
}
if(radioButton4.isSelected() == true){
button = 4;
myStats.setCourseOfferings(button);
}
}
}
谢谢,任何帮助表示赞赏。
答案 0 :(得分:1)
我建议你采用这种方法:
有一个变量可以保留所选的ID或数字,或者您希望作为myStats.setCourseOfferings()
方法的参数传递的任何内容。
只需实施一个ActionListener
即可收听来自JRadioButtons
的动作事件。将此侦听器附加到每个单选按钮。请查看教程:How to Use Buttons, Check Boxes, and Radio Buttons以获取更多信息。
使用JComponent.putClientProperty()方法将值附加到每个单选按钮。
使用JCOmponent.getClientProperty()方法检索此值并更新第一步中定义的变量。
Integer selectedRadioButton = -1; //declared as class variable
final String COURSE_ID = "CourseID"; //declared class variable
...
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JRadioButton) {
JRadioButton radioButton = (JRadioButton)e.getSource();
selectedRadioButton = (Integer)radioButton.getClientProperty(COURSE_ID);
}
}
};
JRadioButton radioButton1 = new JRadioButton("Radio 1");
radioButton1.putClientProperty(COURSE_ID, 1);
radioButton1.addActionListener(actionListener);
JRadioButton radioButton2 = new JRadioButton("Radio 2");
radioButton2.putClientProperty(COURSE_ID, 2);
radioButton2.addActionListener(actionListener);
// and so on
JButton submit = new JButton("Submit");
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myStats.setCourseOfferings(selectedRadioButton);
}
});
答案 1 :(得分:0)
为什么在addItemListener
上使用addActionListener
代替JRadioButton
?
除此之外,这里有一个非常相似的程序,你可能想知道它与你的不同:http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton