簡體   English   中英

如何在Swing中最好地布置這些組件?

[英]How can I best lay out these components in Swing?

對於需要顯示圖像中顯示的組件的JPanel,最佳方法/ LayoutManager是什么? (請注意,當更改窗口大小時,字段應水平調整大小,而不是標簽)。

我目前正在嘗試(並且正在努力)使用GridBagLayout。 這是正確的方法還是有更好的方法? 我應該試圖分裂+征服單獨的部分嗎?

任何想法或建議將不勝感激。 :-)

http://img261.imageshack.us/img261/2091/layouthb6.png

PS如果你讀到這個並且沒有圖像,請在我解決時請耐心等待!

我會看看嵌套JPanels 將每一行作為單獨的JPanel然后只是堆疊JPanels將非常容易。

否則,您可以使用GridBagLayout 這是我用來更好地控制組件放置位置的那個。

使用GridBagLayout您可以通過遞增GridBagConstraint .gridx來布局行,然后重置GridBagConstraint.gridx = 0GridBagConstraint.gridy++以移動到下一行。

我會建議第三方免費布局管理器。 想到MigLayout (檢查鏈接頁面底部的示例表單),或者JGoodies FormLayout ,它特別擅長這些形式。

標准的布局管理器很好,但是對於像這樣的屏幕頻繁使用而言是不夠的(馬蒂斯設計師在Netbeans中使用的新GroupLayout並非一無所獲)。 鏈接的布局管理器更容易使用和維護恕我直言。

我肯定會使用DesignGridLayout

DesignGridLayout layout = new DesignGridLayout(this);
layout.row().grid(label0).add(field0);
layout.emptyRow();
layout.row().grid(label1).add(field1);
layout.row().grid(label2).add(field2);
layout.row().grid(label3).add(field3);
layout.row().grid(label4).add(field4);
layout.emptyRow();
layout.row().grid(label5).add(field5).grid(label6).add(field6);
layout.row().grid(label7).add(field7).grid(label8).add(field8);
layout.emptyRow();
layout.row().grid(label9).add(field9);

免責聲明:我是DesignGridLayout作者之一。

如果您可以使用外部代碼,請查看JGoodies布局管理器。

就個人而言,我發現GridBagLayout比它的價值更麻煩 - 我已經編寫了自己的布局管理器來避免它。

我個人更喜歡GroupLayout

即使您沒有使用GUI構建器,也可以手動使用布局,如果您理解這些概念,則可以非常簡單明了地使用。

TableLayout將是我的選擇,您可以使用TableLayout.FILL等常量來自動調整大小並指定固定的像素寬度。

TableLayout

您的布局看起來基本上由“線”組成。 所以我個人認為:

  • 使用Box.createVerticalBox()創建一個垂直框; 這將垂直堆疊其組件 - 實際上,您可以添加線條
  • 為每一行創建一個面板:我認為你可以使用帶有BorderLayout的JPanel(左邊的標簽,文本字段作為中心組件),或者再次使用Box.createHorizo​​ntalBox(),並為其添加標簽和文本字段,適當的最小/首選大小
  • 您可以在需要行分隔符的垂直框中添加“支柱”
  • 你的兩個標簽/字段組需要一些特殊處理,但它基本上只是一個水平框,里面有兩條“線”。

就個人而言,我從來沒有用駁船桿接觸GridBagLayout。 也許這只是我,但我發現並發算法和機器代碼編程比理解GridBagLayout更容易......

我曾經非常討厭做過搖擺布局,直到Netbeans推出他們的gui建設者。 你可以在這里看到一個演示。 http://rghosting.co.cc

從Java SE 6開始,您可以使用GroupLayout

它非常簡單,看起來就像你想要的那樣。

對於這個窗口:

替代文字http://img165.imageshack.us/img165/8237/sampleusinggrouplayoutca2.png

我有這個代碼:

    // Layout the components using the new GroupLayout added
    // in java 1.6.
    // Adding to the main frame
    private void layoutComponents(){
        JPanel panel = new JPanel();

        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        layout.setAutoCreateGaps(true);         
        layout.setAutoCreateContainerGaps(true);

        SequentialGroup hGroup = layout.createSequentialGroup();

        JLabel nameLbl  = new JLabel("Name");
        JLabel countLbl = new JLabel("Amount");
        JLabel dateLbl  = new JLabel("Date(dd/MM/yy)");
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(nameLbl).
                addComponent(countLbl).
                addComponent(dateLbl).
                addComponent(go));

        hGroup.addGroup(layout.createParallelGroup().
                addComponent(name).
                addComponent(count).
                addComponent(date));

        layout.setHorizontalGroup(hGroup);

        SequentialGroup vGroup = layout.createSequentialGroup();

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(nameLbl).
                addComponent(name));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(countLbl).
                addComponent(count));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(dateLbl).
                addComponent(date));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(go));
        layout.setVerticalGroup(vGroup);

        frame.add( panel , BorderLayout.NORTH );
        frame.add( new JScrollPane( textArea ) );
    }

GridBagLayout絕對是最好的選擇。 NetBeans Matisse可能是實現這一目標的最快方式。 如果您使用的是其他IDE,只需復制並粘貼自動生成的代碼即可。

當然是GridBagLayout

如果表單只是這個,那么你需要多個JPanel (如果你有按鈕,你可能還需要一個按鈕,具體取決於它們的位置/方式)

字段0 - 4和9的gridwidth設置為4,其他默認值(1,但不需要將其設置為1)

字段組之間的“空間”可以通過插圖實現。

(對於這種布局,至少)我不會建議第三方布局管理器。 GridBagLayout並不復雜。 它只需要(一點點)的時間來適應它。

Visual Editor + Eclipse可以輕松地為您完成這些工作。 (我對Netbeans及其搖擺開發不太滿意。嘗試過,但從未墜入愛河。也許,我太沉迷於Eclipse)

我認為,在所有標准布局中,GridBagLayout應該最適合您的需求。

您可以嘗試類似下面的示例,其中我使用addToLayout將復雜的GridBagConstraints構造函數簡化為我真正需要的內容。

如果您可以使用IDE,這可以使事情變得更容易(例如,NetBeans中的Matisse)。

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagLayoutTest extends JPanel {

    public GridBagLayoutTest() {
        setLayout(new GridBagLayout());
        addToLayout(new JLabel(     "Label 0" ), 0, 0, 0d, 1);
        addToLayout(new JTextField( "Field 0" ), 0, 1, 1d, 4);
        addToLayout(new JLabel(     "*"       ), 0, 5, 0d, 1);
        addToLayout(new JPanel()               , 1, 0, 0d, 1);
        addToLayout(new JLabel(     "Label 1" ), 2, 0, 0d, 1);
        addToLayout(new JTextField( "Field 1" ), 2, 1, 1d, 4);
        addToLayout(new JLabel(     "*"       ), 2, 5, 0d, 1);
        addToLayout(new JLabel(     "Label 2" ), 3, 0, 0d, 1);
        addToLayout(new JTextField( "Field 2" ), 3, 1, 1d, 1);
        addToLayout(new JLabel(     "*"       ), 3, 2, 0d, 1);
        addToLayout(new JLabel(     "Label 3" ), 3, 3, 0d, 1);
        addToLayout(new JTextField( "Field 3" ), 3, 4, 1d, 1);
        addToLayout(new JLabel(     "*"       ), 3, 5, 0d, 1);
    }

    private void addToLayout(java.awt.Component comp, int y, int x, double weightx, int gridwidth) {
        add(comp, new GridBagConstraints(x, y, gridwidth, 1, weightx, 0d,
                                         GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                                         new java.awt.Insets(2, 4, 2, 4), 0, 0 ) );
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new GridBagLayoutTest());
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
}

您可以將布局描述為HTML表格,然后使用此工具: http//www.onyxbits.de/content/blog/patrick/java-gui-building-gridbaglayout-manager-made-easy

將布局轉換為Java代碼以配置GridBagLayout。

暫無
暫無

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

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