如何设置JFrame的外观和感觉

11

我有些困惑在哪里放置这个:

try {
    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception e){

}

我没有继承JFrame类,而是使用了JFrame f = new JFrame();。谢谢 :D


以编程方式设置外观和感觉 - chicout
在初始化框架之前,请确保已配置外观。 - Martijn Courteaux
5个回答

15

最常见的地方是将它放在您的静态void main(String[] args)方法内部。就像这样:

public static void main(String[] args) {
    try { 
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
    } catch(Exception ignored){}

    new YourClass(); //start your application
}  

查看更多信息,请访问此网站: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html


2
基本上是正确的,但不建议在Nimbus中使用 :) 它在jdk6的com.sun.*中开始其生命周期,并确定会在jdk7中移动到javax.swing中。因此,不要硬编码类名,而是查询UIManager安装的lookAndFeels并循环遍历它们,直到找到包含“Nimbus”的类为止。 - kleopatra
说实话,我从来没有为我的Java程序使用任何外观。但如果我以后会用,我会使用你的代码片段!非常感谢。 - Byron Voorbach

12
注意:这不是对问题的回答(问题是在哪里设置LAF),而是回答如何以一种独立于包名的方式设置LAF。这样可以简化生活,万一类被移动,例如将Nimbus从com.sun*移到javax.swing中。 基本方法是查询UIManager安装的LAF,循环遍历它们直到找到匹配项并设置它。下面是在SwingX中实现这些方法的示例:
/**
 * Returns the class name of the installed LookAndFeel with a name
 * containing the name snippet or null if none found.
 * 
 * @param nameSnippet a snippet contained in the Laf's name
 * @return the class name if installed, or null
 */
public static String getLookAndFeelClassName(String nameSnippet) {
    LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
    for (LookAndFeelInfo info : plafs) {
        if (info.getName().contains(nameSnippet)) {
            return info.getClassName();
        }
    }
    return null;
}

使用方法(这里没有异常处理)

String className = getLookAndFeelClassName("Nimbus");
UIManager.setLookAndFeel(className); 

10

UIManager.setLookAndFeel()不适用于已经创建的组件。以下是一种设置应用程序中每个窗口外观的好方法。这将在程序中所有打开的窗口上设置它。任何新创建的窗口都将使用UIManager设置的外观。

    UIManager.setLookAndFeel(lookModel.getLookAndFeels().get(getLookAndFeel()));
    for(Window window : JFrame.getWindows()) {
        SwingUtilities.updateComponentTreeUI(window);
    }

1
谢谢,它运行得很好。SonarLint提到可以使用Window.getWindows()代替。 - Eric Duminil

2

您可以在创建JFrame后的主方法中,或者一个继承JFrame的类的构造函数中放置此块。


    try
    {
        // 设置所需的外观
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        // 更新组件树 - 将外观与给定的框架相关联。
        SwingUtilities.updateComponentTreeUI(frame);
    }//end try
    catch(Exception ex)
    {
        ex.printStackTrace();
    }//end catch


0
   try {
        for (javax.swing.UIManager.LookAndFeelInfo info :  javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
     } catch (ClassNotFoundException | InstantiationException || javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(  Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接