Issue when adding two JPanels

I’m trying to add two JPanels to a JFrame, one with a simple backround and another one with buttons etc. Either I get only buttons or only the background. I can’t find a solution to my problem anywhere, so any help would be appreciated. I’m still new to Java, so please don’t hate.

GuiMainMenu:

public class GuiMainMenu extends JFrame implements ActionListener, KeyListener {
    private static final long serialVersionUID = -7936366600070922227L;

    Color blue = new Color(114, 137, 218);
    Color gray = new Color(44, 47, 51);
    Color white = new Color(255, 255, 255);

    ImagePanel panel = new ImagePanel(new ImageIcon("image.png").getImage());

    public static int width;
    public static int height;

    JPanel p = new JPanel();
    JPanel p1 = new JPanel();
    JLabel l = new JLabel();
    JLabel l1 = new JLabel();
    JLabel l2 = new JLabel();
    JButton b = new JButton();
    JButton b1 = new JButton();
    JButton b2 = new JButton();
    String title = "-";

    
    public GuiMainMenu() {
        setSize(m.X, m.Y);
        setTitle(title);
        setResizable(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addKeyListener(this);

        p.setLayout(null);

        width = getWidth();
        height = getHeight();

        getRootPane().addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                width = getWidth();
                height = getHeight();

                addButtons();
                addLabels();
            }
        });

        addLabels();
        addButtons();
        
        add(p);
        add(panel);
        setVisible(true);
    }

    public void addLabels() {
        l.setSize(width, height);
        l.setLocation(5, -40);
        l.setText("x");
        l.setHorizontalAlignment(SwingConstants.LEFT);
        l.setVerticalAlignment(SwingConstants.BOTTOM);
        l.setForeground(blue);
        l.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));

        l1.setSize(width, height);
        l1.setLocation(-22, -40);
        l1.setText("y");
        l1.setHorizontalAlignment(SwingConstants.RIGHT);
        l1.setVerticalAlignment(SwingConstants.BOTTOM);
        l1.setForeground(blue);
        l1.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));

        l2.setText("test label");
        l2.setSize(width, height);
        l2.setLocation(0, -75);
        l2.setVerticalAlignment(SwingConstants.CENTER);
        l2.setHorizontalAlignment(SwingConstants.CENTER);
        l2.setForeground(blue);
        l2.setFont(new Font("Trebuchet MS", Font.BOLD, 26));

        p.add(l);
        p.add(l1);
        p.add(l2);
        validate();
    }

    public void addButtons() {
        b.setText("button0");
        b.setFocusable(false);
        b.setBorder(null);
        b.setLocation(width / 2 - 200, height / 2 - 35);
        b.setSize(400, 35);
        b.setForeground(white);
        b.setBackground(blue);
        b.addActionListener(this);

        b1.setText("button1");
        b1.setFocusable(false);
        b1.setBorder(null);
        b1.setLocation(width / 2 - 200, height / 2 + 10);
        b1.setSize(400, 35);
        b1.setForeground(white);
        b1.setBackground(blue);
        b1.addActionListener(this);

        p.add(b);
        p.add(b1);
        validate();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
    }
}

ImagePanel Class:

class ImagePanel extends JPanel {
    private static final long serialVersionUID = -7270956677693528549L;
    private Image img;

    public ImagePanel(String img) {
        this(new ImageIcon(img).getImage());
    }

    public ImagePanel(Image img) {
        this.img = img;
    }

    public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, GuiMainMenu.width, GuiMainMenu.height, null);
    }
}

Answer

p.setLayout(null); 

Don’t use a null layout. Swing was designed to be used with layout managers.

Read the section from the Swing tutorial on Layout Managers for more information and working examples.

Either I get only buttons or only the background

add(p);
add(panel);

The default layout manager for a JFrame is the BorderLayout. If you don’t specify a constraint both components get added to the CENTER. However only the last one added will be displayed.

Try the following to see the difference:

add(p, BorderLayout.PAGE_STRT);
add(panel, BorderLayout.CENTER);

one with a simple backround and another one with buttons etc.

However above is not what you want. Swing components have a parent/child relationship. So what you really need is:

backgroundPanel.add(buttonPanel);
add(backgroundPanel, BorderLayout.CENTER);

Note I used more meaningful names because “p” and “panel” and not descriptive. Use descriptive names for variable so people can understand what they mean.

Leave a Reply

Your email address will not be published. Required fields are marked *