簡體   English   中英

Java GridBagLayout

[英]Java GridBagLayout

我的任務是使用Swing在Java中設計一個基本的用戶界面,但是我堅持不懈地解決它。 我想創建類似於此的東西但是我嘗試使用GridBagLayout導致了一些非常混亂的東西。 任何人都可以提供一些關於如何像這樣布局我的GUI的技巧嗎?

我有一個JTabbedPane,我添加了兩個選項卡,並為這兩個選項卡中的每一個添加了一個包含我的控件的JPanel。 我怎么想把它說出來

我會推薦這個

  • 整個GUI使用BorderLayout,
  • JTable在JScrollPane中,應該放在BorderLayout.CENTER中。
  • 包含標簽,字段和按鈕的頂部JPanel放置在BorderLayout.PAGE_START中。
  • 頂部JPanel也可以使用BorderLayout,並可以按住BorderLayout.PAGE_END位置的按鈕。
  • 按鈕將由GridLayout(1,0,x,0)使用JPanel保持,其中x是按鈕之間的間隙
  • 標簽和JTextField位於使用GridBagLayout的JPanel中,並放置在BorderLayout.CENTER位置的頂部JPanel中。
  • 您不是盲目地遵循這些建議,而是嘗試並使用嵌套JPanel的不同組合,每個組合都使用自己的布局。
  • 你也看看這個鏈接

這是我建議的:

  • 使用帶有GridLayout(3,2)的JPanel pTextBox來保存所有標簽+文本框
  • 使用JPanel pButtons和GridLayout(1,3)或BoxLayout(水平)按住所有按鈕
  • 使用帶有BoxLayout(垂直)的JPanel pAll pTextBox來保存pTextBoxpButtons和Table。
  • 使用struts,gluees和min / max / prefererd尺寸來調整間距/調整大小的行為。

另請查看: http//docs.oracle.com/javase/tutorial/uiswing/layout/visual.html,以准確比較您的需求。

有一個組件JTable想要占用窗口中的所有可用空間。 這意味着需要一個BorderLayout,其中包含該BorderLayout的BorderLayout.CENTER中包含JTable的JScrollPane。 其他組件將位於BorderLayout.PAGE_START中的另一個JPanel中

在這個新的JPanel中,沒有組件需要垂直調整其大小,所以我沒有看到BorderLayout的必要性。 我會用垂直的BoxLayout組合它。 在此面板中再插入兩個,一個用於標簽和文本字段的GridBagLayout,以及一個用於按鈕的FlowLayout,具有中心對齊和一些水平間隙。 我更喜歡使用GridLayout的GridLayout嵌入式按鈕,因為如果您調整主面板的大小,使用FlowLayout按鈕將保持它們之間的相同距離。

這里有一些代碼......我無法正確顯示JTextFields所以你必須解決這個問題。

主要:

    import javax.swing.SwingUtilities;

public class Main {

    public static void main(String [] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                Panel panel = new Panel();
            }

        });

    }

}

面板:

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

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

public class Panel extends JPanel{

    private JFrame frame; 

    private JLabel label1;
    private JLabel label2; 
    private JLabel label3;

    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;

    private JButton button1;
    private JButton button2;
    private JButton button3;

    private JTable table;


    public Panel() {

        label1 = new JLabel("label1");
        label2 = new JLabel("label2");
        label3 = new JLabel("label3");

        textField1 = new JTextField("textField1", 20);
        textField2 = new JTextField("textField2", 20);
        textField3 = new JTextField("textField3", 100);

        button1 = new JButton("Hello");
        button2 = new JButton("Goodbye");
        button3 = new JButton("Love");

        table = new JTable(20,20);

        frame = new JFrame("My application");
        frame.setSize(1000, 1000);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        this.setOpaque(true);

        setLayout(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();

        gc.weightx = 1;
        gc.weighty =1;
        gc.fill = GridBagConstraints.NONE;

        gc.gridx = 0;
        gc.gridy = 0;
    //  gc.anchor = GridBagConstraints.WEST;
        add(label1, gc);

        gc.gridx = 2;
        gc.gridy = 0;
        //gc.anchor = GridBagConstraints.EAST;
        add(textField1, gc);

        gc.gridx = 0;
        gc.gridy = 1;

        //gc.anchor = GridBagConstraints.WEST;
        add(label2, gc);

        gc.gridx = 2;
        gc.gridy = 1;
        //gc.anchor = GridBagConstraints.EAST;
        add(textField2, gc);

        gc.gridx = 0;
        gc.gridy = 2;
        //gc.anchor = GridBagConstraints.WEST;
        add(label3, gc);

        gc.gridx = 2;
        gc.gridy = 2;
        //gc.anchor = GridBagConstraints.EAST;
        add(textField3, gc);

        gc.gridx = 0;
        gc.gridy = 3;
        //gc.anchor = GridBagConstraints.WEST;
        add(button1, gc);

        gc.gridx = 1;
        gc.gridy = 3;
        gc.anchor = GridBagConstraints.CENTER;
        add(button2, gc);

        gc.gridx = 2;
        gc.gridy = 3;
    //  gc.anchor = GridBagConstraints.EAST;
        add(button3, gc);

        gc.gridx = 1;
        gc.gridy = 4;
        gc.anchor = GridBagConstraints.CENTER;
        add(table, gc);

        frame.add(this);
    }

}

暫無
暫無

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

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