"AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: No such child

详细异常如下:

 

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: No such child: 1
	at java.awt.Container.getComponent(Container.java:281)
	at javax.swing.JComponent.rectangleIsObscured(JComponent.java:4337)
	at javax.swing.JComponent.paint(JComponent.java:1025)
	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5122)
	at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:285)
	at javax.swing.RepaintManager.paint(RepaintManager.java:1128)
	at javax.swing.JComponent._paintImmediately(JComponent.java:5070)
	at javax.swing.JComponent.paintImmediately(JComponent.java:4880)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:723)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

 

 

导致该异常发生的大致原因是:

 

Java UI是单线程的,如果在UI操作上使用了多线程,则会导致一些内部属性不同步情况,比如:主线程上正要进行重绘操作,已经获取到了父容器总共有3个子组件;此时,另外一个线程正好从父容器上删除了一个子组件;这就导致了子组件数不同步,当主线程要获取第3个子组件时,就会抛异常了,因为此时已经没有第3个子组件了。

 

 

解决办法:

 

将非主线程对UI进行的操作放入到以下run方法中

 

SwingUtilities.invokeLater(new Runnable() {
	public void run() {
		
	}
});
 

你可能感兴趣的:(经验)