0

Java 很新。已经学习/学习了几个星期了。我们的任务之一是制作棋盘。目前我有一门叫做“棋盘”的课。这是 GUI,它有一个创建 GUI 的方法,然后实例化另一个类“ChessSquare”的 64 个实例——构成棋盘的棋子。我在一个名为“ChessRun”的类中还有一个主要方法,它启动整个过程并实例化“ChessBoard”。

我的问题是,当单击 ChessSquare 实例之一时,它需要激活 ChessBoard 中的方法。但是,在尝试此操作时,我不断收到“找不到符号”。我知道它无法识别该符号,因为它是在其他地方实例化的,但我该如何解决这个问题?我必须将此方法保留在 ChessBoard 中。

主要方法:

    public class ChessRun{

      public static void main(String[] args){
        int i = 1;
        ChessBoard[] CB = new ChessBoard[2];
        CB[i] = new ChessBoard();
        CB[i].start();
      }

    }

棋盘类:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;

    public class ChessBoard{

      int clickcount = 0;
      int firstsquare;
      int secondsquare;

      public void Chessboard(){
      }

      public void start(){
        int i = 0;
        ChessSquare[] cs = new ChessSquare[64]; 
        JFrame G = new JFrame();
        JPanel P = new JPanel();
        G.setContentPane(P);
        GridLayout grid = new GridLayout(8, 8);
        P.setLayout(grid);
        G.setTitle("Chess Board");
        G.setSize(360, 360);

        while (i < 64){
          cs[i] = new ChessSquare();
          cs[i].setsquarenumber(i);
          cs[i].setsize();
          cs[i].initialpiece(i);
          cs[i].setpiece();
          P.add(cs[i]);
          i++;
        }

        G.setVisible(true);
        G.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }

      public void beenPressed(int sq){
        if(clickcount == 0){
          clickcount++;
          firstsquare = sq;
        }
        else if(clickcount == 1){
          clickcount++;
          secondsquare = sq;
        } 
        else if(clickcount == 2){

      }

      }
    }

ChessSquare 类(错误出现在此类中 - 显示为:CB1.beenPressed(squarenumber); 的行)

    import javax.swing.*;
    import javax.swing.JButton;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;

    public class ChessSquare extends JButton implements ActionListener{

      private int squarenumber;
      private String piece = "emptysquare";

      ImageIcon emptysquare = new ImageIcon("EmptySquare.jpg");
      ImageIcon selectedsquare = new ImageIcon("SelectedSquare.jpg");
      ImageIcon pawn = new ImageIcon("Pawn.jpg");
      ImageIcon bishop = new ImageIcon("Bishop.jpg");
      ImageIcon rook = new ImageIcon("Rook.jpg");
      ImageIcon knight = new ImageIcon("Knight.jpg");
      ImageIcon king = new ImageIcon("King.jpg");
      ImageIcon queen = new ImageIcon("Queen.jpg");

      public ChessSquare(){
        addActionListener(this);
      }

      public void actionPerformed(ActionEvent e){
        System.out.println("Pressed");
        CB[1].beenPressed(squarenumber);
      }

      public void setsquarenumber(int i){
        this.squarenumber = i;
      }

      public void initialpiece(int i){
        if(47 < i && i < 56){
          piece = "pawn";
        }
        if(i == 56 || i == 63){
          piece = "rook";
        }
        if(i == 57 || i == 62){
          piece = "knight";
        }
        if(i == 58 || i == 61){
          piece = "bishop";
        }
        if(i == 60){
          piece = "king";
        }
        if(i == 59){
          piece = "queen";
        }
        }

      public void setsize(){
        this.setPreferredSize(new Dimension(44, 44));
      }

      public String getpiece(){
        return piece;
      }

      public void setpiece(String s){
          piece = s;
      }

      public void setpiece(){
        if(piece == "emptysquare"){
          this.setempty();
        }
        if(piece == "pawn"){
          this.setpawn();
        }
        if(piece == "rook"){
          this.setrook();
        }
        if(piece == "knight"){
          this.setknight();
        }
        if(piece == "bishop"){
          this.setbishop();
        }
        if(piece == "queen"){
          this.setqueen();
        }
        if(piece == "king"){
          this.setking();
        }
      }

      public void setempty(){
        this.setIcon(emptysquare);
      }
      public void setselected(){
        this.setIcon(selectedsquare);
      }
      public void setpawn(){
        this.setIcon(pawn);
      }
      public void setrook(){
        this.setIcon(rook);
      }
      public void setknight(){
        this.setIcon(knight);
      }
      public void setbishop(){
        this.setIcon(bishop);
      }
      public void setqueen(){
        this.setIcon(queen);
      }
      public void setking(){
        this.setIcon(king);
      }

    }
4

1 回答 1

2

CB 数组(应重命名为 chessBoards 以符合 Java 命名约定)尚未在 ChessSquare 类中声明,因此在那里不可见。您需要将数组的引用传递给另一个类,以便它可见。

另外,不要将字符串与==or进行比较!=。这些检查引用相等——如果一个对象与另一个引用对象完全相同,那不是您感兴趣的。请改用该equals(...)方法。

我自己,我会让棋盘将 ActionListener 添加到方格中,而不是将其添加到棋盘类的内部。这样广场就不需要董事会的参考。我也会避免扩展 JButton。

于 2015-03-17T17:02:13.797 回答