简体   繁体   中英

Swing : GridBagLayout adding components not the expected way

I'm just trying to use the GridBagLayout following this tutorial.

I can't get what I want to have.

I would want each Panel to take as much space as possible but it is not the case, I really tried everything, following the steps in the Oracle Docs

But the result is always the same :

GBC

I really do not understand why the layout is not working.

I tried changing the layout of the JFrame, setting the panel as ContentPane(), tried to add the constraints found on both websites, nothing seems to work.

Your help will is appreciated.


Full Code

public class Monitor extends JFrame{

    // Menu
    private JMenuBar bar = new JMenuBar();
    private JMenu file = new JMenu("File");
    private JMenuItem open = new JMenuItem("Open");
    private JMenuItem exit = new JMenuItem("Exit");

    private JPanel mainPanel = new JPanel();

    private JPanel secondaryPanel;
    private JPanel explorerPanel, tagPanel;

    public Monitor(){
        setTitle("");
        setSize(750,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);


        initComps();
    }

    public void initComps(){

        explorerPanel = new JPanel();
        explorerPanel.setBackground(Color.BLACK);
        explorerPanel.setPreferredSize(new Dimension(250,300));

        tagPanel = new JPanel();
        tagPanel.setBackground(Color.yellow);
        tagPanel.setPreferredSize(new Dimension(250, 300));

        secondaryPanel = new JPanel();
        secondaryPanel.setBackground(Color.blue);
        secondaryPanel.setPreferredSize(new Dimension(500, 600));

        mainPanel = new JPanel();
        mainPanel.setPreferredSize(new Dimension(750, 600));
        mainPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0 ; gbc.gridy = 0 ; gbc.gridwidth = 1 ; gbc.gridheight = 1;
        mainPanel.add(explorerPanel, gbc);

        gbc.gridx = 1 ; gbc.gridy = 0 ; gbc.gridwidth = 2 ; gbc.gridheight = 2;
        mainPanel.add(secondaryPanel, gbc);

        gbc.gridx = 0 ; gbc.gridy = 1 ; gbc.gridwidth = 1 ; gbc.gridheight = 1;
        mainPanel.add(tagPanel, gbc);

        this.setContentPane(mainPanel);

        // Menu Bar
        file.add(open);

        exit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        exit.setMnemonic('q');
        file.add(exit);

        bar.add(file);
        setJMenuBar(bar);
    }

    public static void main(String[] args) {
        new Monitor().setVisible(true);

    }
}

EDIT :

When adding

gbc.weightx = 1;
gbc.weighty = 1;

Result

GBC

You need to set GridBagConstraints.weightx and GridBagConstraints.weighty to something non-zero:

From the documentation

GridBagConstraints.weightx, GridBagConstraints.weighty

Used to determine how to distribute space, which is important for specifying resizing behavior. Unless you specify a weight for at least one component in a row (weightx) and column (weighty), all the components clump together in the center of their container. This is because when the weight is zero (the default), the GridBagLayout object puts any extra space between its grid of cells and the edges of the container.

Setting it to 1 for all of the components should work fine if you want them all to fill up the space.

There is also more explanation of these fields on the Oracle tutorial page you linked under the heading weightx, weighty .

EDIT:

You also need to set the fill for the components, otherwise it defaults to NONE :

GridBagConstraints.fill

Used when the component's display area is larger than the component's requested size to determine whether (and how) to resize the component. Possible values are GridBagConstraints.NONE (the default), GridBagConstraints.HORIZONTAL (make the component wide enough to fill its display area horizontally, but don't change its height), GridBagConstraints.VERTICAL (make the component tall enough to fill its display area vertically, but don't change its width), and GridBagConstraints.BOTH (make the component fill its display area entirely).

Here is what I get by setting weightx to 0.33 for explorerPanel and tagPanel , and 0.66 for secondaryPanel , and setting fill to GridBagConstraints.BOTH for all of them:

在此处输入图片说明

Your problem comes from your dimensions.

Don't use "preferred" only. Use minimum as well.

explorerPanel = new JPanel();
explorerPanel.setBackground(Color.BLACK);
explorerPanel.setMinimumSize(new Dimension(250, 300));
explorerPanel.setPreferredSize(new Dimension(250, 300));

tagPanel = new JPanel();
tagPanel.setBackground(Color.yellow);
tagPanel.setMinimumSize(new Dimension(250, 300));
tagPanel.setPreferredSize(new Dimension(250, 300));

secondaryPanel = new JPanel();
secondaryPanel.setBackground(Color.blue);
secondaryPanel.setMinimumSize(new Dimension(500, 600));
secondaryPanel.setPreferredSize(new Dimension(500, 600));

mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(750, 600));
mainPanel.setMinimumSize(new Dimension(750, 600));
mainPanel.setLayout(new GridBagLayout());

This worked in my test: 在此处输入图片说明

You can fix this by using BorderLayout

Rather than this:

    this.setContentPane(mainPanel);

You can do this:

getContentPane().add(mainPanel, BorderLayout.CENTER);

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