0

我有这个代码片段(实际上是代码的一部分),在一个类中,我有为 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//
    }
}

}
4

2 回答 2

3

保留对您的详细信息面板的引用。添加一个方法来禁用按钮,例如:

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);
        }
    }
于 2013-10-22T23:52:27.633 回答
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);

当您想启用它们时...

于 2013-10-22T23:55:50.110 回答