簡體   English   中英

使用Java Swing進行布局

[英]Layout using Java Swing

我試圖找出哪種是最好的布局。 我有一個通過將JPanel擴展為以下對象而創建的grid

public class TestPane extends JPanel{
    // code
}

我需要

  • 標簽以顯示一些文本/數字。
  • 一個供用戶選擇選項的下拉列表。
  • 用戶輸入一些參數的文本字段。

編輯

大致布局

這是該圖的草稿。 抱歉,沒有這么整潔。

關於使用哪種布局的任何建議。

請注意,標簽和grid隨着算法的計算而不斷更新。

編輯- grid組件很大-所以目前我在想它必須在左邊或右邊-其余組件可以在它旁邊的不同行中。

我開始將GridBagLayout與遇到的一些示例代碼一起使用,但是不清楚如何向其中添加網格:

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;


public class GridBagLayoutExample {
JFrame guiFrame;


public static void main(String[] args) {

     //Use the event dispatch thread for Swing components
     EventQueue.invokeLater(new Runnable()
     {

        @Override
         public void run()
         {

             new GridBagLayoutExample();         
         }
     });

}

public GridBagLayoutExample()
{ 
    guiFrame = new JFrame();

    //make sure the program exits when the frame closes
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setTitle("GridBagLayout Example");
    guiFrame.setSize(600,300);

    //This will center the JFrame in the middle of the screen
    guiFrame.setLocationRelativeTo(null);

    //creating a border to highlight the component areas
    Border outline = BorderFactory.createLineBorder(Color.black);

    //create GribBagLayout and the GridBagLayout Constraints
    GridBagLayout gridBag = new GridBagLayout();
    GridBagConstraints cons = new GridBagConstraints();

    cons.fill = GridBagConstraints.BOTH;

    JPanel compPanel = new JPanel();
    compPanel.setLayout(gridBag);

    cons.gridx = 2;
    cons.gridy = 2;
    JLabel randomLbl = new JLabel("In Xanadu did Kubla Khan, "
            + "A stately pleasure-dome decree");
    randomLbl.setBorder(outline);
    gridBag.setConstraints(randomLbl, cons);
    compPanel.add(randomLbl);

    cons.ipady = 100;
    cons.ipadx = 100;
    cons.weighty = 1.0;
    cons.gridx = 0;
    cons.gridy = 0;
    JLabel tallLbl = new JLabel("Tall and Long");
    tallLbl.setBorder(outline);
    gridBag.setConstraints(tallLbl, cons);
    compPanel.add(tallLbl);

    cons.ipady = 50;
    cons.ipadx = 100;
    cons.weightx = 0;
    cons.gridx = 0;
    cons.gridy = 1;
    JButton hello = new JButton("Hello");
    gridBag.setConstraints(hello, cons);
    compPanel.add(hello);

    cons.ipady = 100;
    cons.ipadx = 10;
    cons.gridx = 1;
    cons.gridy = 1;
    JButton goodbye = new JButton("GoodBye");
    gridBag.setConstraints(goodbye, cons);
    compPanel.add(goodbye);

    cons.weightx = 0;
    cons.gridx = 0;
    cons.gridy = 2;
    JButton eh = new JButton("eh?");
    gridBag.setConstraints(eh, cons);
    compPanel.add(eh);


    guiFrame.add(compPanel);
    guiFrame.setVisible(true);

}

}

我也在考慮哪種方式可以使外觀看起來更好。 我應該將網格與標簽和文本框分開放置還是一起放置? 從設計的角度來看,哪個是推薦的?

花一點時間,將您的UI分解為職責范圍...

在您建議的布局中,您有三個主要區域,例如...

主要

您也有一個子組,例如...

子

在我看來,這帶來了根據您的需要至少兩個或三個布局管理器的可能性。 這通常稱為復合布局。

從“網格”區域開始。

根據您的需要,主網格可以是GridBagLayoutGridLayout 這將創建主GridPanel

您可以將其添加到另一個將使用BorderLayout JPanel ,並將其添加到中心位置。

在此面板中,您將“ Current state”組件添加到SOUTH位置。 這形成GridStatePanel

然后,您將創建另一個JPanel並可能使用GridBagLayout將您的字段(黃色部分)添加到其中,這將形成FieldsPanel

然后,您將創建另一個JPanel並可能使用BorderLayout ,將FieldsPanel添加到WEST位置,並將GridStatePanel到中心。 這將形成您的基本面板或主要面板...我將其稱為MainContentPane ...,以便我們知道我們在說什么。

從那里,為它做客,創建另一個JPanel NORTH位置,添加“ Title”組件,然后將MainContentPane添加到中心。

這是一個非常強大且重要的概念,很難找到一個能夠一次布局整個布局的布局管理器。

如果您使用GridBagLayout來完成整個操作,您將會非常困惑。 我傾向於更喜歡排版。 這是您的布局在我看來:

  1. 您最外面的面板具有帶有West和Center的BorderLayout。 (請記住,使用BorderLayout時,拉伸窗口時,中間的東西會擴展;而西方(或東方,北方,南方)的東西不會擴展)

  2. 在中心內,您有一個JPanel,它也具有BorderLayout。 在其北部是JLabel(“ Title”)。 桌子中間是桌子。 南部是JLabel(“當前狀態”)

  3. 在西部,您有一個具有另一個BorderLayout的JPanel。 在北部是一個帶有GridLayout(3,2)的JPanel,帶有JLabels(Label1,Label2,Label3)和關聯的文本字段。 在中心是您的下拉列表(也許?從圖中不清楚)。 在南部是按鈕。 (並且,您可能想限制按鈕的大小,否則它將占用面板的整個寬度)

使用諸如GridBagLayout之類的布局管理器來組織JObject,例如按鈕,文本字段和組合框

暫無
暫無

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

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