Java JPanel哪种布局

Java JPanel哪种布局,java,swing,layout-manager,Java,Swing,Layout Manager,假设我有一个框架,我想在其中创建6个面板,如下所示: 哪种布局最好?我试过这样的方法: public static void main ( String[] args ) { JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(800,600))

假设我有一个框架,我想在其中创建6个面板,如下所示:

哪种布局最好?我试过这样的方法:

public static void main ( String[] args ) {
        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(800,600));
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        JPanel leftTop = new JPanel();
        leftTop.setPreferredSize(new Dimension(251,300));
        leftTop.setBackground(Color.black);
        frame.getContentPane().add(leftTop);

        JPanel middleTop = new JPanel();
        middleTop.setPreferredSize(new Dimension(251,300));
        middleTop.setBackground(Color.white);
        frame.getContentPane().add(middleTop);

        JPanel rightTop = new JPanel();
        rightTop.setPreferredSize(new Dimension(251,300));
        rightTop.setBackground(Color.red);
        frame.getContentPane().add(rightTop);

        JPanel leftBottom = new JPanel();
        leftBottom.setPreferredSize(new Dimension(251,300));
        leftBottom.setBackground(Color.green);
        frame.getContentPane().add(leftBottom);

        JPanel middleBottom = new JPanel();
        middleBottom.setPreferredSize(new Dimension(251,300));
        middleBottom.setBackground(Color.yellow);
        frame.getContentPane().add(middleBottom);

        JPanel rightBottom = new JPanel();
        rightBottom.setPreferredSize(new Dimension(251,300));
        rightBottom.setBackground(Color.black);
        frame.getContentPane().add(rightBottom);

        frame.pack();
        frame.setVisible(true);
    }

但是如果我改变面板的大小,结果就不会这么好了哈哈。

假设你有一个JFrame,你想要6个JPanel,就像这样:

创建这个GUI的方法是分而治之

  • 您必须通过调用SwingUtilities invokeLater方法来启动Swing应用程序,以便将Swing组件的创建和执行放在服务器上。是的,即使对于小型测试程序也是如此

  • 我为左侧、中间和右侧创建了3个JPanel。每个JPanel都使用一个BorderLayout

  • 是的,我必须为6个内部JPanel指定一个首选尺寸。这是因为6个内部JPanel没有任何内部Swing组件。通常在Swing中,应该让Swing组件自行调整大小

  • 我将左、中、右JPanel封装在主JPanel中。通常,除了JPanel和JScrollPane之外,不应该向JFrame添加任何Swing组件。当你违反这条规则时,会发生无法解释的坏事。我宁愿发现并修复自己的编码错误,也不愿以一种不同寻常的方式使用Java类

  • 这是密码

    package com.ggl.testing;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class LayoutTest implements Runnable {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new LayoutTest());
        }
    
        @Override
        public void run() {
            JFrame frame = new JFrame("Layout Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel mainPanel = new JPanel();
            mainPanel.add(createLeftPanel());
            mainPanel.add(createCenterPanel());
            mainPanel.add(createRightPanel());
    
            frame.add(mainPanel);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        private JPanel createLeftPanel() {
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
    
            JPanel leftTop = new JPanel();
            leftTop.setPreferredSize(new Dimension(251, 250));
            leftTop.setBackground(Color.black);
            panel.add(leftTop, BorderLayout.NORTH);
    
            JPanel leftBottom = new JPanel();
            leftBottom.setPreferredSize(new Dimension(251, 350));
            leftBottom.setBackground(Color.green);
            panel.add(leftBottom, BorderLayout.SOUTH);
    
            return panel;
        }
    
        private JPanel createCenterPanel() {
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
    
            JPanel middleTop = new JPanel();
            middleTop.setPreferredSize(new Dimension(251, 400));
            middleTop.setBackground(Color.cyan);
            panel.add(middleTop, BorderLayout.NORTH);
    
            JPanel middleBottom = new JPanel();
            middleBottom.setPreferredSize(new Dimension(251, 200));
            middleBottom.setBackground(Color.yellow);
            panel.add(middleBottom, BorderLayout.SOUTH);
    
            return panel;
        }
    
        private JPanel createRightPanel() {
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
    
            JPanel rightTop = new JPanel();
            rightTop.setPreferredSize(new Dimension(251, 100));
            rightTop.setBackground(Color.red);
            panel.add(rightTop, BorderLayout.NORTH);
    
            JPanel rightBottom = new JPanel();
            rightBottom.setPreferredSize(new Dimension(251, 500));
            rightBottom.setBackground(Color.black);
            panel.add(rightBottom, BorderLayout.SOUTH);
    
            return panel;
        }
    
    }
    

    看看GridBagLayout:不需要选择布局。您可以嵌套布局;每列使用一个,一个包含列。(
    BoxLayout
    对两者都适用)。