如何从附加到一个ActionListener(使用for循环创建)的多个JButton中删除边框?

时间:2017-11-29 01:36:14

标签: java swing user-interface

我正在创建一个使用虚拟键盘输入的刽子手游戏。我使用for循环创建了键盘布局,以创建按钮,然后将其放入JButton ArrayList中。所有按钮都使用一个ActionListener。

在游戏结束时,我需要重置所有按钮,使它们不再有边框(如果玩家猜对了,游戏过程中会在它们周围放置绿色或红色边框)。我试图循环遍历JButton ArrayList来更改边框,但它只对最后按下的按钮执行此操作。有没有办法可以做到这一点?

我也想知道如何让按钮恢复原来的设计,因为当我使用button.setBorder(null)时按钮基本上消失了。我想删除我之前在程序中设置的MatteBorder

public void createKeyboard() {

    String buttonLabel;
    char buttonLabelChar;

    for (char c = 'A'; c <= 'Z'; c ++) {    
        buttonLabelChar = c;
        buttonLabel = "" + buttonLabelChar; 
        JButton button = new JButton(buttonLabel);

        keyboardArray = new ArrayList<JButton>();
        keyboardArray.add(button);

        button.setPreferredSize(new Dimension(40, 40));
        pnlKeyboard.add(button);
        button.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event) {


                    hm.setLetterGuessed(event.getActionCommand().charAt(0));
                    hm.revealLetter();

                    if (hm.revealLetter()) {

                        button.setBorder(new MatteBorder(4, 4, 4, 4, Color.GREEN));
                        button.setEnabled(false);

                    }
                    else {

                        button.setBorder(new MatteBorder(4, 4, 4, 4, Color.RED));
                        button.setEnabled(false);
                        hm.loseLife();

                        setImage();
                    }

                    if (hm.winCheck()) {

                        JOptionPane.showMessageDialog(mainFrame, "You win!", "Game Over",
                                JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));

                        for (int i = 0; i < keyboardArray.size(); i ++) { // THIS DOESN'T WORK
                            button.setBorder(null);
                        }

                    }
                    else if (hm.getLives() == 0) {

                        JOptionPane.showMessageDialog(mainFrame, "You lose :(", "Game Over",
                                JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));

                    }


                    setWord();


                }
            }
        );

    }
}

1 个答案:

答案 0 :(得分:0)

一个选项是完全避免使用匿名类,以便侦听器可以使用keyboardArray方法之外的createKeyboard()

ArrayList<JButton> keyboardArray;
public void createKeyboard() {

    String buttonLabel;
    char buttonLabelChar;

    for (char c = 'A'; c <= 'Z'; c ++) {    
        // ...

        keyboardArray = new ArrayList<JButton>();
        keyboardArray.add(button);

        // ...

        button.addActionListener(new ButtonListener());
    }
}

class ButtonListener {
    public void actionPerformed(ActionEvent event) {

        hm.setLetterGuessed(event.getActionCommand().charAt(0));
        hm.revealLetter();

        if (hm.revealLetter()) {
            button.setBorder(new MatteBorder(4, 4, 4, 4, Color.GREEN));
            button.setEnabled(false);
        } else {
            button.setBorder(new MatteBorder(4, 4, 4, 4, Color.RED));
            button.setEnabled(false);
            hm.loseLife();
            setImage();
        }

        if (hm.winCheck()) {
            JOptionPane.showMessageDialog(mainFrame, "You win!", "Game Over", JOptionPane.INFORMATION_MESSAGE, new ImageIcon( Hangman.class.getResource("images/11rightleg.png")));

            for (int i = 0; i < keyboardArray.size(); i ++) { // THIS DOESN'T WORK
                keyboardArray.get(i).setBorder(null);
            }
        } else if (hm.getLives() == 0) {
            JOptionPane.showMessageDialog(mainFrame, "You lose :(", "Game Over",
                            JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));
        }
        setWord();
    }
}