如何在Java swing中从另一个类中禁用一个类中的多个按钮?

时间:2013-10-22 23:37:54

标签: java swing user-interface jbutton disabled-control

我有这段代码片段(实际上是代码的一部分),在一个类中,我为26个字母表创建了所有Jbutton。我有另一个跟踪时间,时间结束或游戏结束的课程,我想一次性禁用所有26个JButton。

这是创建Jbuttons的代码

public class DetailsPanel extends JPanel {



        public DetailsPanel() {
            setLayout(new BorderLayout());
            setBorder(BorderFactory.createTitledBorder(" click here "));

            JPanel letterPanel = new JPanel(new GridLayout(0, 5));
            for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
                String buttonText = String.valueOf(alphabet);
                JButton letterButton = new JButton(buttonText);
                letterButton.addActionListener(clickedbutton());
                letterPanel.add(letterButton, BorderLayout.CENTER);
            }
            add(letterPanel, BorderLayout.CENTER);
        }

        }

在我的maincontrol class中,我想要关闭

中的所有Jbuttons
public class maincontrol {
     int counter;
     DetailsPanel dp;
     public maincontrol(DetailsPanel dp) {
     this.dp = dp;
     int counter = 0;
}

public void turnoff(){
   if ( counter>10){
       //turn off all here//
    }
}

}

2 个答案:

答案 0 :(得分:3)

保留对DetailsPanel的引用。添加一个方法来禁用按钮,例如:

public class DetailsPanel extends JPanel {
    private final JPanel letterPanel;
    public DetailsPanel() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder(" click here "));

        letterPanel = new JPanel(new GridLayout(0, 5));
        ...
    }
    public void disableButtons() {
        for (Component c : letterPanel.getComponents()) {
          if (c instanceof JButton) c.setEnabled(false);
        }
    }
}

要禁用按钮时调用它。或者更聪明一点,并根据你传入的内部转数来做:

    private static final int MAX_TURNS = 10;
    public void updateButtons(int turn) {
        for (Component c : letterPanel.getComponents()) {
          if (c instanceof JButton) c.setEnabled(turn <= MAX_TURNS);
        }
    }

答案 1 :(得分:1)

正如Hovercraft Full Of Eels已经建议的那样,你可以简单地将所有按钮保存在某种简单的java.util.List中,只需遍历列表,在需要时改变按钮的状态......

例如......

public class DetailsPanel extends JPanel {

    private List<JButton> buttons = new ArrayList<>(26);

    public DetailsPanel() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder(" click here "));

        JPanel letterPanel = new JPanel(new GridLayout(0, 5));
        for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
            String buttonText = String.valueOf(alphabet);
            JButton letterButton = new JButton(buttonText);
            buttons.add(letterButton);
            letterButton.addActionListener(clickedbutton());
            letterPanel.add(letterButton, BorderLayout.CENTER);
        }
        add(letterPanel, BorderLayout.CENTER);
    }

    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (JButton btn : buttons) {
            btn.setEnabled(enabled);
        }
    }

}

这意味着,当您想要禁用按钮时,您只需执行类似......

的操作
// detailsPane is a reference to an instance of DetailsPane
detailsPane.setEnabled(false);

// detailsPane is a reference to an instance of DetailsPane
detailsPane.setEnabled(true);

当你想启用它们时......