Could you hava a look at this picture. I've expressed what I want and what I've got.

This is some excerpt from my code:

GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.5;
    c.weighty = 1.0;
    c.fill = GridBagConstraints.BOTH;
    commonPanel.add(new Panel(new GridBagLayout()), c);

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 0.5;
    c.weighty = 1.0;
    c.fill = GridBagConstraints.BOTH;
    commonPanel.add(new Panel(new GridBagLayout()), c);

    c.gridx = 2;
    c.gridy = 0;
    c.weightx = 0.5;
    c.weighty = 1.0;
    c.fill = GridBagConstraints.BOTH;
    commonPanel.add(new Panel(new GridBagLayout()), c);

By the way, I understand almost nothing. My reasoning is:

  1. I setup c.fill = GridBagConstraints.BOTH; Then all the space should be divided between all the panels both hirizontally and vertically.

  2. c.weightx = 0.5; is taken from the tutorial. They divide the horizontal space between three buttons and for some reasons use weights 0.5 for all the three buttons. Well, let's do the same though it is a mystery why it should be 0.5.

  3. c.weighty = 1.0; should occupy all the space vetically.

Well, not working. Could you clarify what I'm doing wrongly.

有帮助吗?

解决方案

I tried with your code snippet. It works fine for me. So the problem should be with your "commonPanel". What I did was, I add this "commonPanel" to the content pane by setting GridBagConstaints as well.

    frame.getContentPane().setLayout(GridBagLayout());

    GridBagConstraints x = new GridBagConstraints();
    x.weightx = 1.0;
    x.weighty = 1.0;
    x.fill = GridBagConstraints.BOTH;

    JPanel commonPanel = new JPanel(new GridBagLayout());
    frame.getContentPane().add(commonPanel, x); 

As I realized, you should change the code related to your commonPanel.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top