4

I set my look and feel by doing this

private void setSubstanceSkin(String theme) {
    try {
        UIManager.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But I get this stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.plaf.metal.MetalRootPaneUI cannot be cast to org.jvnet.substance.SubstanceRootPaneUI
at org.jvnet.substance.utils.SubstanceCoreUtilities.getTitlePane(SubstanceCoreUtilities.java:3590)
at org.jvnet.substance.title.ImageWrapperHeaderPainter.paintExtraBackground(ImageWrapperHeaderPainter.java:172)
at org.jvnet.substance.SubstanceFillBackgroundDelegate.update(SubstanceFillBackgroundDelegate.java:198)
at org.jvnet.substance.SubstanceMenuBarUI.__org__jvnet__substance__SubstanceMenuBarUI__container__update(SubstanceMenuBarUI.java:251)
at org.jvnet.substance.SubstanceMenuBarUI.update(SubstanceMenuBarUI.java)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent.paint(JComponent.java:1040)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1967)
at java.awt.Window.paint(Window.java:3877)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
4

1 回答 1

5

Substance Look And Feel must be intialized on Event Dispatch Thread, inside invokeLater(), more to see in API

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(
               new SubstanceBusinessBlackSteelLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            throw new RuntimeException(e);
        }
    }
}); 

on runtime must be called SwingUtilities.updateComponentTreeUI(frame);, then JFrame should be as local variable

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(
               new SubstanceBusinessBlackSteelLookAndFeel());
            SwingUtilities.updateComponentTreeUI(frame);
        } catch (UnsupportedLookAndFeelException e) {
            throw new RuntimeException(e);
        }
    }
});
于 2013-07-13T06:40:30.453 回答