调整Gridbag布局

时间:2012-10-09 09:03:25

标签: java swing user-interface gridbaglayout

我正在尝试使用java中的GRIDBAG布局实现此布局

public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        JLabel label1,label2,label3,result,title;
        JButton calculate_btn;
        JTextField side1,side2,side3;

    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
    //natural height, maximum width
    c.fill = GridBagConstraints.HORIZONTAL;
    }


        title = new JLabel("Area of Triangle");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 2;
    c.gridy = -1;
    pane.add(title, c);



    label1 = new JLabel("Side 1: ");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
         c.ipady = 20;
    c.gridx = 1;
    c.gridy = 1;
    pane.add(label1, c);

        label2 = new JLabel("Side 2: ");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
         c.ipady = 20;
    c.gridx = 1;
    c.gridy = 2;
    pane.add(label2, c);


        label3 = new JLabel("Side 3: ");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 20;
    c.gridx = 1;
    c.gridy = 3;
    pane.add(label3, c);

        side1 = new JTextField("   ");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 20;
    c.gridx = 2;
    c.gridy = 1;
    pane.add(side1, c);

        side2 = new JTextField("Side 3: ");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 20;
    c.gridx = 2;
    c.gridy = 2;
    pane.add(side2, c);

        side3 = new JTextField("Side 3: ");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 20;
    c.gridx = 2;
    c.gridy = 3;
    pane.add(side3, c);

    calculate_btn = new JButton("Calculate");
    //c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 30;      //make this component tall
    c.weightx = 0.5;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 5;
    pane.add(calculate_btn, c);

        result = new JLabel("Result displayed here");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 20;
    c.gridx = 2;
    c.gridy = 7;
    pane.add(result, c);


    }

所以上面的代码基本上只是将添加到GUI中的组件,但我并没有得到我想要的东西,这就是我想要实现的目标

enter image description here

但这是我用上面的代码获得的

enter image description here

所以当我编译上面是我最终的时候,如果可能我也不希望用户调整窗口大小,我猜测一些布尔值与其中一个窗口属性..

3 个答案:

答案 0 :(得分:3)

问题是您正在设置ipady垂直“拉伸”您的组件。您可能正在寻找insets属性:http://docs.oracle.com/javase/7/docs/api/java/awt/GridBagConstraints.html#insets

尝试使用:

c.insets = new Insets(10, 0, 10, 0);

答案 1 :(得分:3)

这是使用GridBagLayout的另一种方法,结果是......

enter image description here enter image description here

public class TestLayout {

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FormPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    protected static class FormPane extends JPanel {

        JLabel label1, label2, label3, result, title;
        JButton calculate_btn;
        JTextField side1, side2, side3;

        public FormPane() {
            // You may not need this, I needed it because the window packed to 
            // small on my mac ;)            
            setBorder(new EmptyBorder(4, 4, 4, 4));

            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();

            title = new JLabel("Area of Triangle");
            label1 = new JLabel("Side 1: ");
            label2 = new JLabel("Side 2: ");
            label3 = new JLabel("Side 3: ");
            side1 = new JTextField(4);
            side2 = new JTextField(4);
            side3 = new JTextField(4);
            calculate_btn = new JButton("Calculate");

            result = new JLabel("Result displayed here");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.gridwidth = 2;
            gbc.weighty = 1;
            add(title, gbc);

            gbc.weighty = 0;
            gbc.gridy = 1;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridwidth = 1;
            add(label1, gbc);
            gbc.gridy++;
            add(label2, gbc);
            gbc.gridy++;
            add(label3, gbc);

            gbc.gridy = 1;
            gbc.gridx = 1;
            add(side1, gbc);
            gbc.gridy++;
            add(side2, gbc);
            gbc.gridy++;
            add(side3, gbc);

            gbc.gridx = 0;
            gbc.gridwidth = 2;
            gbc.gridy++;
            add(result, gbc);

            gbc.gridy++;
            gbc.weighty = 1;
            gbc.anchor = GridBagConstraints.NORTH;
            add(calculate_btn, gbc);

        }

    }

}

如果您想在标题和字段之间添加空格以及一些Insets

gbc.insets = new Insets(0, 0, 8, 0);
add(title, gbc);

// Don't forget to reset them ;)
gbc.insets = new Insets(0, 0, 0, 0);

我刚刚意识到结果应该显示在按钮下面。简单地交换行add(result, gbc)add(calculate_btn, gbc),一切都应保持不变

答案 2 :(得分:2)

我在您的代码中看到的一个问题是,您为所添加的所有元素重复使用相同的GridBagConstraints对象,这是不推荐的。

我的建议是使用类似NetBeans或Eclipse中提供的GUI构建器。 Java中的手工编码GUI非常痛苦,特别是GridBagLayout设计用于生成的布局代码。

或者,使用TableLayout之类的内容 - 或者在read up的错综复杂的内容中使用GridBagConstraints