繁体   English   中英

eclipse中的错误:未解决的编译

[英]Error in eclipse:unresolved compilation

我有这个代码

public class watermark {

    public static void main(String[] args) {
        wmmain m = new wmmain();
        m.setSize(800, 500);
        m.setVisible(true);
    }

    class wmmain extends JFrame /* MAIN WINDOW */
    {
        JMenuBar jmb;
        // ......
    }
}

它在命令提示符下运行正常,但是当我尝试在eclipse中运行代码时,它给出了以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
No enclosing instance of type watermark is accessible. Must qualify the allocation with an enclosing instance of type watermark (e.g. x.new A() where x is an instance of watermark).

at watermark.main(watermark.java:20)

我该怎么办??

来自文档

要实例化内部类,必须首先实例化外部类。

句法:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

您需要外部类实例来创建Inner类的实例。

wmmain m=new WaterMark().new wmmain();

我想你不能调用innerClass里面自己的静态方法。

import javax.swing.JFrame;
public class testFrame {
public static void main(String[] args) {
    // TODO Auto-generated method stub


}
private void getMe(){
    wmmain m = new wmmain();
    m.setSize(800,500);
    m.setVisible(true);
}
class wmmain extends JFrame /* MAIN WINDOW */
{
    public wmmain(){

    }
     }
     }

尝试这个 。 它会工作。 或者您可以使用PermGenError的答案

如果wmmain类没有使用watermark成员变量并且您想要隐藏窗口类,那么您还可以将wmmain声明为私有静态内部类:

public class watermark {
    public static void main(String[] args) {
        wmmain m =  new wmmain();
        m.setSize(800, 500);
        m.setVisible(true);
    }

    private static class wmmain extends JFrame {
        JMenuBar jmb;
        // ......
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM