添加组件时,Gridbag布局会调整大小

时间:2013-12-01 19:34:18

标签: java swing layout-manager gridbaglayout

我是网格布局的新手。在进行测试时,我注意到当我添加组件时,我的网格包布局没有保留特定的大小(网格大小)。

我希望我的应用程序与各种屏幕尺寸兼容,因此我无法提前对其进行硬编码。

我可能做错了什么。我不明白应该设计网格的方式吗?

更新:我所要做的就是设置首选的组件大小。他们停止了任意增长。

代码:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;


public class Main extends JFrame {
    // Drawing panels
    private JPanel panelLeft;
    private JPanel panelCenter;
    private JPanel panelRight;
    private JPanel panelTopCenter, panelTopLeft, panelTopRight;
    private JPanel storyPanel;

    // Layout
    private GridBagLayout gridBadLayout;

    private static final String DEFAULT_GAME_WORLD_NAME = "DefaultWorldName";

    // Constructor
    public Main() {
        initializePanels();
    }

    /*
     * Initializes look and feel of the window. No components should be
     * initalized in this because controller would like to lazily initialize
     * them once data is avialable.
     */
    private void initializePanels() {
        this.setTitle("World Maker");
        this.setBounds(55, 5, 600, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBackground(Color.LIGHT_GRAY);

        gridBadLayout = new GridBagLayout();

        panel.setLayout(gridBadLayout);
        GridBagConstraints c = new GridBagConstraints();
        this.add(panel);

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 0.3;
        c.weighty = 0.1;
        c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.BOTH;

        panelTopLeft = new JPanel();
        panelTopLeft.setBackground(Color.WHITE);
        panelTopLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelTopLeft, c);

        c.gridx = 1;
        c.gridwidth = 3;
        panelTopCenter = new JPanel();
        panelTopCenter.setBackground(Color.WHITE);
        panelTopCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelTopCenter, c);

        c.gridx = 4;
        c.gridwidth = 1;
        panelTopRight = new JPanel();
        panelTopRight.setBackground(Color.WHITE);
        panelTopRight.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelTopRight, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 3;
        c.gridwidth = 1;
        c.weightx = 0.3;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.BOTH;
        panelLeft = new JPanel();
        panelLeft.setBackground(Color.WHITE);
        panelLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelLeft, c);

        c.gridx = 1;

        c.gridy = 1;
        c.gridheight = 3;
        c.gridwidth = 3;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.CENTER;
        panelCenter = new JPanel();
        panelCenter.setBackground(Color.WHITE);
        panelCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelCenter, c);

        c.gridx = 4;
        c.gridy = 1;
        c.gridheight = 3;
        c.gridwidth = 1;
        c.weightx = 0.3;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.BOTH;

        panelRight = new JPanel();
        panelRight.setBackground(Color.WHITE);
        panelRight.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelRight, c);

        addRandomComponents();

        this.setVisible(true);
        panel.setOpaque(true);
        this.validate();
    }

    private void addRandomComponents() {
        //add JButton to panelTopLeft
        panelTopLeft.add(new JButton("TEST1"));
        panelTopLeft.add(new JButton("TEST2"));
        panelTopLeft.add(new JButton("TEST3"));

        //add combo box to panelRight
        panelRight.add(new JComboBox(Arrays.asList("TEST1", "TEST2").toArray()));

    }

    public static void main(String[] args) {
        Main main = new Main();
    }
}

截图: 1)在添加任何组件之前 Before I add any components, observe that all grids are of correct sizes

2)添加组件后(参见左上图) After I add components, observe that grid sizes are messy

1 个答案:

答案 0 :(得分:0)

这是因为没有为组件设置首选大小。设置后,组件的长度不会超过大小。

panelTopLeft.setPreferredSize(new Dimension(100, 0));

完整的固定代码。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;


public class Main extends JFrame {
    // Drawing panels
    private JPanel panelLeft;
    private JPanel panelCenter;
    private JPanel panelRight;
    private JPanel panelTopCenter, panelTopLeft, panelTopRight;
    private JPanel storyPanel;

    // Layout
    private GridBagLayout gridBadLayout;

    private static final String DEFAULT_GAME_WORLD_NAME = "DefaultWorldName";

    // Constructor
    public Main() {
        initializePanels();
    }

    /*
     * Initializes look and feel of the window. No components should be
     * initalized in this because controller would like to lazily initialize
     * them once data is avialable.
     */
    private void initializePanels() {
        this.setTitle("World Maker");
        this.setBounds(55, 5, 600, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBackground(Color.LIGHT_GRAY);

        gridBadLayout = new GridBagLayout();

        panel.setLayout(gridBadLayout);
        GridBagConstraints c = new GridBagConstraints();
        this.add(panel);

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 0.3;
        c.weighty = 0.1;
        c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.BOTH;

        panelTopLeft = new JPanel();
        panelTopLeft.setBackground(Color.WHITE);
        panelTopLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelTopLeft, c);

        c.gridx = 1;
        c.gridwidth = 3;
        panelTopCenter = new JPanel();
        panelTopCenter.setBackground(Color.WHITE);
        panelTopCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelTopCenter, c);

        c.gridx = 4;
        c.gridwidth = 1;
        panelTopRight = new JPanel();
        panelTopRight.setBackground(Color.WHITE);
        panelTopRight.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelTopRight, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 3;
        c.gridwidth = 1;
        c.weightx = 0.3;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.BOTH;
        panelLeft = new JPanel();
        panelLeft.setBackground(Color.WHITE);
        panelLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelLeft, c);

        c.gridx = 1;

        c.gridy = 1;
        c.gridheight = 3;
        c.gridwidth = 3;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.CENTER;
        panelCenter = new JPanel();
        panelCenter.setBackground(Color.WHITE);
        panelCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelCenter, c);

        c.gridx = 4;
        c.gridy = 1;
        c.gridheight = 3;
        c.gridwidth = 1;
        c.weightx = 0.3;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.BOTH;

        panelRight = new JPanel();
        panelRight.setBackground(Color.WHITE);
        panelRight.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.add(panelRight, c);

        addRandomComponents();

        this.setVisible(true);
        panel.setOpaque(true);
        this.validate();
    }

    private void addRandomComponents() {
        //add JButton to panelTopLeft
        panelTopLeft.add(new JButton("TEST1"));
        panelTopLeft.add(new JButton("TEST2"));
        panelTopLeft.add(new JButton("TEST3"));
        panelTopLeft.setPreferredSize(new Dimension(100, 0));

        //add combo box to panelRight
        panelRight.add(new JComboBox(Arrays.asList("TEST1", "TEST2").toArray()));

    }

    public static void main(String[] args) {
        Main main = new Main();
    }
}