我有一個Swing應用程序Nimbus l & f。有時我得到這個異常,但我不知道它被觸發的地方以及如何避免它,因爲所有源都來自跟蹤堆棧中的Java庫類。這是一個Java問題或我的問題?如何避免這種ClassCastException?
我的系統的一些信息:
avaVersion = 1.8.0_20
OSNAME = Linux的
OSVERSION = 3.15.10-201.fc20.x86_64
異常消息:
java.lang.ClassCastException: java.awt.Font cannot be cast to javax.swing.Painter
javax.swing.plaf.nimbus.NimbusStyle.getBackgroundPainter(NimbusStyle.java:708)
javax.swing.plaf.nimbus.SynthPainterImpl.paintBackground(SynthPainterImpl.java:99)
javax.swing.plaf.nimbus.SynthPainterImpl.paintPanelBackground(SynthPainterImpl.java:957)
javax.swing.plaf.synth.SynthPanelUI.update(SynthPanelUI.java:154)
javax.swing.JComponent.paintComponent(JComponent.java:777)
javax.swing.JComponent.paint(JComponent.java:1053)
javax.swing.JComponent.paintToOffscreen(JComponent.java:5223)
javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:290)
javax.swing.RepaintManager.paint(RepaintManager.java:1265)
javax.swing.JComponent._paintImmediately(JComponent.java:5171)
javax.swing.JComponent.paintImmediately(JComponent.java:4982)
javax.swing.RepaintManager$4.run(RepaintManager.java:824)
javax.swing.RepaintManager$4.run(RepaintManager.java:807)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:807)
javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:782)
javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:731)
javax.swing.RepaintManager.access$1300(RepaintManager.java:64)
javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1720)
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
java.awt.EventQueue.access$400(EventQueue.java:97)
java.awt.EventQueue$3.run(EventQueue.java:697)
java.awt.EventQueue$3.run(EventQueue.java:691)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.awt.Font cannot be cast to javax.swing.Painter
編輯:
如果一致性和經常發生,我也許能弄明白。
我也不可能嘗試將字體明顯地轉換爲畫家。
EDIT2:
完整的異常堆棧在這裏複製。
EDIT3
應用GUI部分內SwingUtilities.invokeAndWait開始()。
我以前也使用過我的Win7機器,但從未在Win7上使用過。此應用程序已存在1年。
發表[mcve] ... – Reimeus
如果您不嘗試將「Font」投射到「Painter」,則可以避免此異常。 – dasblinkenlight
您有一個間歇性錯誤,強烈暗示併發或線程問題。您是否在Swing事件線程上啓動GUI?一定要做到這一點,特別是一些外觀和感覺,如Nimbus。換句話說 - 在Runnable中創建GUI,並使用SwingUtilities.invokeLater(...)將它們排隊到Swing事件隊列中。 –