【问题标题】:Java Compiler cannt resolve the Symbol of overwritten JButtonJava 编译器无法解析被覆盖的 JButton 的符号
【发布时间】:2018-06-23 15:45:38
【问题描述】:

我正在编写一个编辑器,它会创建一个按钮矩阵。单击时显示的按钮符号会发生变化。所以我创建了一个扩展 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);

    }
}
}

还有我创建的 Button 的类:

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

【问题讨论】:

  • 请包含完整的编译错误。关于您的代码的一些评论:混合德语和英语令人困惑。选择一个(最好是英语)并坚持下去。 --- 包名、变量名、字段名和方法名应始终以小写字母开头。
  • 附带说明:您不应该为此重写 JButton,因为您不会更改 JButton 的固有行为,只是附加一个 ActionListener。此外,您还需要学习和使用Java naming conventions。变量名应全部以小写字母开头,而类名应以大写字母开头。学习这一点并遵循这一点将使我们更好地理解您的代码,并使您更好地理解他人的代码。
  • 提示:添加@HovercraftFullOfEels(或任何重要的@)以通知新评论的人。 “我在博客 2 中添加了完整的错误消息” 什么是“博客 2”?
  • 只是为了确定:MapGenerator.javaAlienGameButton.java 在同一个目录中?

标签: java swing inheritance compiler-errors jbutton


【解决方案1】:

如 cmets 中所述,如果您觉得尝试使用 javac FileName.java 进行编译,它将无法正常工作。请使用以下两个命令使其工作:

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

而不是

javac AlienGameButton.java
javac MapGenerator.java

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

PS:

一些建议和编码标准:

包名和类名不能完全相同

包名 应始终以小写字母开头,类名应始终 从大写字母开始

【讨论】:

  • 非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多