在Java中遇到困难,类实例调用不同类实例中的方法

时间:2015-03-17 16:56:52

标签: java swing instances cannot-find-symbol

Java的新手。现在正在学习/学习几周。我们的任务之一是制作一个国际象棋棋盘。目前我有一个名为“ChessBoard”的课程。这是GUI,它有一个创建GUI的方法,然后实例化另一个类的64个实例" ChessSquare" - 构成棋盘的国际象棋方块。我在一个名为" 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();
      }

    }

ChessBoard Class:

    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);
      }

    }

1 个答案:

答案 0 :(得分:2)

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

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

我自己,我有棋盘将ActionListener添加到方块而不是在棋方方面内部添加它。这样广场就不需要董事会的参考。我也会避免扩展JButton。