Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/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 gridheight将组件移动到彼此的顶部_Java_Swing_User Interface_Layout_Gridbaglayout - Fatal编程技术网

Java gridheight将组件移动到彼此的顶部

Java gridheight将组件移动到彼此的顶部,java,swing,user-interface,layout,gridbaglayout,Java,Swing,User Interface,Layout,Gridbaglayout,我有一个JFrame,它主要由左侧和右侧组成。左边有两个组件,一个在另一个上面,右边有9个,同样一个在另一个上面。由于左边有2个,我希望1个组件与右边的6个组件的垂直空间相等。我正在使用gridBagConstraints布局,并将每个JPanel放置在主JFrame中。一切看起来都很好(除了我刚才说的)。为了对这个问题进行排序,我在左边的JPanel上使用了c.gridHeight=6。但是,这会将6个jpanel放在彼此的右侧,忽略它们的格式设置。我怎样才能纠正这个问题?仍然导致相同问题的代

我有一个
JFrame
,它主要由左侧和右侧组成。左边有两个组件,一个在另一个上面,右边有9个,同样一个在另一个上面。由于左边有2个,我希望1个组件与右边的6个组件的垂直空间相等。我正在使用
gridBagConstraints
布局,并将每个
JPanel
放置在主
JFrame
中。一切看起来都很好(除了我刚才说的)。为了对这个问题进行排序,我在左边的JPanel上使用了
c.gridHeight=6
。但是,这会将6个
jpanel
放在彼此的右侧,忽略它们的格式设置。我怎样才能纠正这个问题?仍然导致相同问题的代码片段有:

void create()
{
    JFrame screen = new JFrame("Dungeon of Doom");
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    screen.setSize(new Dimension(screenWidth, screenHeight));

    screen.setPreferredSize(new Dimension(screenWidth, screenHeight));

    //Add all JPanes to screen
    c.gridy = 0;
    c.gridx = 0;
    c.gridheight = 6;

    screen.add(lookReply(), c);


    c.gridy = 0;
    c.gridx = 1;
    screen.add(title(), c);

    c.gridy = 1;
    c.gridx = 1;
    screen.add(space(), c);



    c.gridy = 2;
    c.gridx = 1;
    screen.add(commands(), c);

            //...So on, same for others

}
一个示例方法——JPanel——所有方法都有相同的内容,只是标题和按钮/标签不同

private JPanel title()
{       

    JPanel titlePanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    titlePanel.setBackground(Color.red);
    titlePanel.setOpaque(true);


    JLabel title = new JLabel("DOD");
    titlePanel.add(title, c);


    return titlePanel;
}
我在JPanel上添加了一个背景,以帮助查看它们在窗口中的位置


感谢大家的帮助!谢谢:)

Aplease@MadProgrammer希望这能帮助你/这是你上次发帖时所需要的()你被要求发布一个
SSCCE
。显然,你仍然不明白什么是
SSCCE
,因为你没有在这个问题中发布一个。因此,在这篇帖子中,你被要求发布一个
MVCE
,它与
SSCCE
的概念相同,只是对所需内容有不同的解释。您既没有发布
SSCCE
也没有发布
MVCE
。请解释你发布的代码是如何运行的?我放弃@camickr I打开了MadProgrammer在其评论中的链接,没有在那里显示post-it compiled,只是显示了最小完整性和可验证性。这对我写的代码来说是正确的@Pokeairgay,我同意MVCE链接没有多大帮助(这就是为什么我仍然使用较旧的SSCCE链接),但是如果你阅读它们,你应该能够将2+2放在一起,理解它们的要求。另外,
runnable示例…
注释应该是赠品。我不明白你的另一个问题,你知道如何发布代码,所以发布SSCCE也是一样的。