Java GridBagLayout:使组件对齐到左侧

hqt*_*hqt 2 java swing 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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,运行时,似乎每个组件都位于框架的中心.但我想要的是:

  1. 两个JLabel(unitLbltypelbl)将在框架的左侧
  2. JTextField并且JComboBox将在两个右侧JLabel,分别具有较小的距离.
  3. 而且,我想JButton在网格的位置(3,0)添加一个新的,但这个位置的高度总和为两个JLabel高度.这意味着,这个按钮高度是"两行".

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

谢谢 :)

Gui*_*let 5

像这样的东西应该做的伎俩:

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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 您需要适当使用weightx/weighty(如何重新分配额外的水平/垂直空间)
  • 使用适当的fill属性(垂直/水平/两者都拉伸的组件?)
  • 使用适当的anchor属性(如果组件未拉伸,或者至少不在两个方向上,它应该位于其单元格中的哪个位置)
  • 我通常喜欢使用插图代替填充,因此,我更喜欢insetsipadx/ipady(多余的空白空间应该围绕构件或构件的内部添加)