简体   繁体   English

没有可访问的MainRender类型的封闭实例

[英]No enclosing instance of type MainRender is accessible

I have a problem with this code 我的代码有问题

public static void main(String[] args) {
    final GLProfile profile = GLProfile.get(GLProfile.GL2);
    GLCapabilities capabilities = new GLCapabilities(profile);

    final GLCanvas glcanvas = new GLCanvas(capabilities);
    MainRender r = new MainRender();
    glcanvas.addGLEventListener(r);
    glcanvas.setSize(700, 400);

    final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);

    final JFrame frame = new JFrame("Render");
    frame.getContentPane().add(glcanvas);

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            if (animator.isStarted())
                animator.stop();
            System.exit(0);
        }
    });

    frame.setSize(frame.getContentPane().getPreferredSize());

    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(0, 0));
    frame.add(p, BorderLayout.SOUTH);

    keyBindings(p, frame, r);
    animator.start();

    Handler h = new Handler();

    p.addMouseListener(new Handler());
    p.addMouseMotionListener(new Handler());
}

At the Handler h = new Handler(); 在处理程序中,h = new Handler(); Eclipse shows this message Eclipse显示此消息

No enclosing instance of type MainRender is accessible. 没有可访问的MainRender类型的封闭实例。 Must qualify the allocation with an enclosing instance of type MainRender (egxnew A() where x is an instance of MainRender). 必须使用MainRender类型的封闭实例(例如xxNew A(),其中x是MainRender的实例)对分配进行限定。

Any solutions? 有什么办法吗?

The issue is that the Handler is a non-static nested class of MainReader. 问题是Handler是MainReader的非静态嵌套类。 This means that you need an instance of MainReader to be able to instantiate the Handler. 这意味着您需要MainReader的实例才能实例化Handler。 Have a look at this stackoverflow answer for more information about non-static vs static nested classes. 请查看 stackoverflow答案,以获取有关非静态和静态嵌套类的更多信息。

To solve the above issue, either you can make the Handler class static (if you can) or replace 要解决上述问题,可以将Handler类设为静态(如果可以),也可以替换为

Handler h = new Handler();

with

Handler h = r.new Handler();

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

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