【问题标题】:GridBagLayout Alignment Issue with Button expanding Column WidthGridBagLayout 对齐问题与按钮扩展列宽
【发布时间】:2015-02-03 21:09:26
【问题描述】:

我正在尝试使用一些 JLabels 图标(框图标)和退出按钮创建一个界面。问题是,当我将按钮放在标签下时,它们会根据按钮的位置分开,如下所示:

GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
c.gridy = 0;
c.weighty = 1;
add(new JLabel(" "), c);

for(int i = 0; i < label.length; i++){  // Adding the JLabels       
    c.fill = GridBagConstraints.NONE;
    c.weighty = 0;
    c.gridy = 1;
    c.gridx++;
    add(label[i], c);
}
// Adding the button
c.gridy ++;
c.gridx = 2;    // Changing the value of gridx, will move the button to that location and it will split another jlabel.
c.weighty = 0.09;
add(eButton, c);
revalidate();
repaint();

【问题讨论】:

  • 为什么不将GridBagLayout 嵌套在一个面板中,该面板位于borderLayout 的中心,然后使用flowLayout 在另一个面板中添加退出按钮,然后可以放置borderLayout以南
  • 1) 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。 2) 提供最小尺寸的 ASCII 艺术或简单的 GUI 布局图,如果可调整大小,则具有更大的宽度和高度。
  • 将按钮放在 gridx 0 处,将网格宽度设置为 REMAINDER 并将锚点设置为 CENTRE
  • 同意@MadProgrammer 的评论,如果您想在任何内容之前/之后/上方/下方添加空格,只需修改插图即可。
  • 谢谢你们。 @MadProgrammer 为什么我不能在 jlabels 上使用锚 NORTH 或 CENTER ?

标签: java swing layout-manager gridbaglayout


【解决方案1】:

GridBagLayout 工作起来很像一张网格纸,它有行和列,组件放置在这些单元格中;您可以指定如何分配额外空间以及如何在这些单元格中对齐组件。

每一行和每一列的大小取决于该行/列中其他组件的布局要求,这意味着一个组件可能拥有比它需要的更多空间,因为同一行/列中的某些其他组件需要更多空间待布置。

您还可以允许一个组件跨越多个行/列,从而占用更多空间。

所以,基本上,你需要做的是告诉按钮它可以占据它所在行的所有列(或至少 5 列被标签占据)。然后,您需要告诉按钮它需要与其区域的中心对齐。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
            c.gridy = 0;
            c.weighty = 1;

            for (int i = 0; i < 5; i++) {  // Adding the JLabels       
                c.fill = GridBagConstraints.NONE;
                c.weighty = 0;
                c.gridy = 1;
                c.gridx++;
                c.insets = new Insets(4, 4, 4, 4);
                JLabel label = new JLabel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(20, 20);
                    }
                };
                label.setBorder(new LineBorder(Color.DARK_GRAY));
                add(label, c);
            }
            c.gridy++;
            c.gridx = 0;
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.anchor = GridBagConstraints.NORTH;
//          c.weighty = 0.09;
            add(new JButton("Exit"), c);
        }

    }

}

仔细查看How to Use GridBagLayout了解更多详情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2021-06-21
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多