我的代码存在问题
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());
}
在Handler h = new Handler(); Eclipse显示此消息
无法访问MainRender类型的封闭实例。必须使用MainRender类型的封闭实例限定分配(例如x.new A(),其中x是MainRender的实例)。
任何解决方案?
答案 0 :(得分:0)
问题是Handler是MainReader的非静态嵌套类。这意味着您需要一个MainReader实例才能实例化Handler。有关非静态嵌套类和静态嵌套类的更多信息,请查看this stackoverflow答案。
要解决上述问题,您可以将Handler类设为静态(如果可以)或替换
Handler h = new Handler();
与
Handler h = r.new Handler();