我正在尝试为我正在编码的应用程序创建一个2D渲染器。
我正在使用paintComponent将所有图像绘制到JPanel中。事情出了问题。
首先,我用过这个:
frame.getContentPane().add(p);
frame.getContentPane().validate();
frame.getContentPane().repaint();
这个工作正常,但框架会因JPanels而过载。
当我使用它作为我的代码时,一切都很完美:
frame.getContentPane().add(p);
frame.getContentPane().validate();
frame.getContentPane().repaint();
frame.getContentPane().list()
System.out.print("\n\n\n");
frame.getContentPane().remove(p);
这对我需要的东西来说是完美的。除了我不希望控制台被内容窗格上的内容发送垃圾邮件。
所以我决定删除list方法并执行此操作:
frame.getContentPane().add(p);
frame.getContentPane().validate();
frame.getContentPane().repaint();
frame.getContentPane().remove(p);
那时它坏了。
这是我得到的错误。
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 1
at java.awt.Container.getComponent(Container.java:302)
at javax.swing.JComponent.rectangleIsObscured(JComponent.java:4340)
at javax.swing.JComponent.paint(JComponent.java:1028)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5139)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:302)
at javax.swing.RepaintManager.paint(RepaintManager.java:1188)
at javax.swing.JComponent._paintImmediately(JComponent.java:5087)
at javax.swing.JComponent.paintImmediately(JComponent.java:4897)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:783)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:735)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:58)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1593)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:670)
at java.awt.EventQueue.access$400(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.awt.EventQueue$2.run(EventQueue.java:629)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:640)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
任何人对如何解决都有任何建议吗?