5

我正在尝试使用 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 中的组件,但我并没有完全得到我想要的,这就是我想要实现的

在此处输入图像描述

但这就是我用上面的代码得到的

在此处输入图像描述

因此,当我编译上面的内容时,我最终得到的结果是,如果可能的话,我也不希望用户调整窗口大小,我猜想一些带有窗口属性之一的布尔值..

4

3 回答 3

3

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

尝试使用这个:

c.insets = new Insets(10, 0, 10, 0);
于 2012-10-09T09:22:06.923 回答
3

这是另一种使用 a 的方法GridBagLayout,它导致...

在此处输入图像描述 在此处输入图像描述

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)一切都应该保持不变

于 2012-10-09T09:45:45.573 回答
2

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

我的建议是使用像 NetBeans 或 Eclipse 中可用的 GUI 构建器。在 Java 中手动编写 GUI 非常痛苦,特别是 GridBagLayout 几乎是为生成的布局代码而设计的。

或者,使用TableLayout之类的东西- 或者硬着头皮阅读GridBagConstraints的复杂性。

于 2012-10-09T09:10:51.343 回答