【问题标题】:Another GridBagLayout Alignment另一个 GridBagLayout 对齐方式
【发布时间】:2016-08-25 09:32:10
【问题描述】:

我在这里阅读了很多关于这个问题的信息......但我似乎不知道如何解决这个问题。 我尝试的所有解决方案都不起作用。

但是让我们从头开始: 我正在使用 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 的锚点。它在那里....但不工作。 它在面板中间完美对齐。

如果它确实重要: 面板用作 SplitPane 的 LeftComponent:

public LeftMenu leftpanel = new LeftMenu(); 

splitPaneTrunks.setLeftComponent(leftpanel);

期待您的帮助。

这是我的侧边菜单的图片...水平居中。不应该这样。

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度。
  • 在 swingx 库中显示 Horizo​​ntalLayout
  • 我怀疑封闭元素没有使用它的完整垂直空间。我为解决此类问题所做的一件事是将层次结构中所有面板的背景颜色设置为不同的颜色。这使得某些类型的布局问题变得显而易见。

标签: java swing layout-manager gridbaglayout


【解决方案1】:

除了使用GridBagLayout,您可以使用JPanel 和布局BorderLayout 进行换行,如下所示:

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

【讨论】:

  • 感谢您的回答...似乎我描述的有点错误我需要什么...编辑了我的文字。为此表示歉意。我不想对齐页面顶部的对象。整个网格袋应在顶部对齐。网格袋中的所有元素都完全按照我的需要定位...但第一个对象不在 JPanel 的顶部。
  • @Marcus 你能发个截图吗?
  • 刚做...不知道,它可以很容易地添加到这里。
  • 告诉我为什么我没有想到这个???奇迹般有效。谢谢你的帮助。我真的应该更多地考虑根据需要包装不同的元素。
  • @Marcus 如果此答案解决了您的问题,请按勾将其标记为已回答
【解决方案2】:

这是我的侧边菜单的图片...水平居中。不应该这样。

我认为您的意思是,如果您希望组件从顶部显示,则不应垂直居中。

无论如何,我认为问题在于您的weighty 约束。至少其中一个组件必须为非零值,否则组件将垂直居中。

阅读 How to Use GridBagLayout 上的 Swing 教程部分。有一个关于 weightx/weighty 约束的部分将更详细地解释这一点。

【讨论】:

    【解决方案3】:

    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,这很容易。十到十五分钟的工作。

    截图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      • 1970-01-01
      • 2014-06-04
      • 2013-01-23
      • 2012-06-24
      相关资源
      最近更新 更多