这里有CheckBoxMenuItems ButtonGroupи。当我为当前CheckBoxMenuItem设置侦听器时,检查条件并在此侦听器中产生错误。我已经激活了另一个CheckBoxMenuItem,这对我来说不是必须的,即使我会写"return“。问题是该方法不能抛出异常,并且类是匿名的。代码如下:
mUserMode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(currentCard == 0) {
return;
}
boolean IsEmptyFields = true, isCheckedAnswers = false;
// check if all fields is fill in ...
endOfCycle: for(Component component: panelForAddingQuesions.getComponents()) {
if(component instanceof JTextField) {
JTextField question = (JTextField)component;
if(question.getText().length() == 0) {
IsEmptyFields = false;
break endOfCycle;
}
}
}
// and if there is one correct answer in every question
// check if all fields is fill in ...
for(Entry<JTextField, ArrayList<JCheckBox>> entrySets: equivalenceOfQuestionFiledsAndItsAnswers.entrySet()) {
isCheckedAnswers = false;
for(JCheckBox checkbox: entrySets.getValue()) {
if(checkbox.isSelected()) {
isCheckedAnswers = true;
}
}
}
if(IsEmptyFields) {
JOptionPane.showMessageDialog(MainActivity.this,
"Error", "Error",
JOptionPane.ERROR_MESSAGE);
}
else if(isCheckedAnswers) {
JOptionPane.showMessageDialog(MainActivity.this,
"Error","Error",
JOptionPane.ERROR_MESSAGE);
}
else {
cardLayout.last(cardPanel);
currentCard = 0;
}
// It doesn't help
//MainActivity.this.mAdminMode.setEnabled(true);
}
});匿名类中有一个方法(аctionPerformed)。我想在一个条件下取消元素的切换ChechBoxItem,即停止这个操作。但是,无论如何,当方法View ctionPerformed完成时,将会自动切换复选框,因为它将被通知а。并且我需要在actionPerformed方法中直接防止它
发布于 2011-05-29 04:39:55
你应该调用MainActivity.this.mAdminMode.setSelected(true);,而不是setEnabled(true)。
https://stackoverflow.com/questions/6162620
复制相似问题