gpt4 book ai didi

Java Swing 布局

转载 作者:搜寻专家 更新时间:2023-10-31 08:20:45 25 4
gpt4 key购买 nike

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

enter image description here

JPanel mainpanel = new JPanel();

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

addPanel(A);
addPanel(B);
addPanel(C);
addPanel(D);
addPanel(E);
....

面板 A、B、C、D... 的大小不固定。

我怎样才能做到这一点?

最佳答案

似乎是通过使用 GridBagLayout ,你可以做到这一点。希望您可以根据自己的喜好将 JPanel 更改为 MAGENTA 颜色:-)

这是输出:

GRIDBAG LAYOUT TEST

这里是代码:

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

关于Java Swing 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9660251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com