在 Java 中遇到困难 class 实例调用不同 class 实例中的方法
Having difficulties in Java with class instances calling upon methods in different class instances
对 Java 很陌生。已经 studying/learning 几周了。我们的任务之一是制作棋盘。目前我有一个名为 'ChessBoard' 的 class。这是 GUI,它有一个创建 GUI 的方法,然后还实例化另一个 class "ChessSquare" 的 64 个实例 - 组成棋盘的国际象棋方格。我在 class 中还有一个名为 "ChessRun" 的主要方法,它启动整个过程并实例化 'ChessBoard'.
我的问题是,当单击其中一个 ChessSquare 实例时,它需要激活 ChessBoard 中的一个方法。但是,尝试此操作时我一直收到 'cannot find symbol' 。我知道它无法识别该符号,因为它是在其他地方实例化的,但我该如何解决这个问题?我必须将此方法保留在 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();
}
}
棋盘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 class(错误在此 class - 行中显示: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);
}
}
CB 数组(应重命名为 chessBoards 以符合 Java 命名约定)尚未在 ChessSquare class 中声明,因此在那里不可见。您需要将数组的引用传递给另一个 class 以便它可见。
此外,不要将字符串与 ==
或 !=
进行比较。这些检查引用相等性——如果一个对象与另一个引用对象完全相同,而这不是您感兴趣的。请改用 equals(...)
方法。
我自己,我会让棋盘将 ActionListener 添加到方块中,而不是将其添加到棋盘内部 class。这样广场就不需要董事会的参考资料了。我也会避免扩展 JButton。
对 Java 很陌生。已经 studying/learning 几周了。我们的任务之一是制作棋盘。目前我有一个名为 'ChessBoard' 的 class。这是 GUI,它有一个创建 GUI 的方法,然后还实例化另一个 class "ChessSquare" 的 64 个实例 - 组成棋盘的国际象棋方格。我在 class 中还有一个名为 "ChessRun" 的主要方法,它启动整个过程并实例化 'ChessBoard'.
我的问题是,当单击其中一个 ChessSquare 实例时,它需要激活 ChessBoard 中的一个方法。但是,尝试此操作时我一直收到 'cannot find symbol' 。我知道它无法识别该符号,因为它是在其他地方实例化的,但我该如何解决这个问题?我必须将此方法保留在 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();
}
}
棋盘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 class(错误在此 class - 行中显示: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);
}
}
CB 数组(应重命名为 chessBoards 以符合 Java 命名约定)尚未在 ChessSquare class 中声明,因此在那里不可见。您需要将数组的引用传递给另一个 class 以便它可见。
此外,不要将字符串与 ==
或 !=
进行比较。这些检查引用相等性——如果一个对象与另一个引用对象完全相同,而这不是您感兴趣的。请改用 equals(...)
方法。
我自己,我会让棋盘将 ActionListener 添加到方块中,而不是将其添加到棋盘内部 class。这样广场就不需要董事会的参考资料了。我也会避免扩展 JButton。