簡體   English   中英

物質L&F不工作

[英]Substance L&F not working

我想在我的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);試了一下UIManager.setLookAndFeel(skin); 代替。 這仍然不起作用,但至少我現在得到一個例外:

org.pushingpixels.substance.api.UiThreadingViolationException:
必須在事件調度線程上進行狀態跟蹤

是不是通過invokeAndWait()調用它解決了?

編輯-2
好的,所以問題是不同的。 創建JTable時拋出異常,而不是在設置L&F時拋出異常。 通過EventQueue.invokeLater()調用JFrame構造函數(然后基本上運行整個應用程序),我能夠解決問題 - L&F現在正確呈現了。 但是之前我從來沒有這樣做過,用這種方式“保存”(用Java術語有效)嗎?

設置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..
            }
        });

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM