Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 摇摆屏设计,哪种布局?_Java_Swing_Layout - Fatal编程技术网

Java 摇摆屏设计,哪种布局?

Java 摇摆屏设计,哪种布局?,java,swing,layout,Java,Swing,Layout,我是swing的新手,我想设计一个我所附类型的屏幕。右大面板使用卡片布局,可在各种按钮点击时显示。我不知道如何安排组件,因为我有6列,随着我添加更多组件,行数也会增加。如果有人能告诉我需要使用哪种布局,或者如何使用的伪代码,我将不胜感激 非常感谢 更新:伙计们,我现在已经将我的解决方案移动到了每个页面上,以使用MigLayout。它非常简单,并且在放置动态组件时非常容易使用。非常感谢您宝贵的时间和回答。非常适合这一点:。值0将指定行/列可以随着添加更多组件而增长 Oracle的一个简短示例:

我是swing的新手,我想设计一个我所附类型的屏幕。右大面板使用卡片布局,可在各种按钮点击时显示。我不知道如何安排组件,因为我有6列,随着我添加更多组件,行数也会增加。如果有人能告诉我需要使用哪种布局,或者如何使用的伪代码,我将不胜感激

非常感谢

更新:伙计们,我现在已经将我的解决方案移动到了每个页面上,以使用MigLayout。它非常简单,并且在放置动态组件时非常容易使用。非常感谢您宝贵的时间和回答。

非常适合这一点:。值
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"));

UPADTE:

但是,如果您需要更灵活的网格布局,请参见Guillaume 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);


看一看或。前者可能比后者更容易使用。

尽管有些人觉得很难学习,但GridBagLayout是本地LayoutManager的最佳选择。如果您可以使用第三方LIB,MigLayout是另一种选择(应该更容易学习)。有时候,JTable也可以做得很好,如果你想要的是一个纯粹的表显示.Form-like layouts->JGoodies-FormLayoutGuys我的组件将被动态添加,那么这些布局支持吗?我的组件将被动态添加,那么这些布局会支持吗?当您尝试它时发生了什么?如果我没记错的话,JGoodies布局允许动态添加组件。您可能想编辑您的问题以强调这样的要求GridLayout有一个很大的不便之处:“单元格”大小对于所有“单元格”都是相等的,因此您不能利用额外的宽度(它将均匀分布)。很难说不知道“表”中的内容+1是的,这是真的,如果这是OP的意图,那么是的GridBagLayout就容易多了。更新answer@DavidKroukamp-我的组件将被动态添加,这些布局也将支持动态添加组件。@dareudream Yes AFAIK all
LayoutManager
s支持动态添加组件。请记住在组件上调用
revalidate()
和/或
repaint()
,以刷新布局以反映对布局所做的更改。我已经通过了GroupLayout,但我的所有组件都是动态添加的,因此此布局将支持该操作。