【问题标题】:UIManager setLookAndFeel is not workingUIManager setLookAndFeel 不工作
【发布时间】:2014-03-13 07:13:33
【问题描述】:

我尝试使用UIManager.setLookAndFeel 函数来更改我的Java 应用程序的外观,但它对我的JFrame/按钮的视觉外观没有影响。我使用Netbeans 7.4,我的应用程序是Java Swing Desktop Application

     /**
     * Main method launching the application.
     */
    public static void main(String[] args) {     
        try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        } catch (Exception e) { 
            System.out.println(e.getMessage());
        }
        System.out.println("init");
        launch(JavaMysqlTestApp.class, args);        
    }

我用过

  • com.sun.java.swing.plaf.motif.MotifLookAndFeel
  • javax.swing.plaf.metal.MetalLookAndFeel
  • com.sun.java.swing.plaf.gtk.GTKLookAndFeel
  • UIManager.getSystemLookAndFeelClassName()
  • UIManager.getCrossPlatformLookAndFeelClassName()

但它们都没有改变我公司的外观。如何让这个功能生效?

【问题讨论】:

  • launch(JavaMysqlTestApp.class, args); 是否启动了另一个应用程序?
  • 它是java desktop application 而不是java application。所以confution来自我认为的那件事。现在我在@Override protected void startup() 函数中使用了UIManager.setLookAndFeel 方法,现在它可以工作了!

标签: java swing look-and-feel uimanager


【解决方案1】:

可以检查所有外观:

    try {
        LookAndFeel laf = UIManager.getLookAndFeel();
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            ...
            if (laf.getName().equals(info.getName())) {
                ...;
            }
        }
    } catch (ClassNotFoundException | InstantiationException
            | IllegalAccessException | UnsupportedLookAndFeelException ex) {
        Logger.getLogger(NewJFrame1.class.getName()).log(Level.SEVERE, null, ex);
    }

可能外观是第二次设置。对于发现此问题的初学者:L&F 仅适用于 swing;如果使用 awt 按钮(而不是 JButton)或 JavaFX,您也不会看到任何变化。

你可以这样做:

UIManager.addPropertyChangeListener(...);

如果有任何变化,请在之后进行更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多