Java Swing - 禁用 JCheckbox 时出现问题
我正在禁用 JCheckbox
,然后借助 setEnabled(...)
方法启用它。
但问题是,如果我禁用未选中的复选框,那么在启用它后它就会被选中。
我希望它们在启用后都具有与禁用之前相同的状态。
I am disabling a JCheckbox
and then enabling it with the help of setEnabled(...)
method.
But the problem is if I disable a unselected checkbox, then it becomes selected after I enable it.
I want all of them to have the same state after being enabled that they had before being disabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否使用 ActionListener 启用/禁用 JCheckBox?如果是这样,那么这是正常的,因为当您单击复选框时,isSelected() 状态会发生变化。
您可以做的是使用 isSelected() 和 setSelected() 方法添加检查。
Are you enabling/disabling the JCheckBox using an ActionListener? If so, then that is normal because when you click on the checkbox, the isSelected() status changes.
What you can do is to add checks using isSelected() and the setSelected() methods.
下面的代码按照您的描述工作:
The below code works as you describe:
如果您调用 setEnabled ,则不应影响选定状态。如果是,那么我建议您重新检查您的代码,看看是否有任何东西可以解释这种行为。正如其他人所建议的,也许您发布的代码示例可能会有所帮助。
更新:
为了完全清楚地说明,只有两种方法可以更改复选框的选定状态。
setSelected
调用
setEnabled
不会更改选定状态。因此,您的代码中一定有一些奇怪的东西导致了这种情况。
If you call
setEnabled
that should not affect the selected state. If it is, then I suggest you reexamine your code to see if there is anything that can account for this behaviour. As someone else suggested, maybe if you post a code sample that may be helpful.Update:
To make it perfectly clear, there are only two ways the selected state of a checkbox can be changed.
setSelected
A call to
setEnabled
does not change the selected state.Thus, there must be something odd in your code that is causing this.