尝试在netbeans中应用和运行物质主题时出现异常

时间:2013-12-15 03:44:34

标签: java swing look-and-feel event-dispatch-thread substance

我在我的swing应用程序中添加了物质主题,并尝试运行它,并且它有效。但是在运行它时我一直遇到这个异常。有谁能告诉我如何解决它?

异常.............................................. .......................................

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.jvnet.substance.SubstanceTableUI$TableStateListener$2.run(SubstanceTableUI.java:1951)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    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)

代码.............................................. .........................................

   try {
        LookAndFeel look = new SubstanceDefaultLookAndFeel();
        UIManager.setLookAndFeel(look);
        SubstanceDefaultLookAndFeel.setCurrentTheme(new SubstanceBottleGreenTheme());
        SubstanceDefaultLookAndFeel.setCurrentWatermark(new SubstanceNoneWatermark());
        SubstanceDefaultLookAndFeel.setCurrentGradientPainter(new SimplisticSoftBorderReverseGradientPainter());
        new Login().setVisible(true);
   } catch (Exception e) {
        e.printStackTrace();
   }

1 个答案:

答案 0 :(得分:1)

确保在事件派发线程上明确创建GUI。当我没有这样做时,我遇到了一些外观和感觉问题。您可以通过在EDT上排队启动代码来实现此目的

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    // start your GUI here
  }
});

即,

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    try {
        LookAndFeel look = new SubstanceDefaultLookAndFeel();
        UIManager.setLookAndFeel(look);
        SubstanceDefaultLookAndFeel.setCurrentTheme(new SubstanceBottleGreenTheme());
        SubstanceDefaultLookAndFeel.setCurrentWatermark(new SubstanceNoneWatermark());
        SubstanceDefaultLookAndFeel.setCurrentGradientPainter(
           new SimplisticSoftBorderReverseGradientPainter());
        new Login().setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

编辑,mKorbel是对的 - 外观和感觉应该在EDT上改变。代码已更改以反映此情况。对于错误信息感到抱歉。