JButton没有使用actionlistener和mouselistener完整地执行任何操作

时间:2016-02-25 00:23:48

标签: java swing jbutton actionlistener

当我点击JButtons时,从未达到过类别,我不知道什么是错的。非常感谢任何和所有帮助。

注意:实现侦听器的类位于MineSweeperPanel类中。

public class MineSweeperPanel extends JPanel {

    // declarations of all variables and components
    private ActionListener listener;
    private Cell iCell;
    //private Icon emptyIcon;
    //private Icon mineIcon;
    //private Icon flagIcon;
    private int size;
    private int mines;
    private int lostCount;
    private int winCount;
    private JButton[][] board;
    private JButton quitButton;
    private JButton resetButton;
    private JLabel lostLabel;
    private JLabel winLabel;
    private JPanel somePanel;
    private JPanel otherPanel;
    private MineSweeperGame game;
    private MouseListener mListener;

    /**********************************************************************
     * Default constructor that creates all of the components of the 
     * gui including the positioning of all of the components.
     *********************************************************************/
    public MineSweeperPanel() {
        // initializations of all components and variables
        // that are used in the constructor.
        setSize();
        winCount = 0;
        lostCount = 0;
        board = new JButton[size][size];
        somePanel = new JPanel();
        otherPanel = new JPanel();
        game = new MineSweeperGame(size,mines);
        winLabel = new JLabel("Wins: " + winCount);
        lostLabel = new JLabel("Loses: " + lostCount);
        quitButton = new JButton("Quit");
        resetButton = new JButton("New Game");

        somePanel.setLayout(new GridLayout(size,size));

        //emptyIcon = new ImageIcon("empty_icon.png");

        // creates the board (each cell) and adds it to the panel
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
                board[row][col] = new JButton (""/*, emptyIcon*/);
                board[row][col].setPreferredSize(new Dimension(25,25));
                board[row][col].setMargin(new Insets(0,0,0,0));
                board[row][col].setFocusPainted(false);
                board[row][col].addActionListener(listener);
                board[row][col].addMouseListener(mListener);
                somePanel.add(board[row][col]);
            }
        }

        // calls to refresh the board display
        displayBoard();

        // adds the win and lost counters/labels to the panel
        otherPanel.add(winLabel);
        otherPanel.add(lostLabel);

        // adds the reset button to the panel
        resetButton.addActionListener(listener);
        otherPanel.add(resetButton);

        // adds the quit button to the panel
        quitButton.addActionListener(listener);
        otherPanel.add(quitButton);

        otherPanel.setLayout(new GridLayout(3, 2));

        // adds panels to JPanel
        add(somePanel);
        add(otherPanel);
    }
        ...
    /**********************************************************************
     * An ActionListener for the quit and reset buttons.
     *********************************************************************/
    public class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == quitButton) {
                System.exit(1);
            }
            else if(e.getSource() == resetButton) {
                game.reset();
            }
            displayBoard();
        }
    }

    /**********************************************************************
     * A MouseListener for left/right click actions - handles flagging 
     * as well as revealing cells.
     *********************************************************************/
    public class ButtonMouseListener implements MouseListener {
        @Override
        public void mouseClicked(MouseEvent e) {
            // if left clicked exposes cell
            if(e.getButton() == MouseEvent.BUTTON1) {
                for(int row = 0; row < size; row++) {
                    for(int col = 0; col < size; col++) {
                        if(e.getSource() == board[row][col])
                            if(!game.getCell(row, col).isExposed()) {
                                game.select(row, col);
                                board[row][col].setEnabled(false);
                            }
                    }
                }
                displayBoard();
            }
            // if right clicked determines a flag or erases flag
            else if(e.getButton() == MouseEvent.BUTTON3) {
                for(int row = 0; row < size; row++) {
                    for(int col = 0; col < size; col++) {
                        if(e.getSource() == board[row][col]) {
                            if(game.getCell(row, col).isFlagged()) {
                                game.getCell(row,col).setFlagged(false);
                            }
                            else {
                                game.getCell(row,col).setFlagged(true);
                            }
                        }
                    }
                }
                displayBoard();
            }
            // if game is won notify player and start a new game
            if(game.getGameStatus() == GameStatus.Won) {
                JOptionPane.showMessageDialog(null, "You Won!");
                game.reset();
                winCount++;
                displayBoard();
                repaint();
            }
            // if game is lost notify player and start a new game
            if(game.getGameStatus() == GameStatus.Lost) {
                JOptionPane.showMessageDialog(null, "You Lose.");
                game.reset();
                lostCount++;
                displayBoard();
                repaint();
            }
        }

        // the following methods are to satisfy the
        // internal class of MouseListener requirements
        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }
    }

}

1 个答案:

答案 0 :(得分:3)

您需要为null ActionListener注册相关组件:

listener = new ButtonListener();//init the listener first, currently the listener is NULL then continue your code
// initializations of all components and variables
// that are used in the constructor.
setSize();
winCount = 0;
lostCount = 0