Game.java调用board.java中存在的函数getLabel()
。当我运行Game.java时,我在终端中收到错误“找不到符号方法getLabel()
”。我无法纠正它。
Game.java
import java.awt.*;
import javax.swing.*;
import javax.swing.JLabel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Game {
private board b;
private bishop bis1;
private JLabel q;
public static void main(String[] args) {
Game f = new Game();
f.start();
}
public void start() {
b = new board();
bis1 = new bishop();
bis1.setLocation(0, 0);
ImageIcon m = bis1.getImage();
q = b.getLabel();
q.addMouseListener(new Mouselist());
b.squares[0][0].add(q);
}
class Mouselist implements MouseListener {
public void mouseClicked(MouseEvent e) {
//k.setIcon(null);
b.squares[1][1].add(new JLabel(new ImageIcon("rook.png")));
}
// mouse entered the JLabel increment count and display it
public void mouseEntered(MouseEvent e) {
b.squares[1][1].add(new JLabel(new ImageIcon("rook.png")));
}
public void mouseExited(MouseEvent e) {
b.squares[1][2].add(new JLabel(new ImageIcon("rook.png")));
}
// mouse was presssed (cliked and released)
// increment counter and display it
public void mousePressed(MouseEvent e) {
b.squares[1][1].add(new JLabel(new ImageIcon("rook.png")));
}
public void mouseReleased(MouseEvent e) {
b.squares[1][1].add(new JLabel(new ImageIcon("rook.png")));
}
}
}
board.java
import javax.swing.*;
import java.awt.*;
import javax.swing.JLabel;
public class board {
public JFrame frame;
public JPanel squares[][] = new JPanel[3][3];
private JLabel sqk = new JLabel(new ImageIcon("knight.png"));
public board() {
frame = new JFrame("Simplified Chess");
frame.setSize(1200, 800);
frame.setLayout(new GridLayout(2, 3));
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
squares[i][j] = new JPanel();
if ((i + j) % 2 == 0) {
squares[i][j].setBackground(Color.black);
} else {
squares[i][j].setBackground(Color.white);
}
frame.add(squares[i][j]);
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public JLabel getLabel() {
return sqk;
}
}
这里可能有什么问题?
答案 0 :(得分:3)
很可能您刚刚添加了getLabel()
方法,但尚未重新编译board.java
答案 1 :(得分:0)
我将你的代码用于board.java和Game.java并将其加载到我的IDE中。我在第24行没有得到错误(q = b.getLabel())。 (我确实得到了一些编译错误,因为你没有提供bishop.java,但这与你的问题无关。)
第24行是您收到错误的地方吗?如果是这样,你能否给我发送3个实际的.java文件。
顺便说一下,尽管与你的问题无关,如上所述,大写班级名称(例如Board和Bishop)是非常标准的做法,我强烈建议你这样做。它将使其他Java程序员更容易阅读您的代码。