簡體   English   中英

另一個 GridBagLayout 對齊方式

[英]Another GridBagLayout Alignment

我在這里閱讀了很多關於這個問題的信息......但我似乎不知道如何解決這個問題。 我嘗試過的所有解決方案都不起作用。

但讓我們從頭開始:我正在使用 Swing 構建我的界面並嘗試模塊化。 所以我的左側主菜單有一個類(擴展 JPanel)。 菜單是用 GridBagLayout 中的幾個按鈕構建的。

但是我無法讓這個布局與窗口(面板)的頂部對齊。 示例:面板頂部的標簽、下方的文本字段、文本字段下方的按鈕等。

請看我的代碼:

public class LeftMenu extends JPanel {
  public LeftMenu(){

    GridBagLayout gbl_panel = new GridBagLayout();
    gbl_panel.columnWidths = new int[] { 86, 0 };
    gbl_panel.rowHeights = new int[] {32, 32, 32, 32, 32, 32, 32, 32, 32 };
    gbl_panel.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
    gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
    setLayout(gbl_panel);

    JLabel lblEnterTrunkId = new JLabel("Enter Trunk ID");
    GridBagConstraints gbc_lblEnterTrunkId = new GridBagConstraints();
    gbc_lblEnterTrunkId.fill = GridBagConstraints.HORIZONTAL;
    gbc_lblEnterTrunkId.insets = new Insets(0, 0, 5, 0);
    gbc_lblEnterTrunkId.gridx = 0;
    gbc_lblEnterTrunkId.gridy = 0;
    gbc_lblEnterTrunkId.anchor = GridBagConstraints.NORTH;
    add(lblEnterTrunkId, gbc_lblEnterTrunkId);
    }
 }

標簽后面有一個文本字段和一些按鈕。 但我認為,這些不相關......如果它們是......它們大多看起來像標簽(只是它們不是標簽......我想你明白我的意思)

我讀過的所有指南都指向 GridBagConstraint 的錨點。 它在那里......但不工作。 它在面板中間完美地對齊。

如果確實重要:Panel 用作 SplitPane 的 LeftComponent:

public LeftMenu leftpanel = new LeftMenu(); 

splitPaneTrunks.setLeftComponent(leftpanel);

期待您的幫助。

這是我的側面菜單的圖片......水平居中。 不應該如此。

http://i.stack.imgur.com/NsBg2.png

您可以使用帶有布局BorderLayoutJPanel進行包裝,而不是使用GridBagLayout ,如下所示:

    setLayout(new BorderLayout());

    JPanel gridBagWrap = new JPanel();

    GridBagLayout gbl_panel = new GridBagLayout();
    gbl_panel.columnWidths = new int[] { 86, 0 };
    gbl_panel.rowHeights = new int[] {32, 32, 32, 32, 32, 32, 32, 32, 32 };
    gbl_panel.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
    gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
    gridBagWrap.setLayout(gbl_panel);

    JLabel lblEnterTrunkId = new JLabel("Enter Trunk ID");
    GridBagConstraints gbc_lblEnterTrunkId = new GridBagConstraints();
    gbc_lblEnterTrunkId.fill = GridBagConstraints.HORIZONTAL;
    gbc_lblEnterTrunkId.insets = new Insets(0, 0, 5, 0);
    gbc_lblEnterTrunkId.gridx = 0;
    gbc_lblEnterTrunkId.gridy = 0;
    gbc_lblEnterTrunkId.anchor = GridBagConstraints.NORTH;
    gridBagWrap.add(lblEnterTrunkId, gbc_lblEnterTrunkId);

    add(gridBagWrap, BorderLayout.NORTH);

這是我的側面菜單的圖片......水平居中。 不應該如此。

我認為您的意思是如果您希望從頂部顯示組件,它不應該垂直居中。

無論如何,我認為問題在於您的weighty約束。 對於至少一個組件,它需要非零,否則組件將垂直居中。

閱讀 Swing 教程中關於如何使用 GridBagLayout 的部分 有一個關於 weightx/weighty 約束的部分將更詳細地解釋這一點。

您對GridBagLayout管理器感到GridBagLayout並不奇怪。 它實際上是其最常提及的功能之一。 GridBagLayout來自九十年代,在組件之間使用像素固定的間隙,不可移植。 使用此管理器,您必須繁瑣地定義布局的每個單元格; 難怪人們感到困惑。

我建議使用MigLayout 如果不能使用,則GroupLayout

這是MigLayout的解決方案:

package com.zetcode;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;

/*
Demonstration of MigLayout manager.
Author: Jan Bodnar
Website: zetcode.com
 */
public class MigLayoutTrunkEx extends JFrame {

    public MigLayoutTrunkEx() {

        initUI();
    }

    private void initUI() {

        JLabel lbl = new JLabel("Enter Trunk ID");
        JTextField textField = new JTextField(10);

        JButton btn1 = new JButton("A");
        JButton btn2 = new JButton("B");
        JButton btn3 = new JButton("C");
        JButton btn4 = new JButton("D");
        JButton btn5 = new JButton("E");

        JTextArea area = new JTextArea(20, 25);
        area.setBorder(BorderFactory.createEtchedBorder());

        JLabel statusBar = new JLabel("Ready");

        createLayout(lbl, textField, btn1, btn2, btn3,
                btn4, btn5, area, statusBar);

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        setLayout(new MigLayout("", "[align center]", ""));

        add(arg[0], "split 7, flowy");
        add(arg[1]);
        add(arg[2]);
        add(arg[3]);
        add(arg[4]);
        add(arg[5]);
        add(arg[6]);
        add(arg[7], "grow, push, wrap");
        add(arg[8], "align left, growx, spanx");

        pack();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutTrunkEx ex = new MigLayoutTrunkEx();
            ex.setVisible(true);
        });
    }
}

一旦您了解MigLayout ,這很容易。 十到十五分鍾的工作。

截圖:

MigLayout 示例截圖

暫無
暫無

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

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