简体   繁体   中英

Problems with JPanel layouting

My GUI looks like this:

在此输入图像描述

my 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).

I think that you have to set up the "grow" attribute of the cells of the GridBagLayout. Probably the cells have this attribute activated so they grow as much as the window size by default.

I hope it helps. Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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