Z
Z
zevilcube2019-09-02 22:30:56
Java
zevilcube, 2019-09-02 22:30:56

How to display one component on top of another in Java Swing?

There is a window:

class Window extends JFrame {

    int width = 1500;
    int height = 750;

    private JPanel mainPanel;
    private JPanel mapPanel;
    private JPanel pausePanel;

    Window() {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLayout(null);
        setSize(width + 5, height + 28);
        setResizable(false);
        setLocation((screenSize.width-getWidth())/2, (screenSize.height-getHeight())/2);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        mainPanel = new MainPanel();
        mapPanel = new MapPanel();
        pausePanel = new PausePanel();

        add(mainPanel);
        mainPanel.add(mapPanel);
        mainPanel.add(pausePanel);

        repaint();
    }

    void showPauseMenu () {
        pausePanel.setVisible(true);
    }

    void hidePauseMenu () {
        pausePanel.setVisible(false);
    }
}


In another piece of code, a call to showPauseMenu is registered when pressing Escape. What needs to be added to it so that the pausePanel is displayed on top of the mapPanel and not below it?

PS All panels - JPanel

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question