簡體   English   中英

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

[英]What would be the best Layout Manager?

在此輸入圖像描述

我想做一個看起來像上面的gui。 現在我有一個面板,它將保存名稱標簽,名稱文本字段,brith日期標簽和生日文本字段。 我的問題是什么是最好的布局管理器在面板上使用,以便“名稱組件”行(標簽+文本字段)和“出生日期組件”行(標簽+文本字段),將垂直均勻地分布在小組。

我想過使用流布局,但這會導致兩行組件之間沒有間隙。 我想過使用網格布局,但我不知道兩行組件之間的間隙大小。

一個更復雜的方法......我考慮將名稱標簽和文本字段放在一個面板中,並將出生日期標簽和文本字段放在另一個面板中,然后使基本面板邊框布局和設置名稱為北,生日日期為南。 ..但是我仍然需要確保名稱組件在名稱面板中垂直居中,而birthdate組件在出生日期面板中垂直居中。

任何幫助表示贊賞。 目標是確保名稱組件行和出生日期組件行垂直展開,名稱組件垂直居中於上半部分,出生日期組件垂直居中於下半部分。 如果有什么問題讓人感到困惑,請告訴我我會嘗試改寫以便更好地理解。

我正在使用嚴格的Java swing與eclipse,沒有GUI構建器或類似的東西。

我喜歡MigLayout 在這種情況下,布局組件將非常容易,因為MigLayout具有類似於表的行為,並且組件將以這種方式排列。 這在QuickStartGuide中有所描述。

編輯:

這是一個小例子:

在此輸入圖像描述

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);
    }
}

我強烈建議你遠離布局經理設計的自動布局 - 它們可以靈活但是它們往往被用作錘子,如果你想要組件調整大小,那么它會非常笨拙自然的方式。

學習使用布局管理器是一門藝術。 您不會只選擇一個並將其用於每個問題,而是采用幾種不同的類型並以類似於您的數據的方式嵌套它們。

在你的情況下,我認為你相處得很好3個流程布局,兩個水平包含在一個垂直中,那種GUI正是flowLayout的用途,你不必添加任何參與的擺弄。

順便說一句,許多問題可以通過嵌套邊框和流程布局來解決 - 邊框可以非常有用,因為中間的方式可以占用盡可能多的空間,但是留下四邊盡可能多的空間 - - 實際上BorderLayout通常只填充2或3個區域 - 但可能很容易嵌套其他邊框或流布局。 這些導致GUI需要非常少的放置細節,但仍然自然調整大小。

編輯:這是一個嵌套在框布局中的兩個流布局的工作代碼。

它是groovy(不是java),但區別在於純語法 - 雜亂少。

注意發明的簡單和缺乏數字 - 它只是像你期望的那樣工作。

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
}

我個人喜歡GridBagLayout

這是一個示例:

在此輸入圖像描述

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適合您的需求。 看看吧。 它具有自動創建間隙的能力(或者您可以管理自己的間隙),它非常適合創建表單。 它可以水平或垂直工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM