我在Java中的ButtonGroup中有四個JRatioButton。兩個第一個被啓用,另外兩個被禁用。如果選擇了一個特定的JRatioButton,我需要啓用兩個禁用的JRatioButton。如何在Java中的ButtonGroup中禁用JRadioButton
我試圖找到按鈕的狀態,並啓用禁用的狀態,顯然我找到了禁用狀態,但沒有改變這種狀態。
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);
}
}
}
即時通訊獲取禁用的按鈕的文本,但我不能禁用它們。
任何幫助?謝謝!
您只需要保留對ButtonGroup對象或jRadioButton對象本身的引用,並在需要時將其禁用。 'ButtonGroup'提供'getElements()'方法 –
您可能正在尋找_enabled_屬性。 – trashgod
我編輯的問題,你可以看到我嘗試使用ButtonGroup的getElements()方法,我可以找到什麼按鈕被禁用,並使用控制檯帶來的文本,但我不能使用setEnabled方法來禁用jRadioButton ....不知道爲什麼...感謝您的幫助 – Hannibal