Skip to content
Advertisement

How do I align a JPanel centered underneath another JPanel?

I have the following GUI that I’m working on.

import javax.swing.*;
import java.awt.*;

public class gui{
    public static void main(String[] args) {
        //Neues Fenster mainFrame
        JFrame mainFrame = new JFrame("Lernjournal");

        //Schliessen des Fensters ermöglichen
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //mainFrame Fenstergrösse
        mainFrame.setSize(1280,720);

        //MenuBar erstellen
        JMenuBar menu = new JMenuBar();

        //Dropdown menu erstellen
        JMenu menu1 = new JMenu("Datei");
        JMenuItem neu = new JMenuItem("Neu");
        JMenuItem speichern = new JMenuItem("Speichern");
        JMenuItem eintrag = new JMenuItem("Einträge");

        //Einfügen der Menus
        menu.add(menu1);
        menu1.add(neu);
        menu1.add(speichern);
        menu1.add(eintrag);

        //Neues Panel erstellen
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        //Titel und Textfeld für Titel
        JLabel titel = new JLabel("Titel: ");
        JTextField titelInput = new JTextField(30);

        //Titel und Textfeld für das Ziel
        JLabel ziel = new JLabel("Zielvorstellung: ");
        JTextArea zielInput = new JTextArea(3,30);

        //JPanels dem JFrame hinzufügen
        mainFrame.getContentPane().add(BorderLayout.PAGE_START, menu);
        mainFrame.getContentPane().add(BorderLayout.NORTH, panel1);
        mainFrame.getContentPane().add(BorderLayout.CENTER, panel2);
        panel1.add(titel);
        panel1.add(titelInput);
        panel2.add(ziel);
        panel2.add(zielInput);

        mainFrame.setVisible(true);
    }
}

panel1 contains the titel and titelInput . At the moment I am setting the BorderLayout of panel1 to BorderLayout.NORTH and panel2 to BorderLayout.CENTER to see if it is possible to see both (which it is not).

As far as I know, panel2 overlays panel1 because BorderLayout is always centered in the JFrame even though there are two JPanels.

I tried using FlowLayout but couldn’t achieve the desired outcome.

This is the GUI if both BorderLayouts are centered. panel2 covers panel1

What I want to achieve is both JPanels to show up centered beneath eachother.

Thanks in advance.

EDIT

I ended up using the Box.createVerticalBox() Function instead and changed some little stuff aswell.

    import javax.swing.*;
import java.awt.*;
import java.sql.*;


public class gui{
    public static void main(String[] args) throws SQLException {

        //Neues Fenster mainFrame
        JFrame mainFrame = new JFrame("Lernjournal");

        //Schliessen des Fensters ermöglichen
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //mainFrame Fenstergrösse
        mainFrame.setSize(1280,720);

        //MenuBar erstellen
        JMenuBar menu = new JMenuBar();

        //Dropdown menu erstellen
        JMenu menu1 = new JMenu("Datei");
        JMenuItem neu = new JMenuItem("Neu");
        JMenuItem eintrag = new JMenuItem("Einträge");

        //Einfügen der Menus
        menu.add(menu1);
        menu1.add(neu);
        menu1.add(eintrag);

        //Neues Panel und neue Box erstellen
        Box box = Box.createVerticalBox();
        JPanel panel1 = new JPanel();
        panel1.add(box);
        
        //Titel und Textfeld für Titel
        JLabel titel = new JLabel("Titel");
        JTextField titelInput = new JTextField(30);

        //Titel und Textfeld für das Ziel
        JLabel ziel = new JLabel("Zielvorstellung");
        JTextArea zielInput = new JTextArea(3,30);

        //JPanels dem JFrame hinzufügen
        mainFrame.setJMenuBar(menu);
        mainFrame.getContentPane().add(BorderLayout.NORTH, panel1);

        //Box füllen
        box.add(titel);
        box.add(titelInput);
        box.add(ziel);
        box.add(zielInput);

        mainFrame.setVisible(true);
    }
}

Answer

Here’s the solution provided in a comment

You add the JMenuBar to the JFrame menu bar with the setJMenuBar method. Then add your JPanels to the NORTH and CENTER. – Gilbert Le Blanc

Advertisement