JPanel:需要布局帮助

时间:2018-08-19 10:01:09

标签: java swing layout layout-manager gridbaglayout

我要构建此布局:

由一个标签,一个文本字段,2个按钮和一个可变高度的文本区域(从1行到40+行)组成。我试图使用GridBagLayout失败。按钮是彼此重叠的,我不知道如何设置每个元素的大小,并且它们之间没有空格。这是到目前为止的代码:

    GridBagLayout gridbag = new GridBagLayout();
    this.setLayout(gridbag);
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 4;
    gridbag.setConstraints(lblGuidelines, c);
    this.add(lblGuidelines);

    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 4;
    gridbag.setConstraints(txtNumberInput, c);
    this.add(txtNumberInput);

    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    c.weightx = 0.5;
    gridbag.setConstraints(btnCheck, c);
    this.add(btnCheck);

    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    c.weightx = 0.5;
    gridbag.setConstraints(btnClear, c);
    this.add(btnClear);

    c.gridx = 0;
    c.gridy = 3;
    c.gridwidth = 4;
    gridbag.setConstraints(textArea, c);
    this.add(textArea);

1 个答案:

答案 0 :(得分:1)

类似...

Something like

基本上,您需要将第二个按钮的MainWindow.Instannce.GamePiece.Add(new Piece()); 位置设置为gridx而不是1。另外,在列不存在的地方指定0毫无意义,这可能会引起问题,但是大多数情况下,这些列的虚拟宽度为gridwidth,因此不会占很大的比重< / p>

0