Java swing布局

cha*_*mae 5 java layout swing layout-manager

我很难确定要使用的布局.帮助任何建议.

在此输入图像描述

JPanel mainpanel = new JPanel();

public void addPanel(JPanel panel){
    mainpanel.add(panel);
}

addPanel(A);
addPanel(B);
addPanel(C);
addPanel(D);
addPanel(E);
....
Run Code Online (Sandbox Code Playgroud)

面板A,B,C,D ......的尺寸不固定.

我怎样才能做到这一点?

nIc*_*cOw 5

似乎可以通过使用GridBagLayout来实现。希望你可以改变JPanelMAGENTA颜色按自己的喜好:-)

这是输出:

网格袋布局测试

这是代码:

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

 public class GridBagLayoutTest
 {


    //defining the constructor
    public GridBagLayoutTest() 
    {

        JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
        maFrame.setLocationByPlatform(true); //centering frame

        JPanel headPanel = new JPanel(); //creating the header panel

        Container container = maFrame.getContentPane();
        container.setLayout(new GridBagLayout()); //setting layout of main frame
        GridBagConstraints cns = new GridBagConstraints(); //creating constraint
        cns.gridx = 0;
        cns.gridy = 0;
        //cns.gridwidth = 3;
        //cns.gridheight = 4;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.FIRST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;        
        headPanel.setBackground(Color.BLUE);
        container.add(headPanel, cns);

        JPanel panel = new JPanel();
        panel.setBackground(Color.CYAN);
        cns = new GridBagConstraints();
        cns.gridx = 1;
        cns.gridy = 0;
        //cns.gridwidth = 7;
        //cns.gridheight = 4;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.FIRST_LINE_END;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel, cns);

        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.RED);
        cns = new GridBagConstraints();
        cns.gridx = 0;
        cns.gridy = 1;
        //cns.gridwidth = 2;
        cns.gridheight = 2;
        cns.weightx = 0.5;
        cns.weighty = 0.3;
        cns.anchor = GridBagConstraints.LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel1, cns);     

        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.PINK);
        cns = new GridBagConstraints();
        cns.gridx = 1;
        cns.gridy = 1;
        //cns.gridwidth = 2;
        //cns.gridheight = 2;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.LINE_END;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel2, cns);                 

        JPanel panel4 = new JPanel();
        panel4.setBackground(Color.ORANGE);
        cns = new GridBagConstraints();
        cns.gridx = 1;
        cns.gridy = 2;
        //cns.gridwidth = 2;
        //cns.gridheight = 2;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.LINE_END;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel4, cns); 

        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setLayout(new GridBagLayout());
        cns = new GridBagConstraints();
        cns.gridx = 0;
        cns.gridy = 4;
        cns.gridwidth = 2;
        cns.gridheight = 2;
        cns.weightx = 1.0;
        cns.weighty = 0.3;
        cns.anchor = GridBagConstraints.LAST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        container.add(mainPanel, cns); 

        JPanel panel3 = new JPanel();
        panel3.setBackground(Color.MAGENTA);
        cns = new GridBagConstraints();
        cns.gridx = 0;
        cns.gridy = 0;
        //cns.gridwidth = 2;
        //cns.gridheight = 2;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.FIRST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        mainPanel.add(panel3, cns); 

        JPanel bottomPanel = new JPanel();
        bottomPanel.setBackground(Color.WHITE);
        cns = new GridBagConstraints();
        cns.gridx = 0;
        cns.gridy = 1;
        //cns.gridwidth = 2;
        //cns.gridheight = 2;
        cns.weightx = 1.0;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.LAST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        mainPanel.add(bottomPanel, cns); 

        //JButton button = new JButton("BUTTON");
        //headPanel.add(button);

        maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
        maFrame.pack();
        maFrame.setVisible(true); //making the frame visible
    }

    //defining the main method
    public static void main(String[] args) 
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                new GridBagLayoutTest(); //instantiating the class
            }
        };
        SwingUtilities.invokeLater(runnable);       
    }
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*yer 2

我想说的是,为 ACF 创建一个临时面板,然后为 BDE 创建一个临时面板,然后创建一个组合 ACF 和 BDE,然后使用 BorderLayout NORTH 和 SOUTH 将最后一个临时面板和主面板添加到框架上