0

Javaはかなり新しいです。数週間勉強/学習しています。私たちの課題の 1 つは、チェス盤を作ることです。現時点では、「ChessBoard」というクラスがあります。これは GUI であり、GUI を作成し、別のクラス "ChessSquare" (ボードを構成するチェスの正方形) の 64 個のインスタンスもインスタンス化するメソッドがあります。また、すべてを開始して「ChessBoard」をインスタンス化する「ChessRun」というクラスのメイン メソッドもあります。

私の問題は、ChessSquare インスタンスの 1 つがクリックされたときに、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 配列 (Java の命名規則に準拠するために chessBoards に名前を変更する必要があります) は ChessSquare クラス内で宣言されていないため、そこには表示されません。配列の参照を他のクラスに渡して、表示できるようにする必要があります。

==また、文字列をorと比較しないでください!=。これらは参照の等価性をチェックします -- 1 つのオブジェクトが別の参照オブジェクトとまったく同じであり、それが目的ではない場合。equals(...)代わりにメソッドを使用してください。

私自身、チェス スクエア クラスの内部に ActionListener を追加するのではなく、チェス ボードに ActionListener を追加してもらいます。そうすれば、正方形はボードの参照を必要としません。また、JButton の拡張も避けます。

于 2015-03-17T17:02:13.797 に答える