使用Java GroupLayout定位

时间:2015-05-23 15:54:38

标签: java swing grouplayout

以下代码创建以下GUI。

IMG http://i62.tinypic.com/6zt0yr.png

但我想拥有TextFields" 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);

1 个答案:

答案 0 :(得分:2)

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

GridBagLayout在x和y中有权重,填写x和y,它就像一个表。

您需要指定包含2列和3行的表。 那么每个元素都需要定位在这个表的行和列中,但是A和C应该将gridtWidth设置为2列。在中间行中,您将jtextdield放在一列中,将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。它可以读取和显示您的框架,并允许您通过将其拖动到框架中的正确位置来添加按钮,它更容易和更快。如果单击添加的元素,您将自动创建一个动作侦听器,并且您已准备好为该动作编写代码。我推荐这种构建UI的方法:)