这个问题在这里已经有了答案:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 个回答)
8年前关闭。
我有一个名为 sagum 的类,它有一个方法名 paint
public void paint(Graphics g) {
int[] apw1 = null;
int n1 = 0;
for (int j = 1; j <= n1; j++) {
g.drawRect(50, 50, (apw1[j] * 20), 30);
String[] p = null;
g.drawString("p" + p[j - 1], (55 + (apw1[j - 1] * 20)), 70);
g.drawString("" + apw1[j - 1], 50 + (apw1[j - 1] * 20), 100);
}
g.drawString("" + apw1[n1], 50 + (apw1[n1] * 20), 100);
}
我想调用它
n1=count+1;
paint(g);
我得到一个错误
non-static method paint(Graphics) cannot be referenced from a static context
我只是在执行方法后尝试调用它来执行我的方法的图形应用程序(调度算法)。
请帮忙
最佳答案
你不应该调用 paint(...)
直接方法,因此您的错误虽然很重要,但需要理解(请参阅 this link ),但不是问题。你需要重新构建你的程序,这样你就不会觉得你有必要进行这个调用。大多数 Swing 和 AWT 应用程序通过更改类属性然后调用 repaint()
来执行“被动”绘图来工作得最好。在渲染的组件上,以便建议 JVM 重新绘制它。
关于java - 无法从静态上下文错误中引用非静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855348/