我们一直在办公室苦苦挣扎,在我们的(大型)小程序中看似无法解决的错误,这是一个用于考试目的的MS Word的Java模拟。这种例外似乎源于摆动本身,我不确定如何解决它。这是发生的事情:
小程序加载屏幕直到100%。在此之后,所有组件都以破碎的方式显示,好像它们的某些绘制方法没有被正确调用,将光标悬停在某些按钮等上会使它们闪烁/显示一段时间。并且每隔一秒左右就会向控制台打印一个异常。 (见下文) Eclipse中不会出现此错误,仅在浏览器中出现。
摘要:Applet组件都是在没有异常的情况下构建的,但在某个组件的每次“绘制”调用中(我假设),它会在视觉上发出错误并且每秒打印一次:
Exception in thread "AWT-EventQueue-11" java.lang.ClassCastException: javax.swing.JLabel cannot be cast to javax.swing.text.JTextComponent
at javax.swing.text.html.EditableView.paint(Unknown Source)
at javax.swing.text.BoxView.paintChild(Unknown Source)
at javax.swing.text.BoxView.paint(Unknown Source)
at javax.swing.text.BoxView.paintChild(Unknown Source)
at javax.swing.text.BoxView.paint(Unknown Source)
at javax.swing.text.ParagraphView.paint(Unknown Source)
at javax.swing.text.html.ParagraphView.paint(Unknown Source)
at javax.swing.text.BoxView.paintChild(Unknown Source)
at javax.swing.text.BoxView.paint(Unknown Source)
at javax.swing.text.html.BlockView.paint(Unknown Source)
at javax.swing.text.BoxView.paintChild(Unknown Source)
at javax.swing.text.BoxView.paint(Unknown Source)
at javax.swing.text.html.BlockView.paint(Unknown Source)
at javax.swing.plaf.basic.BasicHTML$Renderer.paint(Unknown Source)
at javax.swing.plaf.basic.BasicLabelUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
帮助将不胜感激,因为这让我疯狂,我似乎无法找到触发这个的因素,因为代码来自之前的开发人员,而且完全混乱。我尝试过不同的JDK版本。
感谢名单。
答案 0 :(得分:4)
java.lang.ClassCastException:javax.swing.JLabel无法强制转换为 javax.swing.text.JTextComponent中
参见JTextComponents的组件树,没有JLabel,JLabel不是JTextComponents的成员
最安全的可能是在任何投射之前测试if (Xxx instanceof JTextComponent)
答案 1 :(得分:0)
找到我的问题的修复程序。目标Java版本是错误的。在build.xml
文件中,有以下几行:
<property name="target" value="1.5" />
<property name="source" value="1.5" />
将这些更改为1.7
,瞧!这似乎是一个Swing错误,已在较新版本的Java中得到修复。
答案 2 :(得分:0)
我注意到当内容周围带有标签的JLabel包含嵌套标签时,可能会出现此问题。解决方案是逃避
上的尖括号