/**
* I'd like to achive following layout:
* +----------+----------+
* | Button 1 | |
* +----------| Button 2 |
* | Button 3 | |
* +----------+----------+
* with following code:
*/
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;
button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
panel.add(button, c);
button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 2;
panel.add(button, c);
button = new JButton("Button 3");
c.gridwidth = 1;
panel.add(button, c);
/**
* but what I achive is:
* +----------+----------+
* | Button 1 | Button 2 |
* +----------+----------+|
* | Button 3 |
* +----------+
*/
/**
* However layout:
* +----------+----------+
* | | Button 2 |
* + Button 1 +----------+
* | | Button 3 |
* +----------+----------+
* is easily achieved as:
*/
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;
button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 2;
panel.add(button, c);
button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
panel.add(button, c);
button = new JButton("Button 3");
panel.add(button, c);
任何线索?
问候,Francesc
答案 0 :(得分:1)
您需要指定gridx和gridy,默认值为0。
button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
panel.add(button, c);
button = new JButton("Button 3");
c.gridy = 1;
c.gridwidth = 1;
panel.add(button, c);
button = new JButton("Button 2");
c.gridx = 1; c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.weighty = 1;c.gridheight = 2;
panel.add(button, c);
答案 1 :(得分:1)
您必须为最后一个按钮设置c.gridx。默认情况下,组件相对于彼此放置。如果为按钮3将gridx设置为0,则强制它到第一列。
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;
button = new JButton("Button 1");
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
panel.add(button, c);
button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = GridBagConstraints.REMAINDER;
panel.add(button, c);
button = new JButton("Button 3");
c.gridx = 0;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button, c);
答案 2 :(得分:1)
为什么不使用使用GridLayouts的嵌套JPanel,例如:
import java.awt.GridLayout;
import javax.swing.*;
public class GridLayoutEg {
public static void main(String[] args) {
JPanel leftPanel = new JPanel(new GridLayout(0, 1));
leftPanel.add(new JButton("Button 1"));
leftPanel.add(new JButton("Button 3"));
JPanel mainPanel = new JPanel(new GridLayout(1, 0));
mainPanel.add(leftPanel);
mainPanel.add(new JButton("Button 2"));
JFrame frame = new JFrame("Foo");
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
或者我们需要知道的故事还有更多吗?
答案 3 :(得分:0)
您需要在约束上设置gridx和gridy。查看本教程:http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
答案 4 :(得分:0)
您尚未为元素指定gridx和gridy设置。
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;
button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
panel.add(button, c);
button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 2;
c.gridx = 1;
c.gridy = 0;
panel.add(button, c);
button = new JButton("Button 3");
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
panel.add(button, c);
注意:gridx和gridy的默认值为0,但最好始终设置该值。它更清晰,依赖于默认值可能会在复制和粘贴等时出现问题。因此这种方式更清晰,更安全(尽管稍微冗长一点)。
答案 5 :(得分:0)
GridBagLayout被认为是有效的(参见http://www.horstmann.com/articles/Taming_the_GridBagLayout.html):
标准布局管理者 Java SDK,GridBagLayout就是 最有用的,但它的复杂性 也被称为恐惧 程序员的心。的一部分 复杂性在于单调乏味 设置网格包约束。
我会阅读这些文章并在论坛上提问(例如http://www.java-forums.org/new-java/28857-gridbaglayout-problems-questions.html)
答案 6 :(得分:0)
是的,其他人都是对的。您必须手动设置GridX和GridY。
此外,我建议为每个组件使用一个新的GridBagConstraints
实例,因为在更复杂的表单中,您可以轻松地忽略在您正在使用的一个实例中实际设置的值。此外,如果您使用具有许多参数的构造函数,则不必关心默认值,因为您必须每次都指定它们。它需要时间来适应它,但它会在以后回报。
/**
* +----------+----------+
* | Button 1 | |
* +----------| Button 2 |
* | Button 3 | |
* +----------+----------+
*/
JPanel panel = new JPanel(new GridBagLayout());
JButton b1 = new JButton("Button 1");
panel.add(b1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
0), 0, 0));
JButton b2 = new JButton("Button 2");
panel.add(b2, new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
0), 0, 0));
JButton b3 = new JButton("Button 3");
panel.add(b3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
0), 0, 0));