Pergunta

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?)

Foi útil?

Solução

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

Outras dicas

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();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top