使用Java GroupLayout进行定位

使用Java GroupLayout进行定位,java,swing,grouplayout,Java,Swing,Grouplayout,下面的代码创建了以下GUI 但是我希望文本字段“A”和“C”完全填充它们各自的行(以便它们的右角与JComboBox的右边缘对齐。非常感谢您的帮助 代码如下: JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); Dimension size = new Dimension( 310

下面的代码创建了以下GUI

但是我希望文本字段“A”和“C”完全填充它们各自的行(以便它们的右角与JComboBox的右边缘对齐。非常感谢您的帮助

代码如下:

JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        Dimension size = new Dimension( 310, 210 );
        frame.setSize(size);
        frame.setPreferredSize(size);

        JTextField tf1 = new JTextField();
        JTextField tf2 = new JTextField();
        JTextField tf3 = new JTextField();

        JLabel label1 = new JLabel( "A");
        JLabel label2 = new JLabel( "B");
        JLabel label3 = new JLabel( "C");

        String[] opts = {"1","2","3"};
        JComboBox dropdown = new JComboBox(opts);
        JPanel panel = new JPanel();

       GroupLayout layout = new GroupLayout(panel);
       panel.setLayout(layout);
       layout.setAutoCreateGaps(true);
       layout.setAutoCreateContainerGaps(true);

       GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

       hGroup.addGroup(layout.createParallelGroup().
                addComponent(label1).addComponent(label2).
                addComponent(label3));

       hGroup.addGroup(layout.createParallelGroup().
                addComponent(tf1).addComponent(tf2).
                addComponent(tf3));

       hGroup.addGroup(layout.createParallelGroup().addComponent(dropdown));


       layout.setHorizontalGroup(hGroup);

       GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

       vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(label1).addComponent(tf1));
       vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(label2).addComponent(tf2).addComponent(dropdown));  
       vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
               addComponent(label3).addComponent(tf3));

       layout.setVerticalGroup(vGroup);

       frame.add( panel, BorderLayout.NORTH );
       frame.setVisible(true);

我认为最好是使用GridBagLayout,它有点难用,但是如果你阅读一些教程,你会发现它是最适合你的解决方案

GridBagLayout在x和y中有权重,在x和y中填充,它就像一张桌子

您需要指定包含两列和三行的表。 然后,每个元素都需要放在这个表行和列中,但是A和C的网格宽度应该设置为2列。在中间行中,将JTabdiDeD放在一列中,第二列中的JCOMBOBOX。 下面是执行此操作的示例程序:

    import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComboBox;

public class Example {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Example window = new Example();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Example() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[] {500, 500};
        gridBagLayout.rowHeights = new int[] {50, 50, 50};
        gridBagLayout.columnWeights = new double[]{1.0, 1.0};
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0};
        frame.getContentPane().setLayout(gridBagLayout);

        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.weightx = 1.0;
        gbc_textField.gridwidth = 2;
        gbc_textField.insets = new Insets(0, 0, 5, 0);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 0;
        gbc_textField.gridy = 0;
        frame.getContentPane().add(textField, gbc_textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        GridBagConstraints gbc_textField_1 = new GridBagConstraints();
        gbc_textField_1.weightx = 1.0;
        gbc_textField_1.insets = new Insets(0, 0, 5, 5);
        gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_1.gridx = 0;
        gbc_textField_1.gridy = 1;
        frame.getContentPane().add(textField_1, gbc_textField_1);
        textField_1.setColumns(10);

        JComboBox comboBox = new JComboBox();
        GridBagConstraints gbc_comboBox = new GridBagConstraints();
        gbc_comboBox.weightx = 1.0;
        gbc_comboBox.insets = new Insets(0, 0, 5, 0);
        gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
        gbc_comboBox.gridx = 1;
        gbc_comboBox.gridy = 1;
        frame.getContentPane().add(comboBox, gbc_comboBox);

        textField_2 = new JTextField();
        GridBagConstraints gbc_textField_2 = new GridBagConstraints();
        gbc_textField_2.weightx = 1.0;
        gbc_textField_2.gridwidth = 2;
        gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_2.gridx = 0;
        gbc_textField_2.gridy = 2;
        frame.getContentPane().add(textField_2, gbc_textField_2);
        textField_2.setColumns(10);
    }

}
如果您不太熟悉,则很难手工编写UI。更好的方法是使用一些UI设计工具。例如Eclipse中的WindowBuilder。它可以读取和显示框架,并允许您通过将按钮拖动到框架中的正确位置来添加按钮,这将更加简单和快速。如果您单击添加的元素,您将自动et创建了一个动作监听器,您已经准备好为动作编写代码了。我建议您采用这种方式构建UI:)