我要构建此布局:
由一个标签,一个文本字段,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);