java - 无法从静态上下文错误中引用非静态方法

标签 java algorithm compiler-construction static compiler-errors

这个问题在这里已经有了答案:





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/

相关文章:

algorithm - 优化哈里斯角点检测器

windows - erlang在windows上的开发环境

c++ - 如何使用 C++ 增加 Visual Studio 内部版本号?

c++ - 头文件和库文件有什么区别?

java - 单击按钮时发出一个 url,它作为后台进程运行

Java多播发送数据,不接收

perl - 一种估计程序运行时间的算法

java - 相同的 org.w3c.dom.Document 变量的行为不同

java - Spring Data ElasticSearch TransportClient Java 配置

javascript 扫雷器 floodfill 算法无法正常工作