Я программирую редактор, который создает матрицу кнопок. Показанный символ кнопки изменяется при нажатии. Поэтому я создал новый класс, расширяющий 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);
}
}
}
И класс кнопки, которую я создал:
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