Java gridbaglayout网格定位问题

时间:2012-12-08 15:50:52

标签: java swing gridbaglayout

我遇到了GridBagLayout的一些问题。 很抱歉无法发布图片。这就是我想要的:

  0 1 2 3 4
0 1 1 1 1 2
1 1 1 1 1 2
2 1 1 1 1 2
3 1 1 1 1 4
4 3 3 3 3 4

这就是我得到的

  0 1 2 3 4
0 1 1 1 1 2
1 1 1 1 1 2
2 1 1 1 1 2
3 1 1 1 1 2
4 3 3 3 3 4

使用以下代码:

    JPanel mainPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.gridheight = 4;
    gbc.gridwidth = 4;
    gbc.gridx = 0;
    gbc.gridy = 0;
    JPanel panelOne = new JPanel();
    //add some content
    mainPanel.add(panelOne, gbc);

    gbc.gridheight = 3;
    gbc.gridwidth = 1;
    gbc.gridx = 4;
    gbc.gridy = 0;
    JPanel panelTwo = new JPanel();
    //Add some content
    mainPanel.add(panelTwo, gbc);

    gbc.gridheight = 1;
    gbc.gridwidth = 4;
    gbc.gridx = 0;
    gbc.gridy = 4;
    JPanel panelThree = new JPanel();
    //Add some content
    mainPanel.add(panelThree, gbc);

    gbc.gridheight = 2;
    gbc.gridwidth = 1;
    gbc.gridx = 4;
    gbc.gridy = 3;
    JPanel panelFour = new JPanel();
    //Add some content
    mainPanel.add(panelFour, gbc);

    mainFrame.setContentPane(mainPanel);

尝试了多次不同的参数,添加/删除了weightx和weighty,没有任何成功。

这是一个变体,只需复制和粘贴:

 package problem;

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Problem {

    public static void main(String[] args) {
        createMainField();
    }

public static void setDamnedSize(Component component, Dimension size) {
    component.setMinimumSize(size);
    component.setMaximumSize(size);
    component.setPreferredSize(size);
    component.setSize(size);
}

public static JPanel createOneButton(String name) {
    JPanel returnPanel = new JPanel(new GridLayout(0, 1));
    JButton button = new JButton(name);
    returnPanel.add(button);
    return returnPanel;
}

public static JFrame createMainField() {
    JFrame mainFrame = new JFrame("Space Race:The Game");
    setDamnedSize(mainFrame, new Dimension(800, 800));
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);

    JPanel mainPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 4;
    gbc.weighty = 4;
    gbc.gridheight = 4;
    gbc.gridwidth = 4;
    gbc.gridx = 0;
    gbc.gridy = 0;
    JPanel panelOne = new JPanel(new GridLayout(1, 0));
    panelOne.setBackground(Color.DARK_GRAY);
    panelOne.add(createOneButton("One"));
    mainPanel.add(panelOne, gbc);

    gbc.weightx = 3;
    gbc.weighty = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 3;
    gbc.gridx = 4;
    gbc.gridy = 0;
    JPanel panelTwo = new JPanel(new GridLayout(0, 1));
    panelTwo.setBackground(Color.LIGHT_GRAY);
    panelTwo.add(createOneButton("Two"));
    mainPanel.add(panelTwo, gbc);

    gbc.weightx = 4;
    gbc.weighty = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 4;
    gbc.gridx = 0;
    gbc.gridy = 4;
    JPanel panelThree = new JPanel(new GridLayout(1, 0));
    panelThree.setBackground(Color.GRAY);
    panelThree.add(createOneButton("Three"));
    mainPanel.add(panelThree, gbc);

    gbc.weightx = 1;
    gbc.weighty = 2;
    gbc.gridheight = 2;
    gbc.gridwidth = 1;
    gbc.gridx = 4;
    gbc.gridy = 3;
    JPanel panelFour = new JPanel(new GridLayout(0, 1));
    panelFour.setBackground(Color.decode("#A0A0A0"));
    panelFour.add(createOneButton("Four"));
    mainPanel.add(panelFour, gbc);

    mainFrame.setContentPane(mainPanel);
    mainFrame.setVisible(true);
    return mainFrame;
}

}

0 个答案:

没有答案