我想在我的Java应用程序中使用Substance L& F库,因此我下载了.jar文件并将它们添加到项目类路径中。然后我想在应用程序的main()
函数中设置L& F,如下所示:
SwingUtilities.invokeAndWait(new Runnable()
{
@Override
public void run()
{
try
{
// Substance
String skin = "org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel";
SubstanceLookAndFeel.setSkin(skin);
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
}
catch(Exception e)
{
System.err.println("Can't initialize specified look&feel");
e.printStackTrace();
}
}
});
这是在创建JFrame之前完成的。但是,即使没有抛出异常,也没有任何反应,GUI在默认的Swing L& F中呈现。
我在这里缺少什么想法?
修改
而不是SubstanceLookAndFeel.setSkin(skin);
来电,而是用UIManager.setLookAndFeel(skin);
代替它。这仍然不起作用,但至少我现在得到一个例外:
org.pushingpixels.substance.api.UiThreadingViolationException:
状态跟踪必须在事件调度线程
通过invokeAndWait()
调用此问题是不是解决了这个问题?
修改-2
好的,所以问题是不同的。创建JTable
时抛出异常,而不是在设置L& F时抛出异常。我能够通过JFrame
调用EventQueue.invokeLater()
构造函数(然后基本上运行整个应用程序)来解决问题 - L& F现在已正确呈现。但是之前我从来没有这样做过,用这种方式“保存”(用Java术语有效)吗?
答案 0 :(得分:3)
设置Substance LaF
时有一个小技巧。在致电UIManager.setLookAndFeel(new SubstanceGraphiteAquaLookAndFeel());
之前,您必须致电UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel");
。所以,设置如下:
public class App {
public static void main(String [] args) {
try {
UIManager.setLookAndFeel(new SubstanceGraphiteAquaLookAndFeel());
UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel");
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable(){
public void run() {
//Your GUI code goes here..
}
});
}
}