У меня есть группа JRadioButtons и один JCheckBox. Если флажок JCheckBox снят, JRadioButtons следует отключить и сбросить, и наоборот. У меня проблема в том, проверяю ли я JCheckBox или нет, JRadioButtons остаются отключенными.
Также, прежде чем перейти к коду, не обращайте внимания на нулевой макет и отсутствие разных классов. Я быстро сделал тестовый проект, чтобы уменьшить количество кода, который мне пришлось бы вставлять сюда.
package test;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 434, 261);
frame.getContentPane().add(panel);
JCheckBox ckbxTestCheckBox = new JCheckBox("Test Check Box");
ckbxTestCheckBox.setBounds(7, 7, 99, 23);
panel.add(ckbxTestCheckBox);
JRadioButton rdbtnTestRadioButton1 = new JRadioButton("Test Radio Button 1");
rdbtnTestRadioButton1.setBounds(7, 34, 121, 23);
panel.add(rdbtnTestRadioButton1);
JRadioButton rdbtnTestRadioButton2 = new JRadioButton("Test Radio Button 2");
rdbtnTestRadioButton2.setBounds(7, 61, 121, 23);
panel.add(rdbtnTestRadioButton2);
JRadioButton rdbtnTestRadioButton3 = new JRadioButton("Test Radio Button 3");
rdbtnTestRadioButton3.setBounds(7, 88, 121, 23);
panel.add(rdbtnTestRadioButton3);
JRadioButton rdbtnTest[] = {rdbtnTestRadioButton1, rdbtnTestRadioButton2, rdbtnTestRadioButton3};
ButtonGroup btnGrpTest = new ButtonGroup();
for(int i = 0; i < rdbtnTest.length; i++){
btnGrpTest.add(rdbtnTest[i]);
}
if(!ckbxTestCheckBox.isSelected()){
for(int i = 0; i < rdbtnTest.length; i++){
rdbtnTest[i].setEnabled(false);
rdbtnTest[i].setSelected(false);
}
} else { //Is this part even necessary?
for(int i = 0; i < rdbtnTest.length; i++){
rdbtnTest[i].setEnabled(true);
}
}
}
}
CheckBoxDemo
, найденный в руководстве, покажет вам, как лучше структурировать ваш код, чтобы следовать ему. - person camickr   schedule 06.06.2016