简体   繁体   English

什么是最好的布局管理器?

[英]What would be the best Layout Manager?

在此输入图像描述

I would like to make a gui that looks like the above. 我想做一个看起来像上面的gui。 right now I have a panel which will hold the name label, name textfield, brith date label and birthday textfield. 现在我有一个面板,它将保存名称标签,名称文本字段,brith日期标签和生日文本字段。 My question is what would be the best layout manager to use on the panel so that the row of "name components" (lable + textfield) and the row of "birth date components" (lable + textfield), would be evenly spread vertically in the panel. 我的问题是什么是最好的布局管理器在面板上使用,以便“名称组件”行(标签+文本字段)和“出生日期组件”行(标签+文本字段),将垂直均匀地分布在小组。

I thought about using flow layout, but that would result in no gap between the two rows of components. 我想过使用流布局,但这会导致两行组件之间没有间隙。 I thought about using a grid layout, but I do not know the gap size between the two rows of components. 我想过使用网格布局,但我不知道两行组件之间的间隙大小。

A more complicated approach...i thought about putting the name label and textfield in one panel, and the birth date label and textfield in another panel, then make the base panel border layout and set name to be north, birthdate to be south...but then i would still have to make sure the name components are vertically centered in the name panel and birthdate components are vertically centered in the birth date panel. 一个更复杂的方法......我考虑将名称标签和文本字段放在一个面板中,并将出生日期标签和文本字段放在另一个面板中,然后使基本面板边框布局和设置名称为北,生日日期为南。 ..但是我仍然需要确保名称组件在名称面板中垂直居中,而birthdate组件在出生日期面板中垂直居中。

Any help is appreciated. 任何帮助表示赞赏。 The goal is to make sure the row of name components and the row of birth date components are vertically spread out, with the name components being centered vertically on the top half, and the birth date components centered vertically on the bottom half. 目标是确保名称组件行和出生日期组件行垂直展开,名称组件垂直居中于上半部分,出生日期组件垂直居中于下半部分。 If anything sounds confusing please let me know I'll try to rephrase for a better understanding. 如果有什么问题让人感到困惑,请告诉我我会尝试改写以便更好地理解。

I am using strictly Java swing with eclipse, no GUI builder or anything like that. 我正在使用严格的Java swing与eclipse,没有GUI构建器或类似的东西。

I like the MigLayout . 我喜欢MigLayout In that case, it would be very easy to layout the components as the MigLayout has a table-like behaviour and the components would be arranged in that way. 在这种情况下,布局组件将非常容易,因为MigLayout具有类似于表的行为,并且组件将以这种方式排列。 This is described in the QuickStartGuide . 这在QuickStartGuide中有所描述。

Edit: 编辑:

Here is a small example: 这是一个小例子:

在此输入图像描述

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;

public class Test {

    private JFrame frame;
    private JTextField nameTextField, birthDateTextField;
    private JLabel nameLabel, birthDateLabel;

    public Test() {
        initComponents();
    }

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

    private void initComponents() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new MigLayout());

        nameTextField = new JTextField(30);
        birthDateTextField = new JTextField(30);
        nameLabel = new JLabel("Name:");
        birthDateLabel = new JLabel("Birth Date:");

        frame.add(nameLabel);
        frame.add(nameTextField, "wrap");
        frame.add(birthDateLabel);
        frame.add(birthDateTextField);
        frame.pack();
        frame.setVisible(true);
    }
}

I highly recommend you stay away from layouts designed to be automated by a layout manager --they can be flexible but they tend to be used as a hammer, a very awkward one that takes a lot of finess if you want components to resize in a natural way. 我强烈建议你远离布局经理设计的自动布局 - 它们可以灵活但是它们往往被用作锤子,如果你想要组件调整大小,那么它会非常笨拙自然的方式。

Learning to use layout managers is a bit of an art. 学习使用布局管理器是一门艺术。 You don't pick just one and use it for every problem, instead you take a few different types and nest them in a way that looks like your data. 您不会只选择一个并将其用于每个问题,而是采用几种不同的类型并以类似于您的数据的方式嵌套它们。

In your case I think you'd get along pretty well with 3 flow layouts, two horizontal contained in one vertical, that kind of GUI is exactly what flowLayout was made for and you won't have to add any fiddling with parameters. 在你的情况下,我认为你相处得很好3个流程布局,两个水平包含在一个垂直中,那种GUI正是flowLayout的用途,你不必添加任何参与的摆弄。

By the way, many problems can be solved by nesting Border and Flow layouts--Border can be exceptionally useful because of the way the middle acts to take as much space as it can, but leaving the 4 sides as much room as they need--in fact BorderLayout often only has 2 or 3 areas filled--but may easily nest other Border or flow layouts. 顺便说一句,许多问题可以通过嵌套边框和流程布局来解决 - 边框可以非常有用,因为中间的方式可以占用尽可能多的空间,但是留下四边尽可能多的空间 - - 实际上BorderLayout通常只填充2或3个区域 - 但可能很容易嵌套其他边框或流布局。 These lead to GUIs that require very little placement specifics but still resize naturally. 这些导致GUI需要非常少的放置细节,但仍然自然调整大小。

EDIT: Here is some working code of two flow layouts nested in a box layout. 编辑:这是一个嵌套在框布局中的两个流布局的工作代码。

It's groovy (not java) but the difference is purely syntax--with less clutter. 它是groovy(不是java),但区别在于纯语法 - 杂乱少。

Note the simplicty and lack of numbers to invent--it just works pretty much like you'd expect. 注意发明的简单和缺乏数字 - 它只是像你期望的那样工作。

import javax.swing.*
import java.awt.*

f=new JFrame()
f.setSize(300,200)
p=f.getContentPane()
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS))
p.add(panel("Button 1"))
p.add(panel("Button 2"))
f.show()

void addPanel(name) {
   def child=new JPanel()
   child.add(new JButton(name))
   child.add(new JTextField(20))
   return child
}

I personally like GridBagLayout . 我个人喜欢GridBagLayout

Here is an example to demonstrate: 这是一个示例:

在此输入图像描述

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class Test {

    private JFrame frame;
    private JTextField nameTextField, birthDateTextField;
    private JLabel nameLabel, birthDateLabel;

    public Test() {
        initComponents();
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
                    // If Nimbus is not available, you can set the GUI to another look and feel.
                }
                new Test();
            }
        });
    }

    private void initComponents() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridBagLayout());

        nameTextField = new JTextField(30);
        birthDateTextField = new JTextField(30);
        nameLabel = new JLabel("Name:");
        birthDateLabel = new JLabel("Birth Date:");

        GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.insets = new Insets(10, 10, 10, 10);

        gc.gridx = 0;
        gc.gridy = 0;
        frame.add(nameLabel, gc);

        gc.gridx = 1;
        gc.gridy = 0;
        frame.add(nameTextField, gc);

        gc.gridx = 0;
        gc.gridy = 1;
        frame.add(birthDateLabel, gc);

        gc.gridx = 1;
        gc.gridy = 1;
        frame.add(birthDateTextField, gc);

        frame.pack();
        frame.setVisible(true);
    }
}

GroupLayout suits your needs. GroupLayout适合您的需求。 Take a look at it. 看看吧。 It has the ability to auto-create gaps (or you can manage your own)and it's great for creating forms. 它具有自动创建间隙的能力(或者您可以管理自己的间隙),它非常适合创建表单。 It works either horizontally or vertically. 它可以水平或垂直工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM