我应该为这个Java GUI分配使用什么样的布局?

时间:2016-02-22 21:57:21

标签: java swing user-interface layout jlabel

enter image description here

我创建了两个面板,一个用于JLabel和textfields,另一个用于文本区域。然而,这是我们的第一个GUI任务,而且我是所有不同布局的新手。我已经尝试过群组布局来尝试对齐文本字段和标签,但它并没有为我做好准备。我只是想知道哪种布局最适合实现所需的外观?

1 个答案:

答案 0 :(得分:2)

基本答案是为不同的部分使用不同的布局,例如,我更喜欢使用GridBagLayout作为详细信息窗格,使用GridLayout来布局细节和相关的文本组件,但如果你聪明,你可以使用一个或多个GridBagLayout

但基本的想法是隔离功能并专注于个性化需求

Layout

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ProjectPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ProjectPane extends JPanel {

        public ProjectPane() {
            JPanel detailsPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(4, 4, 4, 4);

            detailsPane.add(new JLabel("Project Name"), gbc);
            gbc.gridy++;
            detailsPane.add(new JLabel("Project Number"), gbc);
            gbc.gridy++;
            detailsPane.add(new JLabel("Project Location"), gbc);
            gbc.gridy++;
            detailsPane.add(new JLabel("Initial Funding"), gbc);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            detailsPane.add(new JTextField(10), gbc);
            gbc.gridy++;
            detailsPane.add(new JTextField(10), gbc);
            gbc.gridy++;
            detailsPane.add(new JTextField(10), gbc);
            gbc.gridy++;
            detailsPane.add(new JTextField(10), gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.CENTER;

            detailsPane.add(new JButton("Create a project"), gbc);

            setLayout(new GridLayout(1, 2));
            add(detailsPane);
            add(new JScrollPane(new JTextArea("No Project")));

        }

    }

}

现在上面的代码主要关注布局,但我很想让detailsPane成为一个单独的组件,从而允许我将它的功能与它自己的类隔离,但那就是我

该组件显然包含在使用JTabbedPane添加到容器中的BorderLayout中,如果这很重要

查看Laying Out Components Within a ContainerHow to Use GridBagLayoutHow to Use GridLayout了解更多详情