Java 根据单选按钮选择不同的输出
我的问题是,我不知道如何检查是否选择了单选按钮,然后根据选择的单选按钮选择不同的输出。基本上,就像我现在的代码一样,一旦一个单选按钮被选中,它似乎永远被选中。如何根据选择的按钮修复代码以发送不同的输出? 这是我的密码Java 根据单选按钮选择不同的输出,java,swing,user-interface,jradiobutton,Java,Swing,User Interface,Jradiobutton,我的问题是,我不知道如何检查是否选择了单选按钮,然后根据选择的单选按钮选择不同的输出。基本上,就像我现在的代码一样,一旦一个单选按钮被选中,它似乎永远被选中。如何根据选择的按钮修复代码以发送不同的输出? 这是我的密码 JRadioButton radioButton1; JRadioButton radioButton2; JRadioButton radioButton3; JRadioButton radioButton4; int button = 1; 感谢您的帮助。您为什么在JRa
JRadioButton radioButton1;
JRadioButton radioButton2;
JRadioButton radioButton3;
JRadioButton radioButton4;
int button = 1;
感谢您的帮助。您为什么在
JRadioButton
上使用addItemListener
而不是addActionListener
除此之外,这里还有一个非常类似的程序,可以运行,您可能想看看它与您的程序有何不同:我建议您采用以下方法:
- 有一个变量,用于保存所选的ID或数字或任何要作为参数传递给
方法的内容myStats.setCourseOfferings()
- 只实现一个
,以侦听来自ActionListener
的操作事件。将此侦听器连接到每个单选按钮。请参阅教程:了解更多信息JRadioButtons
- 使用方法将值“附加”到每个单选按钮
- 使用方法检索此值并更新第一步中定义的变量
如果不是答案,我不得不承认我不理解这个问题。。顺便说一句,这里没有问题。你的问题是什么?另外:1)对代码块使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程。2) 为了更快地获得更好的帮助,请发布一个。我的问题是,如何根据选择的按钮修复代码以发送不同的输出。到目前为止,isSelected()不起作用。“如何修复代码,根据选择的按钮发送不同的输出?”因此,在该按钮的末尾添加一个“?”,然后。哦,如果有SSCCE,请在评论中告诉我。我添加了这个问题,并删减了代码…这好吗?如果我包含所有的代码,它确实可以编译,但根据你的说法,我不能这么做
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);
}
}
}
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);
}
});