是否可以通过单击jRadioButton
来更改jComboBox
中的项目?例如:选择第一个jRadioButton
,并且jComboBox
中有五个选项。当您单击同一按钮组中的另一个 jRadiobutton
时,jComboBox
中的选项将替换为新选项。我需要使用 ActionListener
吗?
最佳答案
是的。但我建议使用 ItemListener 而不是 ActionListener。 在 itemStateChanged 方法中,检查单选按钮是否被选中。
radioButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
//change your combobox
}
else if (e.getStateChange() == ItemEvent.DESELECTED) {
//change to another
}
}
});
关于java - 通过单击 jRadiobutton 更改 jComboBox 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30153018/