GridBagLayout填充

Aly*_*Aly 2 java user-interface swing gridbaglayout

我使用a GridBagLayout(当前)显示两行.我知道这个布局对于这个任务来说太过分了,但我正在努力学习如何使用它.问题是我已将两个面板添加到两个单独的行中,并且内容周围存在巨大差距(请参阅下面的图像和代码): alt text http://www.imagechicken.com/uploads/1264533379009864500.png

Image background;
    public Table(){
        super();
        ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
        background = ii.getImage();
        setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);

        setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        JButton button = new JButton("hello world");
        JPanel panel1 = new JPanel();
        panel1.setPreferredSize(new Dimension(800,100));
        panel1.add(button, BorderLayout.CENTER);
        panel1.setBackground(Color.yellow);
        add(panel1, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;

        JPanel middlePanel = new JPanel();
        middlePanel.setPreferredSize(new Dimension(800,350));
        middlePanel.add(button, BorderLayout.CENTER);
        middlePanel.setBackground(Color.blue);
        add(middlePanel, constraints);


    }
Run Code Online (Sandbox Code Playgroud)

Pet*_*ang 7

使用

constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1d;
constraints.weighty = 1d;
Run Code Online (Sandbox Code Playgroud)

JavaDoc for weightx/ weightysay:

指定如何分配额外的水平/垂直空间.

的JavaDocfill:

当组件的显示区域大于组件的请求大小时,将使用此字段.它决定是否调整组件的大小,如果是,则如何.