取消选择 JRadioButton
我正在创建的代码涉及 JRadioButton 和 JComboBox。我希望在选择 JRadioButton 时启用 JComboBox,在未选择或取消选择时禁用 JComboBox。我的问题是,如果我取消选择 JRadioButton,则 JComboBox 将不会被禁用。我该怎么做?这是我的代码
LouisClub=new JComboBox();
LouisClub.setEnabled(false);
LouisClub.addItem("Writer");
LouisClub.addItem("Photojournalist");
LouisClub.addItem("Cartoonist");
LouisClub.addItem("Layout Artist");
Louis=new JRadioButton("The Louisian");
Louis.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LouisClub.setEnabled(true);
}
});
The code I'm creating involves a JRadioButton and a JComboBox. I want the JComboBox to be enabled if the JRadioButton is selected and disabled if it's not selected or deselected. My problem is that the JComboBox won't be disabled if I deselect the JRadioButton. How can I do this? Here's my code
LouisClub=new JComboBox();
LouisClub.setEnabled(false);
LouisClub.addItem("Writer");
LouisClub.addItem("Photojournalist");
LouisClub.addItem("Cartoonist");
LouisClub.addItem("Layout Artist");
Louis=new JRadioButton("The Louisian");
Louis.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LouisClub.setEnabled(true);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于此类事情,您应该使用 JCheckBox 而不是 JRadioButton,然后您需要在 actionPerformed() 方法中检查复选框状态并基于该启用/禁用组合框。另外
,使用 ChangeListener 而不是 ActionListener 可能会更好(不确定)。
You should JCheckBox instead of JRadioButton for such things and then you need check for checkBox status in actionPerformed() method and based on that enable/disable comboBox. Something like
Also it might be good (Not sure) to use ChangeListener instead of ActionListener.
您应该使用 ItemListener 而不是 ActionListener。
下面是代码:
我还有两点意见要说:
他们的变量的开头(他们更喜欢 louisClub 而不是 LouisClub)
窗口(有关详细信息,请参阅 java 教程)。
再见,
让-马克
You should use an ItemListener instead of an ActionListener.
Here is the code :
I have two more comments to make :
beginning of their variables (they prefer louisClub to LouisClub)
window (see the java tutorial for more information).
bye,
Jean-Marc