Question

I am trying to update my look and feel without any errors, but I can't figure out what I am doing wrong.

This is my Window class:

public class Window extends JFrame {
    private static final long serialVersionUID = 1L;

    public Window() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new MigLayout());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setMinimumSize(new Dimension(600, 700));
        setVisible(true);

        setContentPane(new JPanel() {
            private static final long serialVersionUID = 1L;

            public void paintComponent(Graphics g) {
                g.drawImage(new ImageIcon("start.jpg").getImage(), 0, 0, getWidth(),     
                    getHeight(), this);
            }
        });
    }
}

And this is my main where I update the UI (Look and Feel)

public class Main {
    public static void main(String[] args) {
        Window.setDefaultLookAndFeelDecorated(true);
        try {
            UIManager.setLookAndFeel(new SubstanceGraphiteLookAndFeel());
        }
        catch (UnsupportedLookAndFeelException e) {}
        Window window = new Window();
    }
}

The console says my error comes from this line: Window window = new Window();

Then this line: setContentPane(new JPanel() {

But if I delete the whole setContentPane bloc, the error then points to the constructor.

Any help would be appreciated. Thank you!

Was it helpful?

Solution

  1. SubstanceGraphiteLookAndFeel() must be wrapped into invokeLater()

  2. Window window = new Window(); shold be wrapped into invokeLater(), more see in Oracle tutorial Initial Thread

  3. g.drawImage(new ImageIcon("start.jpg").getImage(), 0, 0, getWidth(), getHeight(), this);

    • use BufferedImage, load as local variable

    • paintComponent is invoked from every mouse, key events and internal methods implemented in APIs, then quite hard and long ImageIcon("start.jpg").getImage() is called repeatly, how many times any event is fired for repaint,

    • load to local variable

  4. setContentPane(new JPanel() {, override getMin/Max/PreferredSize

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top