简体   繁体   English

我应该使用哪个LayoutManager?

[英]Which LayoutManager should I use?

布局

Please see the image attached. 请参阅附图。 I'm a beginner at Java GUI and was hoping someone could guide me in choosing a Layout Manager for GUI like this. 我是Java GUI的初学者,希望有人可以指导我选择这样的GUI布局管理器。 I know I might have to use nested layoutmanagers, but I'm not sure which would help me accomplish this job. 我知道我可能不得不使用嵌套布局管理器,但我不确定哪个可以帮助我完成这项工作。

Here's an idea: 这是一个想法:

  • Use a BorderLayout in the main JPanel. 在主JPanel中使用BorderLayout。 Set the JList at the CENTER part 在中心部分设置JList
  • On the EAST part of the above layout, add a new JPanel with a GridLayout of 6 rows, 2 columns 在上面布局的EAST部分,添加一个新的JPanel,其GridLayout为6行,2列
  • On the GridLayout, add each of the labels, buttons, text fields, etc. in the same order as they're defined - from top to bottom and from left to right. 在GridLayout上,按照它们定义的顺序添加每个标签,按钮,文本字段等 - 从上到下,从左到右。

Alternatively: you could use a 7x2 GridLayout and fill the two positions above the buttons with empty text fields, to separate the labels/fields above from the buttons below. 或者:您可以使用7x2 GridLayout并使用空文本字段填充按钮上方的两个位置,以将上面的标签/字段与下面的按钮分开。

check out DesignGridLayout , it will be perfectly suited for this form I think 看看DesignGridLayout ,我觉得它非常适合这种形式

just look at their example: 看看他们的例子:

在此输入图像描述

with just a few lines of clean code: 只需几行干净的代码:

    layout.row().grid(label("Last Name"))   .add(lastNameField) .grid(label("First Name"))  .add(firstNameField);
    layout.row().grid(label("Phone"))       .add(phoneField)    .grid(label("Email"))       .add(emailField);
    layout.row().grid(label("Address 1"))   .add(address1Field);
    layout.row().grid(label("Address 2"))   .add(address2Field);
    layout.row().grid(label("City"), 1)     .add(cityField);
    layout.row().grid(label("State"))       .add(stateField)    .grid(label("Postal Code")) .add(postalField);
    layout.row().grid(label("Country"), 1)  .add(countryField);
    layout.emptyRow();
    layout.row().center().add(newButton).add(deleteButton).add(editButton).add(saveButton).add(cancelButton);

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

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