Java网格布局网格定位问题

Java网格布局网格定位问题,java,swing,gridbaglayout,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 GridB

我在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;
}

}

考虑发布一个演示您的问题的最小代码示例,一个。这将允许我们运行您的代码,修改它,甚至纠正它。在回复之前,请阅读链接,因为它提供了有关SSCCE要求的许多重要细节。好的,这里是问题的变体,只需复制和粘贴并运行:“只需复制和粘贴并…”抛出编译错误。我不知道你怎么会认为那些不可编译的代码片段(到目前为止发布的任何代码片段)会成为SSCCE!请仔细阅读这份文件。顺便说一下,我会在第一个单元格中使用一个空单元格(例如,标签没有文本)的
GridLayout(5,5)
。@lozga:是的,您发布的代码不是我们可以简单复制、粘贴和运行的代码。如果你仍然需要我们的帮助,没有大的解决方案来,那么再考虑张贴一个。它将需要是小的,它将需要有所有必要的进口。。。只要阅读链接就可以了,因为里面都有解释。祝你好运!另一项更正,现在已测试复制和粘贴合规性