我正在尝试为我正在编码的应用程序创建一个 2D 渲染器。
我正在使用paintComponent 将所有图像绘制到JPanel,并且很好。事情出了问题。
首先,我使用了这个:
frame.getContentPane().add(p);
frame.getContentPane().validate();
frame.getContentPane().repaint();
这工作得很好,除了框架会因 JPanel 过载。
当我将其用作我的代码时,一切都运行良好:
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)
有人对如何修复有任何建议吗?