GridBagLayout中的按钮宽度

时间:2015-03-12 13:24:37

标签: java swing user-interface gridbaglayout

需要一点GridbagLayout指导。请参阅下面的代码。我想要的是button2的宽度是按钮1,3和4的两倍。但是,所有四个实际上都以相同的宽度显示。显然做错了,但我无法弄清楚是什么。

感谢。

import java.awt.*;
import javax.swing.*;

public class GridbagLayoutTetst {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 250);

        JPanel panel = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        panel.setLayout(gridbag);

        JButton button0 = new JButton("Button 0");
        c.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(button0, c);
        panel.add(button0);

        JButton button1 = new JButton("Button 1");
        c.gridwidth = 1;
        gridbag.setConstraints(button1, c);
        panel.add(button1);

        JButton button2 = new JButton("Button 2");
        c.gridwidth = 2;
        gridbag.setConstraints(button2, c);
        panel.add(button2);

        JButton button3 = new JButton("Button 3");
        c.gridwidth = 1;
        gridbag.setConstraints(button3, c);
        panel.add(button3);

        JButton button4 = new JButton("Button 4");
        c.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(button4, c);
        panel.add(button4);

        frame.add(panel);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            //
        }
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

好的,通过使用fillsetPreferredSizeweightx的组合,我实现了我想要的结果。见下文。现在button2的宽度始终是该行中其他按钮的两倍,即使我调整了框架的大小。就像对我的黑客一样。希望得到关于“首选”方式的指导。

import java.awt.*;
import javax.swing.*;

public class GridbagLayoutTetst {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 250);

        JPanel panel = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        panel.setLayout(gridbag);

        JButton button0 = new JButton("Button 0");
        c.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(button0, c);
        panel.add(button0);

        JButton button1 = new JButton("Button 1");
        button1.setPreferredSize(new Dimension (0,28));
        c.weightx = 0.2;
        c.gridwidth = 1;
        gridbag.setConstraints(button1, c);
        panel.add(button1);

        JButton button2 = new JButton("Button 2");
        button2.setPreferredSize(new Dimension (0,28));
        c.weightx = 0.4;
        gridbag.setConstraints(button2, c);
        panel.add(button2);

        JButton button3 = new JButton("Button 3");
        button3.setPreferredSize(new Dimension (0,28));
        c.weightx = 0.2;
        gridbag.setConstraints(button3, c);
        panel.add(button3);

        JButton button4 = new JButton("Button 4");
        button4.setPreferredSize(new Dimension (0,28));
        c.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(button4, c);
        panel.add(button4);

        frame.add(panel);
        frame.setVisible(true);

        System.out.println(button1.getPreferredSize());
    }
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            //
        }
        SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                createAndShowGUI();
            }
        });
    }

}