2

我正在编写一个 Java gui 来模仿一些文书工作,特别是一种具有许多仅在运行时才知道的“行”的表单,并且这些行分为两部分(50%-50%,一个标签和一个条目) 或划分 25% 75%,(假设是一个数字和一个完整的句子)。

有人会认为已经说过(仅在此处显示显着的行,下面的代码更完整)

GridBagConstraints c = new GridBagConstraints();
...
c.gridx = 0;
c.gridwidth = 2;
...
mainPanel.add(l, c);

其次是:

c.gridx = 2;
c.gridwidth = 4;
...
mainPanel.add(l, c);

将“确定”在 x 方向上,面板分为 4,给我 50-50,这应该让我可以自由地这样做以获得我的 25%-75% 版本:

GridBagConstraints c = new GridBagConstraints();
...
c.gridx = 0;
c.gridwidth = 1;
...
mainPanel.add(l, c);

其次是:

c.gridx = 1;
c.gridwidth = 3;
...
mainPanel.add(l, c);

但我得到的是所有线都被划分为 50-50。我能够将行划分为 50-50,然后其他行不划分,这对于初步版本来说是可以的。

我是否误解了这里的工作方式?我在“类似问题”侧边栏中注意到这篇文章 (http://stackoverflow.com/questions/7509781/java-gridbaglayout-automated-construction) 推荐 MiG 布局,我会认真考虑。

相关代码如下,项目的其余部分是一个标准的空 NetBeans Java 桌面应用程序:

public class GridBagDemoView extends FrameView {
public GridBagDemoView(SingleFrameApplication app) {
    super(app);

    initComponents();

    // Reset the grid position
    nextGridY = 0;


    addLine_50_50("50", "50");
    addLine_50_50("50", "50");
    addLine_50_50("50", "50");
    addLine_50_50("50", "50");

    addLine_25_75("25", "75");
    addLine_25_75("25", "75");
    addLine_25_75("25", "75");
    addLine_25_75("25", "75");


    mainPanel.validate();
    mainPanel.revalidate();
    mainPanel.repaint();

}

private void addLine_50_50(String left, String right) {

    GridBagConstraints c = new GridBagConstraints();

    // "Universal" settings
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(5, 5, 5, 5);
    c.anchor = GridBagConstraints.NORTHWEST;
    c.gridheight = 1;
    c.weightx = 1;
    c.weighty = 1;

    // Settings for the labels (LHS of panel)
    JTextArea l = new JTextArea();
    l.setText(left);
    c.gridx = 0;
    c.gridy = nextGridY;
    c.gridwidth = 2;
    c.weightx = 1;
    mainPanel.add(l, c);

    // Settings for the text (RHS of panel)
    JTextArea ta = new JTextArea();
    ta.setText(right);
    c.gridx = 2;
    c.gridy = nextGridY;
    c.gridwidth = 2;
    c.weightx = 1;
    mainPanel.add(ta, c);

    // Increase row number of next line
    nextGridY++;
}

private void addLine_25_75(String left, String right) {

    GridBagConstraints c = new GridBagConstraints();

    // "Universal" settings
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(5, 5, 5, 5);
    c.anchor = GridBagConstraints.NORTHWEST;
    c.gridheight = 1;
    c.weighty = 1;

    // Settings for the labels (LHS of panel)
    JTextArea l = new JTextArea();
    l.setText(left);
    c.gridx = 0;
    c.gridy = nextGridY;
    c.gridwidth = 1;
    c.weightx = 1;
    mainPanel.add(l, c);

    // Settings for the lext (RHS of panel)

    JTextArea ta = new JTextArea();
    ta.setText(right);
    c.gridx = 1;
    c.gridy = nextGridY;
    c.gridwidth = 3;
    c.weightx = 1;
    mainPanel.add(ta, c);

    // Increase row number of next line
    nextGridY++;

}
Generated code etc...
4

2 回答 2

0

据我所知,网格包布局不会强制列的宽度相等。所以你得到的可能是你的第一列宽度为 50%,第二列和第三列的宽度为 0 或接近零,第四列又是 50%。

尝试制作一行,分别填充四列中的每一列。确保使用weightx选项。

于 2011-12-11T10:22:36.110 回答
0

给 GridBagLayout 一个关于单个单元格是什么的提示 - 只需为每个单个单元格添加一个空白区域作为第一行。

gbc.gridx = 0;
gbc.gridwidth = 1;
for (int i=0; i<4; i++)
{  
  container.add(Box.createRigidArea(new Dimension()), gbc);
  gbc.gridx ++;
} 

//now add your rows
gbc.gridx = 0;
...
于 2011-12-11T17:09:49.283 回答