摆动屏幕设计,哪个布局?

dar*_*eam 2 java layout swing

我是摇摆新手,我想设计一个我附加类型的屏幕.右侧大面板使用卡片布局,可以在各种按钮点击时显示.我不知道如何安排组件,我有6列,行数随着我添加更多组件而增长.如果有人可以让我知道我需要使用哪种布局或者需要如何处理它的伪代码,我将不胜感激.

万分感谢 !!!

在此输入图像描述

更新:伙计我现在已经在每个上移动我的解决方案以使用MigLayout.它非常简单,在动态组件放置的情况下非常容易使用.非常感谢您的价值时间和答案.

Dav*_*amp 5

GridLayout这是完美的:GridLayout(int rows,int cols).值0将指定在添加更多组件时行/列可以增长.

来自Oracle的简短示例:

在此输入图像描述

GridLayout experimentLayout = new GridLayout(0,2);//create grid any amount of rows and 2 coloumns

...

compsToExperiment.setLayout(experimentLayout);//add gridlayout to Component/JPanel

compsToExperiment.add(new JButton("Button 1"));
compsToExperiment.add(new JButton("Button 2"));
compsToExperiment.add(new JButton("Button 3"));
compsToExperiment.add(new JButton("Long-Named Button 4"));
compsToExperiment.add(new JButton("5"));
Run Code Online (Sandbox Code Playgroud)

UPADTE:

但是,如果您需要更灵活的网格布局,请参阅GridBagLayoutGuillaume Polet建议:

在此输入图像描述

正如您所看到的那样,每个组件使用超过1行/列的全部内容:

在此输入图像描述

JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
                //natural height, maximum width
                c.fill = GridBagConstraints.HORIZONTAL;
}




button = new JButton("Button 1");
if (shouldWeightX) {
                   c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);




button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);




button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);




button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;      //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);




button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;       //reset to default
c.weighty = 1.0;   //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0);  //top padding
c.gridx = 1;       //aligned with button 2
c.gridwidth = 2;   //2 columns wide
c.gridy = 2;       //third row
pane.add(button, c);
Run Code Online (Sandbox Code Playgroud)