2016-02-25 40 views
0

當我點擊JButtons時,interclass從未到達,我不知道什麼是錯的。任何和所有的幫助,非常感謝。JButton沒有對actionlistener和mouselistener完整執行任何操作

備註:實現偵聽器的類位於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) { 
     } 
    } 

} 
+0

你只是聲明你的聽衆變量,不會給予任何對象引用他們,所以他們仍然' null'。 –

+0

使用['SwingUtilities#isLeftMouseButton'](https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingUtilities.html#isLeftMouseButton-java.awt.event)會更合適。 MouseEvent-)和等效方法,而不是檢查'MouseEvent'按鈕屬性 – MadProgrammer

回答

3

您需要註冊的ActionListener的相關組件的不null

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 
+0

同意。您需要初始化兩個偵聽器。另外,在「source == xx」中處理'else'情況不是一個壞主意嗎?用於調試目的的語句。 – ldmtwo

+0

那麼你如何初始化mouselistener? –

+1

@AdamMorris基本上同樣的方法,但是使用'ButtonMouseListener'來代替 – MadProgrammer

相關問題