Java GridBagLayout:使组件左对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12902444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:42:53  来源:igfitidea点击:

Java GridBagLayout : make component align to left

javaswinggridbaglayout

提问by hqt

I have this layout using GridBagLayout:

我有这个布局使用GridBagLayout

public class Example extends JFrame {
    public Example() {
        Border outline = BorderFactory.createLineBorder(Color.black);
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel pane = new JPanel(gbl);

        gbc.weighty = 1.0;
        gbc.weightx = 1.0;

        JLabel unitLbl = new JLabel("Unit");
        unitLbl.setBorder(outline);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.ipadx = 30;
        gbc.ipady = 10;
        gbl.setConstraints(unitLbl, gbc);
        pane.add(unitLbl);

        JLabel typeLbl = new JLabel("Type");
        typeLbl.setBorder(outline);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.ipadx = 30;
        gbc.ipady = 10;
        gbl.setConstraints(typeLbl, gbc);
        pane.add(typeLbl);

        JTextField unitField = new JTextField();
        typeLbl.setBorder(outline);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.ipadx = 30;
        gbc.ipady = 10;
        gbl.setConstraints(unitField, gbc);
        pane.add(unitField);

        String[] type = {"All", "Verb", "Noun", "Adjective"};
        JComboBox<String> comboBox = new JComboBox<String>(type);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.ipadx = 30;
        gbc.ipady = 10;
        gbl.setConstraints(comboBox, gbc);
        pane.add(comboBox);


        add(pane, BorderLayout.CENTER);
        setSize(new Dimension(400, 300));
        getContentPane().setBackground(Color.WHITE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }
}

In this example, when run, It seems that every component is at the center of the frame. But what I want is :

在这个例子中,运行时,似乎每个组件都在框架的中心。但我想要的是:

  1. Two JLabel(unitLbland typelbl) will be on the left of frame
  2. JTextFieldand JComboBoxwill be on the right of two JLabel, respectively with a small distance between.
  3. Moreover, I want to add a new JButtonat location (3,0) of the grid, but the height of this location sum of two JLabelheight. It means, this button height is on "two line".
  1. 两个JLabel(unitLbltypelbl) 将在框架的左侧
  2. JTextFieldJComboBox将在两个 的右侧JLabel,分别与它们之间有一小段距离。
  3. 此外,我想JButton在网格的位置 (3,0) 处添加一个新的,但是这个位置的JLabel高度是两个高度的总和。这意味着,这个按钮的高度在“两行”上。

How can I fix this code to achieve this goal ? Please help me.

如何修复此代码以实现此目标?请帮我。

Thanks :)

谢谢 :)

回答by Guillaume Polet

Something like this should do the trick:

像这样的事情应该可以解决问题:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Example extends JFrame {
    public Example() {
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel pane = new JPanel(gbl);
        gbc.anchor = GridBagConstraints.WEST;

        JLabel unitLbl = new JLabel("Unit");
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(3, 3, 3, 30);
        gbl.setConstraints(unitLbl, gbc);
        pane.add(unitLbl);

        JLabel typeLbl = new JLabel("Type");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbl.setConstraints(typeLbl, gbc);
        pane.add(typeLbl);

        gbc.weightx = 1.0;
        gbc.insets = new Insets(3, 3, 3, 3);

        JTextField unitField = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbl.setConstraints(unitField, gbc);
        pane.add(unitField);

        String[] type = { "All", "Verb", "Noun", "Adjective" };
        JComboBox<String> comboBox = new JComboBox<String>(type);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        gbl.setConstraints(comboBox, gbc);
        pane.add(comboBox);

        final JButton someButton = new JButton("Click me");
        someButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(someButton, "You have clicked " + someButton.getText());
            }
        });

        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.fill = GridBagConstraints.VERTICAL;
        pane.add(someButton, gbc);
        add(pane, BorderLayout.CENTER);
        setSize(new Dimension(400, 300));
        getContentPane().setBackground(Color.WHITE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }
}
  • You need to use appropriately weightx/weighty(how is the extra horizontal/vertical space redistributed)
  • Use the appropriate fillattribute (is the component stretched vertically/horizontally/both?)
  • Use the appropriate anchorattribute (if the component is not stretched, or at least not in both direction, where should it be located within its cell)
  • I usually prefer to use insets instead of padding, therefore, I prefer insetsover ipadx/ipady(extra white-space should be added around the component or inside the component)
  • 您需要适当地使用weightx/weighty(如何重新分配额外的水平/垂直空间)
  • 使用适当的fill属性(组件是否垂直/水平/两者都拉伸?)
  • 使用适当的anchor属性(如果组件没有被拉伸,或者至少不是双向拉伸,它应该位于其单元格内的哪个位置)
  • 我通常喜欢使用插图代替填充,因此,我更喜欢insetsipadx/ipady(多余的空白空间应该围绕构件或构件的内部添加)

回答by MadProgrammer

You want to use GridBagConsraints#anchorto define the position within the cell that you want to align the component to.

您想使用它GridBagConsraints#anchor来定义单元格中要将组件对齐到的位置。

To allow a component to span over number of cells, you want to use GridBagConstraints#gridwidthand GridBagConstraints#gridheight(the default is 1)

要允许组件跨越多个单元格,您需要使用GridBagConstraints#gridwidthGridBagConstraints#gridheight(默认值为 1)

enter image description here

在此处输入图片说明

public class TestLayout09 {

    public static void main(String[] args) {
        new TestLayout09();
    }

    public TestLayout09() {
        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 LayoutPane());
                frame.setBackground(Color.WHITE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class LayoutPane extends JPanel {

        public LayoutPane() {
            Border outline = BorderFactory.createLineBorder(Color.black);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            // I'm not sure this really is what you want, but I may be mistaken
//            gbc.weighty = 1.0;
//            gbc.weightx = 1.0;

            JLabel unitLbl = new JLabel("Unit");
            unitLbl.setBorder(outline);
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.ipadx = 30;
            gbc.ipady = 10;
            gbc.anchor = GridBagConstraints.WEST;
            add(unitLbl, gbc);

            JLabel typeLbl = new JLabel("Type");
            typeLbl.setBorder(outline);
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.ipadx = 30;
            gbc.ipady = 10;
            add(typeLbl, gbc);

            JTextField unitField = new JTextField();
            typeLbl.setBorder(outline);
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.ipadx = 30;
            gbc.ipady = 10;
            gbc.anchor = GridBagConstraints.EAST;
            add(unitField, gbc);

            String[] type = {"All", "Verb", "Noun", "Adjective"};
            JComboBox<String> comboBox = new JComboBox<String>(type);
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.ipadx = 30;
            gbc.ipady = 10;
            add(comboBox, gbc);

            JButton btn = new JButton("Test");
            gbc.gridx = 3;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridheight = 2;
            add(btn, gbc);
        }
    }
}

回答by Dan D.

Some answers:

一些答案:

Put the anchor for unitlbl to WEST.

将 unitlbl 的锚点放在 WEST。

gbc.anchor = GridBagConstraints.WEST;

And the anchor for unitField to EAST.

以及 unitField 到 EAST 的锚点。

gbc.anchor = GridBagConstraints.EAST;

And for the button:

对于按钮:

JButton button = new JButton("Test");
gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.weighty = 1;
pane.add(button, gbc);