0

I am programming an editor, which creates a button matrix. The shown symbol of the button changes while clicking. So i created a new class extending JButton and changed something. When compiling the Compiler tells me, that it cannot resolve the symbol AlienGameButton. But why? How can I solve the problem?

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);

    }
}
}

And the class of the Button I created:

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;

    }
}

}

Here is the compiler error:

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
Turing85
  • 18,217
  • 7
  • 33
  • 58
YuKanda
  • 3
  • 2
  • 2
    Please include the full compilation error. Some remarks on your code: mixing german and english is confusing as hell. Choose one (preferably english) and stick with it. --- Package-, variable-, field- and methodnames should always start with a lowercase letter. – Turing85 Jun 23 '18 at 15:48
  • 2
    As a side note: you shouldn't be overriding JButton for this since you're not changing the innate behavior of JButton, just attaching an ActionListener. Also, you will want to learn and use [Java naming conventions](http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. – Hovercraft Full Of Eels Jun 23 '18 at 15:51
  • 1
    Please look at [What does a “Cannot find symbol” compilation error mean?](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – Hovercraft Full Of Eels Jun 23 '18 at 15:52
  • Tip: Add @HovercraftFullOfEels (or whoever, the `@` is important) to *notify* the person of a new comment. *"I added the full error message at blog 2"* What is 'blog 2'? – Andrew Thompson Jun 23 '18 at 15:55
  • Just to be sure: `MapGenerator.java` and `AlienGameButton.java` are in the same directory? – Turing85 Jun 23 '18 at 15:56
  • @YuKanda How are you running application? Have you ensured that AlienGameButton is compiled? – Aman Chhabra Jun 23 '18 at 15:57
  • @Turing85 Seems to be the case as both of them have same package – Aman Chhabra Jun 23 '18 at 15:57
  • @AmanChhabra one cannot run a java program if it is not yet compiled... And since it is not compiling, it may well be that they are not located in the same directory. – Turing85 Jun 23 '18 at 15:57
  • @Turing85 Yes, they are both in the same Package called MapGenerator. – YuKanda Jun 23 '18 at 15:58
  • Rebuild the project – Hovercraft Full Of Eels Jun 23 '18 at 15:59
  • @AmanChhabra I tried to compile it in IntelliJ with javac filename.java. With both files. AlienGameButton compiles but the other one doesnt. – YuKanda Jun 23 '18 at 15:59
  • I cannot reproduce the problem. For me, the code compiles just fine. – Turing85 Jun 23 '18 at 16:02
  • @YuKanda Try compiling using javac MapGenerator/AlienGameButton.java and then javac MapGenerator/MapGenerator.java – Aman Chhabra Jun 23 '18 at 16:04
  • I dont know why, but i copied the two files from the package in the src directory. now it works. i dont know why. – YuKanda Jun 23 '18 at 16:06
  • @YuKanda That is because you are not using package name while compiling . Please see my answer below: https://stackoverflow.com/a/51002900/1262248 – Aman Chhabra Jun 23 '18 at 16:11

1 Answers1

0

As mentioned in the comments, if you feel try compiling using javac FileName.java, it won't work. Please use the below two commands to make it work:

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

instead of

javac AlienGameButton.java
javac MapGenerator.java

Also, as you said when you move it to src directory it starts working, that is because then package is changed to default package.

PS:

Some suggestions and coding standards:

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

Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39