人们经常询问这个错误,但我无法找到对我正在处理的代码有帮助的答案。我想这与有一个实例或什么有关?
我希望用户能够在 GUI (GridJApplet) 中输入一个数字,并在单击“Go”时将该数字传递给 JPanel (GridPanel) 以将网格重绘到该宽度和高度。
我尝试在 GridJApplet 中创建 getter 和 setter,但随后无法在我的其他类中使用 getter,它给了我错误“无法在静态上下文中引用非静态方法 getGridSize()”。我在 NetBeans 工作,还没有完成这段代码。我真的不明白如何让用户输入在另一个班级工作。
这是 GridJApplet 中的代码
public void setGridSize() {
size = (int) Double.parseDouble(gridSize.getText());
}
public int getGridSize() {
return this.size;
}
这是来自 GridPanel 的代码
public void executeUserCommands(String command) {
if (command.equals("reset")) {
reset();
} else if (command.equals("gridResize")) {
NUMBER_ROWS = GridJApplet.getGridSize(); //error occurs here
}
repaint();