【问题标题】:Adjusting Gridbag layout调整 Gridbag 布局
【发布时间】:2012-09-29 13:42:15
【问题描述】:

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

但这就是我使用上述代码得到的结果

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

【问题讨论】:

  • pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 此代码行由Darryl Burkecamickr 编写,尤其是第二位提到的Swing 大师在此论坛上提供了一些关于GBC 的示例,
  • GBC 仅(正确)仅在所有列都已填充的情况下(使用不可见的 JComponents)才有效,然后您可以将任何列用于任何行:-),
  • +1 用于布置GridBagLayout

标签: java swing user-interface gridbaglayout


【解决方案1】:

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

试试这个:

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

【讨论】:

  • 程序初始化时的默认大小呢?它出来非常小,我想解决这个问题。
  • 还有应用的TITLE怎么推到最顶端?
  • @noobprogrammer 不要在 cmets 中发布其他问题,而是发布另一个问题。尝试发布SSCCE,这可以让其他人更好更快地帮助您。您可能需要查看 JTextField.setColumns(int) 和 JFrame.pack()。您还可以在 JFrame 的内容窗格上设置一个空边框(看看 BorderFactory.createEmptyBorder()...
【解决方案2】:

这是使用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) 行,一切都应该保持不变

【讨论】:

  • 太棒了!有什么方法可以设置 JFRAME 的大小并将其锁定为该大小?
  • JFrame.setSize,不过要小心,您在可变环境中工作,字体大小、DPI 和分辨率永远不会相同,最好使用 pack 让框架调整到首选大小其内容。我倾向于在我的许多框架中添加一个空边框,以便在内容周围留出一些空间。要阻止用户调整框架大小,请使用 JFrame.setResizable
【解决方案3】:

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

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

或者,使用 TableLayout 之类的东西 - 或者在复杂的 GridBagConstraints 上硬着头皮和 read up

【讨论】:

  • 重复使用相同的 GBC 不是问题。它可能看起来很难看,但这不是这里的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 2012-06-28
相关资源
最近更新 更多