Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GridBagLayout-意外添加/更改的间距_Java_Swing_Awt_Layout Manager_Gridbaglayout - Fatal编程技术网

Java GridBagLayout-意外添加/更改的间距

Java GridBagLayout-意外添加/更改的间距,java,swing,awt,layout-manager,gridbaglayout,Java,Swing,Awt,Layout Manager,Gridbaglayout,因此,我在添加面板时,面板之间的间距(包含JTextAreas)发生了变化,请参见以下内容:D 例子 第一次按下按钮时,调用addTextArea()。状态1->图片中的状态2。问题是面板按钮与新添加的WorkDescription(JTextArea)不太接近。当按钮被多次按下时,它们之间的间距会发生变化 按钮之前跳了一大步,但是有点小c.weighty=0.1-0.3跳跃较小 // The panel is placed in the center of a JFrame (BorderL

因此,我在添加面板时,面板之间的间距(包含
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();
}
public void addTextArea(){
WorkDescription wd=new WorkDescription();//WorkDescription扩展了JPanel
面板。移除(面板按钮);
c、 weighty=0.25;//我和weighty搞混了很多。
//0.2-0.25使面板按钮的“向下跳跃”最少
面板。添加(wd,c);
如果(c.gridy<3){
c、 gridy++;
c、 权重=1;
面板。添加(面板按钮,c);
}
panel.revalidate();
panel.repaint();
}

我找到的最佳解决方案是

GridBagLayout
切换到
GridBagLayout

private JPanel panel_Center = new JPanel(new GridLayout(5,1)); 
然后,当然,删除所有
GridBagConstraints

public void addTextArea() {

    WorkDescription wd = new WorkDescription();

    panel.remove(panel_buttons); 
    panel.add(wd);

    if(addedWorks < 4 ) {       

        addedWorks++;
        panel.add(panel_buttons);

    }

    panel.revalidate();
    panel.repaint();
}
public void addTextArea(){
WorkDescription wd=新的WorkDescription();
面板。移除(面板按钮);
面板。添加(wd);
if(addedWorks<4){
addedWorks++;
面板。添加(面板按钮);
}
panel.revalidate();
panel.repaint();
}

我找到的最佳解决方案是

GridBagLayout
切换到
GridBagLayout

private JPanel panel_Center = new JPanel(new GridLayout(5,1)); 
然后,当然,删除所有
GridBagConstraints

public void addTextArea() {

    WorkDescription wd = new WorkDescription();

    panel.remove(panel_buttons); 
    panel.add(wd);

    if(addedWorks < 4 ) {       

        addedWorks++;
        panel.add(panel_buttons);

    }

    panel.revalidate();
    panel.repaint();
}
public void addTextArea(){
WorkDescription wd=新的WorkDescription();
面板。移除(面板按钮);
面板。添加(wd);
if(addedWorks<4){
addedWorks++;
面板。添加(面板按钮);
}
panel.revalidate();
panel.repaint();
}

StackOverFlow是我的新手,我不太熟悉这方面的编程。。问题清楚吗?:)请编辑问题并使其生效。我现在更新了:数据布局约束(例如,
gridbagstraints
)并不意味着是“动态的”(至少90%的时间)。因此,避免在程序逻辑中使用它们。使用它们来构建“静态”UI。1)“我现在已经更新了”OK,但是请注意,现在的问题仍然没有您当前的尝试。如果我们不能在完全没有更改的情况下复制/粘贴编译/运行,那么它就不是MCVE。2) 通常,GUI是通过将用户认为是1个GUI的不同部分中的布局组合而实现的。与GUI的该部分相关的每个布局。没有“最佳布局”,只有“工作的最佳布局”。3) 以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更大的宽度和高度,以显示如何使用额外的空间。我对StackOverFlow和编程这方面比较陌生。。问题清楚吗?:)请编辑问题并使其生效。我现在更新了:数据布局约束(例如,
gridbagstraints
)并不意味着是“动态的”(至少90%的时间)。因此,避免在程序逻辑中使用它们。使用它们来构建“静态”UI。1)“我现在已经更新了”OK,但是请注意,现在的问题仍然没有您当前的尝试。如果我们不能在完全没有更改的情况下复制/粘贴编译/运行,那么它就不是MCVE。2) 通常,GUI是通过将用户认为是1个GUI的不同部分中的布局组合而实现的。与GUI的该部分相关的每个布局。没有“最佳布局”,只有“工作的最佳布局”。3) 以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更大的宽度和高度,以显示应如何使用额外的空间。