5

对于需要显示图像中显示的组件的 JPanel,最好的方法/LayoutManager 是什么?(请注意,当窗口大小改变时,字段应该水平调整大小,而不是标签)。

我目前正在尝试(并且正在努力)使用 GridBagLayout 来做到这一点。这是正确的方法还是有更好的方法?我是否应该尝试划分+征服单独的部分?

任何想法或建议将不胜感激。:-)

http://img261.imageshack.us/img261/2091/layouthb6.png

PS如果你读到这里没有图片,请耐心等待我修复它!

4

13 回答 13

11

I would look at nesting JPanels. It would be very easy to make each row a separate JPanel and then just stack the JPanels.

Otherwise you can use the GridBagLayout. That is the one I use for more control over where components are placed.

Using GridBagLayout you can layout a row by incrementing the GridBagConstraint.gridx and then resetting GridBagConstraint.gridx = 0 and GridBagConstraint.gridy++ to move to the next row.

于 2009-01-09T14:52:11.317 回答
4

I would suggest a third party free layout manager. MigLayout comes to mind (check the example form at the bottom of the linked page), or JGoodies FormLayout, which is especially good at formslike these.

The standard layout managers are good, but whoefully inadequate for frequent use for screens like these (the new GroupLayout used by the Matisse designer in Netbeans wasn't built for nothing). The linked layout managers are a lot easier to use and maintain IMHO.

于 2009-01-09T15:17:15.353 回答
2

我肯定会使用DesignGridLayout

DesignGridLayout layout = new DesignGridLayout(this);
layout.row().grid(label0).add(field0);
layout.emptyRow();
layout.row().grid(label1).add(field1);
layout.row().grid(label2).add(field2);
layout.row().grid(label3).add(field3);
layout.row().grid(label4).add(field4);
layout.emptyRow();
layout.row().grid(label5).add(field5).grid(label6).add(field6);
layout.row().grid(label7).add(field7).grid(label8).add(field8);
layout.emptyRow();
layout.row().grid(label9).add(field9);

免责声明:我是 DesignGridLayout 的作者之一。

于 2009-01-12T06:55:42.523 回答
1

If you can use external code, take a look at the JGoodies layout managers.

Personally, I find GridBagLayout to be more trouble than it's worth -- to the extent that I've written my own layout managers to avoid it.

于 2009-01-09T15:11:55.113 回答
1

I personally prefer GroupLayout.

Even if you're not using a GUI builder you can use the layout by hand and it's fairly simple and straightforward to work with if you understand the concepts.

于 2009-01-09T15:14:12.177 回答
0

TableLayout 将是我的选择,您可以使用诸如 TableLayout.FILL 之类的常量来自动调整大小并指定固定的像素宽度。

表格布局

于 2009-01-09T15:28:18.683 回答
0

您的布局看起来基本上是由“线条”组成的。所以就个人而言,我会:

  • 使用 Box.createVerticalBox() 创建一个垂直框;这将垂直堆叠其组件-实际上,让您添加行
  • 为每一行创建一个面板:我认为您可以使用带有 BorderLayout 的 JPanel (以及左侧的标签,文本字段作为中心组件),或者再次使用 Box.createHorizo​​ntalBox(),然后向其中添加标签和文本字段, 具有适当的最小/首选大小
  • 您可以在需要行分隔符的垂直框中添加“支柱”
  • 带有两组标签/字段的行需要一些特殊处理,但它本质上只是一个水平框,里面有两条“行”。

就个人而言,我从来没有用驳船杆接触过 GridBagLayout。也许只有我一个人,但我发现并发算法和机器代码编程比理解 GridBagLayout 更容易......

于 2009-01-09T15:55:51.110 回答
0

GridBagLayout, 当然。

如果表格只是这样,那么您确实需要多个JPanel. (如果您有按钮,您可能还需要一个,具体取决于它们的位置/方式)

字段 0 - 4 和 9 将gridwidth设置为 4,其他为默认值(1,但您无需将其设置为 1)

字段组之间的“空间”可以通过插入来实现。

(至少对于这个布局)我不建议第三方布局管理器。GridBagLayout并不复杂。只需要(一点点)时间来适应它。

Visual Editor + Eclipse 可以轻松为您完成这些工作。(我对 Netbeans 及其 Swing 开发不是很满意。尝试过,但从未爱上。可能是我太沉迷于 Eclipse)

于 2009-01-10T21:17:22.943 回答
0

从 Java SE 6 开始,您可以使用GroupLayout

这很容易,看起来就像你想要的那样。

对于这个窗口:

替代文字 http://img165.imageshack.us/img165/8237/sampleusinggrouplayoutca2.png

我有这个代码:

    // Layout the components using the new GroupLayout added
    // in java 1.6.
    // Adding to the main frame
    private void layoutComponents(){
        JPanel panel = new JPanel();

        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        layout.setAutoCreateGaps(true);         
        layout.setAutoCreateContainerGaps(true);

        SequentialGroup hGroup = layout.createSequentialGroup();

        JLabel nameLbl  = new JLabel("Name");
        JLabel countLbl = new JLabel("Amount");
        JLabel dateLbl  = new JLabel("Date(dd/MM/yy)");
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(nameLbl).
                addComponent(countLbl).
                addComponent(dateLbl).
                addComponent(go));

        hGroup.addGroup(layout.createParallelGroup().
                addComponent(name).
                addComponent(count).
                addComponent(date));

        layout.setHorizontalGroup(hGroup);

        SequentialGroup vGroup = layout.createSequentialGroup();

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(nameLbl).
                addComponent(name));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(countLbl).
                addComponent(count));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(dateLbl).
                addComponent(date));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(go));
        layout.setVerticalGroup(vGroup);

        frame.add( panel , BorderLayout.NORTH );
        frame.add( new JScrollPane( textArea ) );
    }
于 2009-01-10T16:06:45.517 回答
0

我认为,在所有标准布局中,GridBagLayout 应该最适合您的需要。

您可以尝试类似以下示例,其中我使用 addToLayout 将复杂的 GridBagConstraints 构造函数简化为我真正需要的。

但是,如果您可以使用 IDE,这确实可以使事情变得更容易(例如 NetBeans 中的 Matisse)。

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagLayoutTest extends JPanel {

    public GridBagLayoutTest() {
        setLayout(new GridBagLayout());
        addToLayout(new JLabel(     "Label 0" ), 0, 0, 0d, 1);
        addToLayout(new JTextField( "Field 0" ), 0, 1, 1d, 4);
        addToLayout(new JLabel(     "*"       ), 0, 5, 0d, 1);
        addToLayout(new JPanel()               , 1, 0, 0d, 1);
        addToLayout(new JLabel(     "Label 1" ), 2, 0, 0d, 1);
        addToLayout(new JTextField( "Field 1" ), 2, 1, 1d, 4);
        addToLayout(new JLabel(     "*"       ), 2, 5, 0d, 1);
        addToLayout(new JLabel(     "Label 2" ), 3, 0, 0d, 1);
        addToLayout(new JTextField( "Field 2" ), 3, 1, 1d, 1);
        addToLayout(new JLabel(     "*"       ), 3, 2, 0d, 1);
        addToLayout(new JLabel(     "Label 3" ), 3, 3, 0d, 1);
        addToLayout(new JTextField( "Field 3" ), 3, 4, 1d, 1);
        addToLayout(new JLabel(     "*"       ), 3, 5, 0d, 1);
    }

    private void addToLayout(java.awt.Component comp, int y, int x, double weightx, int gridwidth) {
        add(comp, new GridBagConstraints(x, y, gridwidth, 1, weightx, 0d,
                                         GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                                         new java.awt.Insets(2, 4, 2, 4), 0, 0 ) );
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new GridBagLayoutTest());
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
}
于 2009-01-23T17:17:35.467 回答
0

您可以将布局描述为 HTML 表格,然后使用此工具: http ://www.onyxbits.de/content/blog/patrick/java-gui-building-gridbaglayout-manager-made-easy

将布局转换为 Java 代码以配置 GridBagLayout。

于 2009-04-16T22:38:15.680 回答
0

在 Netbeans 推出他们的 gui builder 之前,我曾经真的很讨厌做 Swing 布局。你可以在这里看到一个演示。http://rghosting.co.cc

于 2009-01-10T13:21:57.240 回答
0

GridBagLayout绝对是这方面的最佳选择。而NetBeans Matisse可能是实现这一目标的最快方法。如果您使用的是其他 IDE,只需复制并粘贴自动生成的代码。

于 2009-01-10T16:37:04.770 回答