简体   繁体   English

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

[英]Error in eclipse:unresolved compilation

i have this code 我有这个代码

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;
        // ......
    }
}

It works fine from the command prompt but when i try to run the code in eclipse it gives me the following error: 它在命令提示符下运行正常,但是当我尝试在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)

What should i do?? 我该怎么办??

From Documentation : 来自文档

To instantiate an inner class, you must first instantiate the outer class. 要实例化内部类,必须首先实例化外部类。

syntax: 句法:

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

you need Outer class instance to create an instance of your Inner class. 您需要外部类实例来创建Inner类的实例。

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

I think you cannot call innerClass inside form Static method of its own. 我想你不能调用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(){

    }
     }
     }

Try this . 尝试这个 。 it will work . 它会工作。 or you can use the answer of PermGenError 或者您可以使用PermGenError的答案

If the wmmain class is not using member variables from watermark and you want to hide the window class, then you could also declare wmmain as a private static inner class: 如果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