简体   繁体   English

如何设置jframe外观

[英]How to set jframe look and feel

I am kind of confused on where to put this : 我有点困惑在哪里把这个:

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

}

I did not extend the JFrame class but used JFrame f = new JFrame(); 我没有扩展JFrame类但使用了JFrame f = new JFrame(); Thanks :D 感谢:D

Note: this is not an answer to the question (which was where to set the LAF). 注意:这不是问题的答案(在哪里设置LAF)。 Instead, it's answering the question how-to set an LAF in a manner that's independent on its package name. 相反,它正在回答如何以独立于其包名的方式设置LAF的问题。 Simplifies life in case the class is moved, as fi Nimbus from com.sun* to javax.swing. 在类被移动的情况下简化生活,如从com.sun *到javax.swing的fi Nimbus。

The basic approach is to query the UIManager for its installed LAFs, loop through them until a match is found and set that. 基本方法是查询UIManager以获取其安装的LAF,循环遍历它们直到找到匹配并设置它。 Here'r such methods as implemented in SwingX: 这里有在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;
}

Usage (here without exception handling) 用法(此处无异常处理)

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

Most common place to put this, is right inside your static void main(String[] args) method. 放置它的最常见的地方就在你的静态void main(String [] args)方法中。 Like so: 像这样:

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

    new YourClass(); //start your application
}  

for more info look at this site: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 欲了解更多信息,请访问该网站: http//docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

UIManager.setLookAndFeel() will not work on components that are already created. UIManager.setLookAndFeel()不适用于已创建的组件。 Here is a good way to set the Look And Feel for every window in your application. 这是为应用程序中的每个窗口设置外观的好方法。 This will set it on all open Windows in your program. 这将在程序中的所有打开的Windows上设置它。 Any new windows created will use what was set by the UIManager. 创建的任何新窗口都将使用UIManager设置的内容。

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

You can put this block in your main method after you have created the JFrame, or in the constructor of a class that extends JFrame. 您可以在创建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
   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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM