如何在 Java 中禁用 ButtonGroup 内的 JRadioButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13041264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable a JRadioButton inside a ButtonGroup in Java
提问by Hannibal
i have four JRatioButtons inside a ButtonGroup in Java. The two first are enabled and the other two are disabled. If one specific JRatioButton is selected i need to enable the two disabled JRatioButtons.
我在 Java 中的 ButtonGroup 中有四个 JRatioButton。前两个被启用,另外两个被禁用。如果选择了一个特定的 JRatioButton,我需要启用两个禁用的 JRatioButton。
Im trying this to find the state of the buttons and enable the disabled ones, apparently i found the ones with the disable state but doesnt change that state.
我试图用这个来找到按钮的状态并启用禁用的按钮,显然我找到了禁用状态的按钮,但没有改变该状态。
private void activateButtons() {
Enumeration<AbstractButton> elements = myButtonGroup.getElements();
while (elements.hasMoreElements()) {
AbstractButton button = (AbstractButton)elements.nextElement();
if (button.isEnabled()) {
System.out.println("This button is disabled! The text of the button is: '" + button.getText() + "'");
button.setEnabled(true);
}
}
}
Im getting the text of the disabled buttons, but i cant disable them.
我收到了禁用按钮的文本,但我无法禁用它们。
Any help? Thanks!
有什么帮助吗?谢谢!
回答by seanxiaoxiao
I don't know if you any problems in finding the reference of the radio buttons in the second group or you just cannot disable the radio buttons.
我不知道您在找到第二组中的单选按钮的参考时是否有任何问题,或者您无法禁用单选按钮。
For the first question, it is simple, you just keep the reference of the radio buttons in the second group.
对于第一个问题,很简单,您只需保留第二组中单选按钮的引用即可。
For the second question, you need to subclass a JRadioButton because I found you can not simply call disable for an object of radio button.
对于第二个问题,您需要对 JRadioButton 进行子类化,因为我发现您不能简单地为单选按钮对象调用 disable。
The code sample of the sub class would be like this.
子类的代码示例将是这样的。
this.editable = editable;
if (editable) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
super.enableEvents(Event.MOUSE_DOWN | Event.MOUSE_UP);
} else {
this.setCursor(CursorFactory.createUnavailableCursor());
super.disableEvents(Event.MOUSE_DOWN | Event.MOUSE_UP);
}
回答by Yasas
Try this, it works.
试试这个,它有效。
AbstractButton button = ...
button.getModel().setEnabled(true/false)
回答by sky91
Ya, you setEnabled(true)to an enabled RadioButton.
So here the edited, hope can help someone.
是的,您将启用(真)设置为启用的RadioButton。
所以在这里编辑,希望可以帮助某人。
private void activateButtons()
{
Enumeration<AbstractButton> elements = myButtonGroup.getElements();
while (elements.hasMoreElements())
{
AbstractButton button = (AbstractButton)elements.nextElement();
if (button.isEnabled()) // if enabled (true)
{
System.out.println("This button is disabled! The text of the button is: '" + button.getText() + "'");
button.setEnabled(false); // set it disabled (false)
}
}
}
Thanks @Hannibal, your post saved my day.
谢谢@Hannibal,你的帖子拯救了我的一天。