CheckBoxMenuItem和ButtonGroup

时间:2011-05-28 15:36:24

标签: java swing

有CheckBoxMenuItems和ButtonGroup。当我为当前CheckBoxMenuItem设置侦听器时,将检查该条件并在此侦听器中生成错误。我有另一个CheckBoxMenuItem,它对我来说没有必要,即使我会写“返回”。  问题是该方法不能抛出异常而且该类是匿名的。 这是代码:

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,即停止此操作。但是,无论如何,方法ÂctionPerformed完成后,会自动切换复选框,因为它会被通知View。我需要在方法actionPerformed

中直接阻止它

1 个答案:

答案 0 :(得分:1)

您应该致电MainActivity.this.mAdminMode.setSelected(true);,而不是setEnabled(true)