0

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

}

4

0 に答える 0