根据单选按钮选择不同的输出

时间:2013-11-29 14:56:25

标签: java swing user-interface jradiobutton

我的问题是我不知道如何检查单选按钮是否被选中,然后根据我选择的单选按钮选择不同的输出。基本上,正如我的代码现在一样,一旦选择了无线电按钮,就会永远选择它。如何根据选择的按钮修复我的代码以发送不同的输出? 这是我的代码......

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);
}
 }
 }

谢谢,任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

我建议你采用这种方法:

示例(它不是SSCCE,只是一个例子)

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