11

我对把这个放在哪里有点困惑:

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

}

我没有扩展JFrame课程,但使用了JFrame f = new JFrame(); 谢谢:D

4

5 回答 5

13

最常见的放置位置是在您的静态 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

于 2012-03-13T10:13:43.733 回答
12

注意:这不是问题的答案(这是在哪里设置 LAF)。相反,它回答了如何以独立于其包名称的方式设置 LAF 的问题。在类被移动的情况下简化生活,如从 com.sun* 到 javax.swing 的 fi Nimbus。

基本方法是向 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); 
于 2012-03-13T11:46:07.523 回答
10

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

    UIManager.setLookAndFeel(lookModel.getLookAndFeels().get(getLookAndFeel()));
    for(Window window : JFrame.getWindows()) {
        SwingUtilities.updateComponentTreeUI(window);
    }
于 2013-02-26T08:34:21.933 回答
2

您可以在创建 JFrame 后将此块放在 main 方法中,或者放在扩展 JFrame 的类的构造函数中。


    try
    {
        //Set the required look and feel
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        //Update the component tree - associate the look and feel with the given frame.
        SwingUtilities.updateComponentTreeUI(frame);
    }//end try
    catch(Exception ex)
    {
        ex.printStackTrace();
    }//end catch

于 2013-07-08T07:42:55.420 回答
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);
    }
于 2015-07-06T08:56:29.220 回答