【问题标题】:Java GridBagLayout - How to position my components gap-less and one by one?Java GridBagLayout - 如何无间隙地一一定位我的组件?
【发布时间】:2012-11-08 08:58:24
【问题描述】:

我正在使用 GridBagLayout 通过以下代码放置我的 GUI 组件,希望组件一个接一个地放在一列中,没有任何间隙:

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestGUI extends JFrame{

    public TestGUI(){

        JPanel bigPanel = new JPanel(new GridBagLayout());
        JPanel panel_a = new JPanel();
        JButton btnA = new JButton("button a");
        panel_a.add(btnA);

        JPanel panel_b = new JPanel();
        JButton btnB = new JButton("button b");
        panel_b.add(btnB);

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weighty = 1D;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTH;
        bigPanel.add(panel_a, c);

        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        bigPanel.add(panel_b, c);

        this.add(bigPanel);
    }

    public static void main(String[] args) {

        TestGUI gui = new TestGUI();
        gui.setVisible(true);
        gui.pack();
    }
}

我希望面板将在列中一一显示。但现在我得到了这个:

由于我要在 bigPanel 中添加更多组件,并且需要对布局进行更多自定义,所以我需要使用 GridBagLayout 而不是其他布局。

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • +1 用于图像,以获得更好的帮助,尽快发布SSCCE,简短、可运行、可编译,并且仅关于 JFrame、JPanels 和 GBC

标签: java swing gridbaglayout


【解决方案1】:

您需要添加一个额外的组件,以便它填充剩余的可用空间并将两个按钮面板推到顶部。当您要添加更多组件时,您当然可以删除该组件。

另一个选项(不需要额外的组件)是为panel_banchor=NORTH 设置weighty=1.0,但是当您添加更多组件时,您必须更改它。

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGUI extends JFrame {

    public TestGUI() {

        JPanel bigPanel = new JPanel(new GridBagLayout());
        JPanel panel_a = new JPanel();
        JButton btnA = new JButton("button a");
        panel_a.add(btnA);

        JPanel panel_b = new JPanel();
        JButton btnB = new JButton("button b");
        panel_b.add(btnB);

        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        bigPanel.add(panel_a, c);
        bigPanel.add(panel_b, c);
        c.weighty = 1.0;
        // Temporary panel to fill the rest of the bigPanel
        bigPanel.add(new JPanel(), c);
        this.add(bigPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGUI gui = new TestGUI();
                gui.pack();
                gui.setVisible(true);
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 2018-07-30
    • 2012-09-29
    • 2013-08-01
    • 2011-08-02
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多