2012-03-07 77 views
1

嗨我是新来的java,我想我会尝试制作一个游戏,其中用户实际上试图自己解决8皇后问题。然而,它增加了难度,开始8个白嘴鸦,高达14个主教,然后8个皇后。初学者的简单8车赛游戏

我已经成功创建了棋盘。我的mouselistener有一个问题...棋盘上的每个方块都是一个按钮,点击时我的意图是该方块会改变颜色以指示它被点击,然后所有不能再次点击的方块也会改变以指示摆脱游戏的广场。

当正方形被点击时,它似乎没有执行任何操作。 对不起,我知道它的微不足道。 谢谢。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 


public class rooks extends JFrame implements MouseListener{ 

    private final int BOARD_SIZE = 8; 
    private final int BOARD_SIZE_COLS = 8; 
    private final int BOARD_SIZE_ROWS = 8; 
    // private JTextField bottom = new JTextField("")             "); 
    // private JLabel bannerl = new JLabel("The game"); 
    // private JButton queens = new JButton(" Play Queens "); 
    private JButton rooks = new JButton(" Play Rooks "); 
    // private JButton bishops = new JButton(" Play Knights "); 
    private JButton[][] cboard = new JButton[BOARD_SIZE][BOARD_SIZE]; 
    private JTextArea bottomtextarea = new JTextArea(); 




    // constructor creating the chessboard 
    public rooks(){ 
     this.setSize(500, 500); 
     this.setTitle("rooks"); 
     // this.setIconImage(); 

     // create JPanels and add JComponents 
     JPanel main = new JPanel(new BorderLayout()); 
     this.setContentPane(main); 

     JPanel north = new JPanel(); 
     north.setLayout(new GridLayout(1,3)); 
     main.add(north, BorderLayout.NORTH); 
     // north.add(queens); 
     north.add(rooks); 
     // north.add(bishops); 

     JPanel south = new JPanel(); 
     main.add(south, BorderLayout.SOUTH); 
     south.add(bottomtextarea); 
     bottomtextarea.setEditable(false); 
     bottomtextarea.setVisible(true); 

     // create grid (actual chessboard) and initialise each button with no char 
     JPanel chessBoard = new JPanel(new GridLayout(BOARD_SIZE, BOARD_SIZE)); 
     main.add(chessBoard, BorderLayout.CENTER); 
     for (int i=0; i<BOARD_SIZE_ROWS; i++){ 
      for(int j=0; j<BOARD_SIZE_COLS; j++){ 
       cboard[i][j] = new JButton(""); 
       chessBoard.add(cboard[i][j]); 

       // as it loops add colour to the board, if (i+j=even then white, otherwise black) 
       if ((i + j) % 2 == 0) { 
          cboard[i][j].setBackground(Color.black); 
         } 
       else { 
          cboard[i][j].setBackground(Color.white); 
         } 
      } 
     } 

     cboard[7][7].addMouseListener(this); 


     this.setResizable(false); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

     } 

    public void mousePressed(MouseEvent e){ 

    } 
    public void mouseReleased(MouseEvent e) { 

    } 

    public void mouseEntered(MouseEvent e) { 

    } 

    public void mouseExited(MouseEvent e) { 

    } 

    public void mouseClicked(MouseEvent e) { 
     System.out.print("it has been clicked"); 
    } 

    void saySomething(String eventDescription, MouseEvent e) { 

    } 





} 

回答

0

您正在向最后一个按钮添加一个MouseListener,JButton在cboard [7] [7]。

  • 为什么使用MouseListener而不是JButtons的ActionListener?这没有意义。
  • 为什么不添加一个ActionListener到全部在for循环中的JButton?
+0

谢谢,我发布后,我只是把它放在一个JButtons。我使用ActionListener并将其放入循环中,以便将其添加到所有按钮。 – 2012-03-07 21:44:55

1

您的代码正在工作。我运行它,当我点击7-7平方(这是右下角的那个)时,我收到消息:“它已被点击”。

由于您只将鼠标侦听器添加到此平方,代码的行为与预期相同。

但也有一些事情你应该重构:

  • 为什么这么定义BOARD_SIZE,BOARD_SIZE_COLS,BOARD_SIZE_ROWS?如果你只使用二次游戏板,你只需要BOARD_SIZE,如果没有,那么你不需要BOARD_SIZE。
  • 它的惯例是用大写字母书写你的课程的第一个字母。因此,它是白嘴鸦,而不是乌鸦
  • 您需要将您的监听器添加到板的每平方,而不是只有一个

这应该是足够的开始。