Java GridBagLayout:使组件向左对齐 [英] Java GridBagLayout : make component align to left

查看:174
本文介绍了Java 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. 两个JLabel(unitLbltypelbl)将位于框架的左侧
  2. JTextFieldJComboBox将位于两个JLabel的右侧,两者之间的距离较小.
  3. 此外,我想在网格的位置(3,0)处添加一个新的JButton,但是此位置的高度是两个JLabel高度的总和.这意味着此按钮的高度在两行"上.
  1. Two JLabel (unitLbl and typelbl) will be on the left of frame
  2. JTextField and JComboBox will be on the right of two JLabel, respectively with a small distance between.
  3. Moreover, I want to add a new JButton at location (3,0) of the grid, but the height of this location sum of two JLabel height. It means, this button height is on "two line".

如何解决此代码以实现此目标?请帮助我.

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

谢谢:)

推荐答案

您要使用

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

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

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

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);
        }
    }
}

这篇关于Java GridBagLayout:使组件向左对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆