简体   繁体   English

如何对齐 JPanel 在另一个 JPanel 下方居中?

[英]How do I align a JPanel centered underneath another JPanel?

Hello你好

I have the following GUI that I'm working on.我正在使用以下 GUI。

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 . panel1包含titeltitelInput 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).目前,我将 panel1 的BorderLayout.NORTH设置为BorderLayout ,将panel1 panel2BorderLayout.CENTER以查看是否可以同时看到两者(实际上不是)。

As far as I know, panel2 overlays panel1 because BorderLayout is always centered in the JFrame even though there are two JPanels.据我所知, panel2覆盖panel1 ,因为即使有两个 JPanel, BorderLayout始终位于 JFrame 的中心。

I tried using FlowLayout but couldn't achieve the desired outcome.我尝试使用FlowLayout但无法达到预期的结果。

This is the GUI if both BorderLayouts are centered.如果两个 BorderLayouts 都居中,这就是 GUI。 panel2 covers panel1面板 2 覆盖面板 1

What I want to achieve is both JPanels to show up centered beneath eachother.我想要实现的是两个 JPanel 都显示在彼此下方。

Thanks in advance.提前致谢。

EDIT编辑

I ended up using the Box.createVerticalBox() Function instead and changed some little stuff aswell.我最终改用Box.createVerticalBox() Function 并更改了一些小东西。

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

The below changes in your code may give you the desired output.您的代码中的以下更改可能会为您提供所需的 output。

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

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.setLayout(new MigLayout());// Set layout

      // Add components
      mainFrame.add(menu, "north, w 100%");
      mainFrame.add(panel1, "center, wrap");
      mainFrame.add(panel2, "center");
      

      mainFrame.setVisible(true);
  }
}

You can Download the dependent library(miglayout15-swing.jar) from here您可以从这里下载依赖库(miglayout15-swing.jar)

Here's the solution provided in a comment这是评论中提供的解决方案

You add the JMenuBar to the JFrame menu bar with the setJMenuBar method.使用 setJMenuBar 方法将 JMenuBar 添加到 JFrame 菜单栏。 Then add your JPanels to the NORTH and CENTER.然后将您的 JPanel 添加到 NORTH 和 CENTER。 – Gilbert Le Blanc ——吉尔伯特·勒布朗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM