Java 如何在1个JPanel内获得2个JPanel,可调整大小,固定比例?

Java 如何在1个JPanel内获得2个JPanel,可调整大小,固定比例?,java,swing,user-interface,layout-manager,Java,Swing,User Interface,Layout Manager,假设一个JFrame只包含1个JPanel。此JPanel分为2个JPanels,分别占据JFrame高度的0.75和0.25。我希望所有这些都可以随着窗口大小调整大小 我不知道如何在Java中实现这一点 我是Java的新手。我读过一些关于布局的书,但我能看到的只是如何在构造函数中设置首选大小(当达到这个数字时停止调整大小)或通过设置边框获得的一些固定大小。JFrame带有边框布局,在其上,添加一个JPanel带有网格布局。将其他两个面板添加到此面板上 有关详细信息,请参阅和 import ja

假设一个
JFrame
只包含1个
JPanel
。此
JPanel
分为2个
JPanel
s,分别占据
JFrame
高度的
0.75
0.25
。我希望所有这些都可以随着窗口大小调整大小

我不知道如何在Java中实现这一点


我是Java的新手。我读过一些关于布局的书,但我能看到的只是如何在构造函数中设置首选大小(当达到这个数字时停止调整大小)或通过设置边框获得的一些固定大小。

JFrame
带有
边框布局
,在其上,添加一个
JPanel
带有
网格布局
。将其他两个面板添加到此面板上

有关详细信息,请参阅和

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.weighty = 0.75;

            JPanel top = new JPanel(new GridBagLayout());
            top.add(new JLabel("Top"));
            top.setBackground(Color.RED);
            add(top, gbc);

            gbc.gridy++;
            gbc.weighty = 0.25;

            JPanel bottom = new JPanel(new GridBagLayout());
            bottom.add(new JLabel("Bottom"));
            bottom.setBackground(Color.BLUE);
            add(bottom, gbc);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}


JFrame
带有
BorderLayout
,在其上添加一个
JPanel
带有
gridbagloayout
。将其他两个面板添加到此面板上

有关详细信息,请参阅和

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.weighty = 0.75;

            JPanel top = new JPanel(new GridBagLayout());
            top.add(new JLabel("Top"));
            top.setBackground(Color.RED);
            add(top, gbc);

            gbc.gridy++;
            gbc.weighty = 0.25;

            JPanel bottom = new JPanel(new GridBagLayout());
            bottom.add(new JLabel("Bottom"));
            bottom.setBackground(Color.BLUE);
            add(bottom, gbc);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}


JFrame
BorderLayout
JPanel
gridbagloayout
的框架中。有关使用
BorderLayout
JFrame
、使用
gridbagloayout
的框架中的
JPanel
的更多详细信息,请参见和。有关详细信息,请参阅和