简体   繁体   English

JPanel布局问题

[英]Problems with JPanel layouting

My GUI looks like this: 我的GUI看起来像这样:

在此输入图像描述

my gui 我的gui

and should look like this: 并且应该如下所示:

在此输入图像描述 solution and this is my code for layout: 解决方案 ,这是我的布局代码:

public void initComponents(){

    setTitle("GUI-Design");
    setSize(800, 600);

    setLayout(new BorderLayout());
    top = new JPanel();
    mid = new JPanel();
    bottom = new JPanel();
    add(top, BorderLayout.NORTH);
    add(mid, BorderLayout.CENTER);
    add(bottom, BorderLayout.SOUTH);


    //TOP Start
    top.setLayout(new BorderLayout());
    JLabel label = new JLabel("Informatik/Technik", JLabel.CENTER);
    ImageIcon img = new ImageIcon(this.getClass().getResource("/images/Logo.jpg"));
    JLabel image = new JLabel("",img, JLabel.CENTER);
    top.setBackground(Color.WHITE);
    top.add(label, BorderLayout.WEST);
    top.add(image, BorderLayout.EAST);
    //TOP END
    //MID Start
    mid.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
......

}

how can I take the huge gap away in the center and in the bottum ? 我怎样才能在中心和底部消除巨大的差距?

I would recommend you to use WindowBuilder plugin for Eclipse to develop windows or components (if you are using another IDE, it must have their own plugins). 我建议你使用Eclipse的WindowBuilder插件来开发窗口或组件(如果你使用的是另一个IDE,它必须有自己的插件)。

I think that you have to set up the "grow" attribute of the cells of the GridBagLayout. 我认为你必须设置GridBagLayout的单元格的“grow”属性。 Probably the cells have this attribute activated so they grow as much as the window size by default. 可能是单元格已激活此属性,因此默认情况下它们会增加窗口大小。

I hope it helps. 我希望它有所帮助。 Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM