How to set jframe look and feel

天大地大妈咪最大 提交于 2019-11-27 06:07:54

问题


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(); Thanks :D


回答1:


Most common place to put this, is right inside your static void main(String[] args) method. 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




回答2:


Note: this is not an answer to the question (which was where to set the LAF). Instead, it's answering the question how-to set an LAF in a manner that's independent on its package name. Simplifies life in case the class is moved, as f.i. Nimbus from com.sun* to javax.swing.

The basic approach is to query the UIManager for its installed LAFs, loop through them until a match is found and set that. Here'r such methods as implemented in 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); 



回答3:


UIManager.setLookAndFeel() will not work on components that are already created. 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. Any new windows created will use what was set by the UIManager.

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



回答4:


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.


    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




回答5:


   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);
    }


来源:https://stackoverflow.com/questions/9541045/how-to-set-jframe-look-and-feel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!