Java 编译器无法解析被覆盖的 JButton 的符号

Java Compiler cannt resolve the Symbol of overwritten JButton

我正在编写一个编辑器,它创建一个按钮矩阵。单击时按钮的显示符号会发生变化。所以我创建了一个新的 class 扩展 JButton 并改变了一些东西。 编译时,编译器告诉我,它无法解析符号 AlienGameButton。但为什么?我该如何解决这个问题?

package MapGenerator;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;

public class MapGenerator {
public static void main(String[] args) {


    //Initialisierung der Mapgröße über einen Input-Dialog

    int hight, width;
    hight = Integer.parseInt(JOptionPane.showInputDialog(null, "Höhe des Spielfeldes: "));
    width = Integer.parseInt(JOptionPane.showInputDialog(null, "Breite des Spielfeldes: "));
    System.out.println(width);
    System.out.println(hight);


    //Erstellen eines Fensters abhängig von der Anzahl der gewünschten Felder

    JFrame GeneratorFenster = new JFrame("Map Generator");
    GeneratorFenster.setSize(hight * 50 + 50, width * 50);
    GeneratorFenster.setVisible(true);

    AlienGameButton buttons[][] = new AlienGameButton[hight][width];

    GeneratorFenster.setLayout(new GridLayout(hight, width));
    for (int i = 0; i < hight; i++) {
        for (int j = 0; j < width; j++) {
            buttons[i][j] = new AlienGameButton();
            GeneratorFenster.add(buttons[i][j]);
        }

        GeneratorFenster.setVisible(true);

    }
}
}

以及我创建的按钮的 class:

package MapGenerator;

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

public class AlienGameButton extends JButton implements ActionListener {

private int count = 0;

public AlienGameButton() {
    this.addActionListener(this);
}


public void actionPerformed(ActionEvent e) {
    count += 1;
    count %= 3;
    switch (count) {
        case 0:
            setText(" ");
            break;

        case 1:
            setText("A");
            break;

        case 2:
            setText("#");
            break;

        case 3:
            setText("P");
            break;

        case 4:
            setText("O");
            break;

    }
}

}

这是编译器错误:

MapGenerator.java:26: error: cannot find symbol
AlienGameButton buttons[][] = new AlienGameButton[hight][width];
^
symbol:   class AlienGameButton
location: class MapGenerator
MapGenerator.java:26: error: cannot find symbol
AlienGameButton buttons[][] = new AlienGameButton[hight][width];
                                  ^
symbol:   class AlienGameButton
location: class MapGenerator
MapGenerator.java:31: error: cannot find symbol
buttons[i][j] = new AlienGameButton();
                            ^
symbol:   class AlienGameButton
location: class MapGenerator
3 errors

如评论中所述,如果您觉得尝试使用 javac FileName.java 进行编译,它是行不通的。请使用以下两个命令使其工作:

javac MapGenerator/AlienGameButton.java
javac MapGenerator/MapGenerator.java

而不是

javac AlienGameButton.java
javac MapGenerator.java

此外,正如您所说,当您将它移动到 src 目录时它开始工作,那是因为包被更改为默认包。

PS:

一些建议和编码标准:

Package name and class name should not be exactly same

Package name should always start from lowercase letter and class name should always start from uppercase letter