In Java, I want to set the Look and Feel of the program (specifically to the 'get system look and feel').

Is it necessary to set the look and feel inside each component (aka, each JPanel, each JFrame)?

Or is setting the look and feel once, enough to affect the whole application?

If once is enough, then where should I place the code to do so (in what class?)

有帮助吗?

解决方案

You define it in the main method and it will be used everywhere.

This is the main screen's main method:

Example:

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainScreen mainWindow = new MainScreen();
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                mainWindow.frame.setVisible(true);
                } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

其他提示

Or is setting the look and feel once, enough to affect the whole application?

Correct

If once is enough, then where should I place the code to do so (in what class?)

Usually, in a class which creates the application frame (before it is created):

    UIManager.setLookAndFeel(lookAndFeelName);
    ...
    JFrame frame = new JFrame(...);

Also, you can change LnF on the fly for the whole application, if you need:

 UIManager.setLookAndFeel(lookAndFeelName);
 SwingUtilities.updateComponentTreeUI(frame);
 frame.pack();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top