0

我们一直在办公室里苦苦挣扎,因为我们的一个(大型)小程序中存在一个看似无法解决的错误,这个小程序是 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版本。

谢谢。

4

3 回答 3

4

java.lang.ClassCastException:javax.swing.JLabel 无法转换为 javax.swing.text.JTextComponent

  • 查看JTextComponents的组件树,没有 JLabel,JLabel 不是 JTextComponents 的成员

  • 最安全的可能是if (Xxx instanceof JTextComponent)在任何铸造之前进行测试

于 2013-06-25T14:15:22.543 回答
0

我注意到,当内容周围带有标签的 JLabel 包含嵌套标签时,可能会出现此问题。解决方案是转义上的尖括号

于 2014-10-30T16:35:34.050 回答
0

找到了解决我的问题的方法。目标 Java 版本错误。在build.xml文件中,有以下几行:

<property name="target" value="1.5" />
<property name="source" value="1.5" />

将这些更改为1.7,瞧!这似乎是一个 Swing 错误,已在较新版本的 Java 中得到修复。

于 2013-07-01T12:57:09.620 回答