2016-08-25 52 views
-1

我在这里读了很多关于这个问题......但我似乎没有得到如何解决这个问题。 我试过的所有解决方案都不起作用。另一个GridBagLayout对齐白痴

但是让我们从开始开始吧: 我正在用Swing构建我的接口并尝试模块化。 所以我有一个类(扩展JPanel)为我的左主菜单。 菜单ist通过GridBagLayout中的多个按钮构建。

但我无法让这个layyout对齐到窗口的顶部(面板)。 Exampel:标签顶部的面板,下面的文本字段,文本字段下方的按钮等

请参阅我的代码:

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的锚点。它在那里......但不工作。 它在面板中间很精彩。

如果这非常重要: IST用作SplitPane的LeftComponent面板:

public LeftMenu leftpanel = new LeftMenu(); 

splitPaneTrunks.setLeftComponent(leftpanel); 

期待您的帮助。

这里是我的侧面菜单的图片...水平居中。因为它不应该。

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

+0

1)为了更好地帮助越早,张贴[MCVE]或[简要,独立的,正确的示例](http://www.sscce.org/)。 2)以最小尺寸提供ASCII艺术或简单的GUI图形*图形,并且如果可调整大小,则具有更大的宽度和高度。 –

+0

在swingx库中显示Horizo​​ntalLayout –

回答

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

感谢您的回答...似乎我描述了一些错误,我需要...编辑我的文本。为此事道歉。我不想对齐页面顶部的对象。整个网格包应该在顶部对齐。网格包中的所有元素都按照我需要的方式精确定位......但第一个对象不在JPanel的顶部。 – Marcus

+0

@Marcus你可以发布截图吗? – nyxaria

+0

只是做了......没有意识到,它可以很容易地添加到这里。 – Marcus

1

这里是我身边的菜单的图片...水平居中。因为它不应该。

我想你的意思是它不应该垂直居中,如果你想从顶部显示的组件。

在任何情况下,我认为问题是你的weighty约束。对于至少一个组件,它必须是非零的,否则组件将垂直居中。

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

0

经理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 example screenshot