如何设置jframe外观

Yay*_*tcm 9 java swing jframe look-and-feel uimanager

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

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

}
Run Code Online (Sandbox Code Playgroud)

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

kle*_*tra 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;
}
Run Code Online (Sandbox Code Playgroud)

用法(此处无异常处理)

String className = getLookAndFeelClassName("Nimbus");
UIManager.setLookAndFeel(className); 
Run Code Online (Sandbox Code Playgroud)


Byr*_*ach 11

放置它的最常见的地方就在你的静态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
}  
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,请访问该网站:http: //docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

  • 基本上是正确的,但不推荐用于Nimbus :)它在jdk6中的com.sun.*中开始了它的生命,确定被移入jdk7中的javax.swing.因此,不是对类名进行硬编码,而是查询UIManager以获取已安装的lookAndFeels并循环遍历它们,直到找到包含"Nimbus"的类为止 (2认同)

Joh*_*ier 9

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

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