所以,我有一个问题,改变面板之间的间距(包含 JTextAreas
)添加面板时,请参见以下图片:d
示例
第一次按下按钮时 addTextArea()
被称为。状态1->图片中的状态2。问题是面板上的按钮与新添加的按钮不太接近 WorkDescription
( JTextArea
). 当按钮被多次按下时,它们之间的间隔就会改变。
按钮以前跳得很大,但是; c.weighty = 0.1 - 0.3
跳跃较小。
// The panel is placed in the center of a JFrame (BorderLayout)
public CONSTRUCTOR {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// ...
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 1;
c.gridheight = 1;
// this is how everything looks at (first pic) start.
panel.add(panel_buttons, c); // panel_buttons is at the right place
}
添加新属性的方法 WorkDescription
,即 JTextArea
:
public void addTextArea() {
WorkDescription wd = new WorkDescription(); //WorkDescription extends JPanel
panel.remove(panel_buttons);
c.weighty = 0.25; // I've messed around with the weighty alot.
// 0.2-0.25 makes the panel_buttons do the least amout of 'down-jump'
panel.add(wd, c);
if(c.gridy < 3 ) {
c.gridy ++;
c.weighty = 1;
panel.add(panel_buttons, c);
}
panel.revalidate();
panel.repaint();
}
1条答案
按热度按时间irtuqstp1#
我找到的最好办法是,
从
GridBagLayout
至GridLayout
.然后,当然,删除所有
GridBagConstraints
```public void addTextArea() {
}