【问题标题】:Componenets alignment in GridBagLayoutGridBagLayout 中的组件对齐
【发布时间】:2018-01-13 03:09:56
【问题描述】:

我正在使用 GridBagLayout 来对齐组件。实际上,我有两个按钮,我想像这样对齐:

想要的布局:

但是下面的代码会导致如下布局:

结果布局:

我的代码:

    iconAdd = new ImageIcon(getClass().getResource("../images/add.png"));
    add = new JButton(iconAdd);
    add.setPreferredSize(new Dimension(130, 100));
    add.setBorder(new LineBorder(Color.decode("#9b9999"), 1, true));
    add.setCursor(Cursor.getPredefinedCursor(12));
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(5, 5, 5, 5);
    pane.add(add, gbc);

    iconSearch = new 
    ImageIcon(getClass().getResource("../images/search.png"));
    search = new JButton(iconSearch);
    search.setCursor(Cursor.getPredefinedCursor(12));
    search.setPreferredSize(new Dimension(130, 100));
    search.setBorder(new LineBorder(Color.decode("#9b9999"), 1, true));
    gbc.gridx++;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(5, 5, 5, 5);
    pane.add(search, gbc);

任何帮助将不胜感激。

【问题讨论】:

标签: java swing layout-manager gridbaglayout


【解决方案1】:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class GridBagLayoutDemo extends JFrame{

    GridBagLayoutDemo(){

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWeights = new double[]{1.0, 1.0, 1.0};
        gridBagLayout.columnWidths = new int[]{0,0,300};
        getContentPane().setLayout(gridBagLayout);

        JButton button1 = new JButton("Long Button");
        GridBagConstraints c1 = new GridBagConstraints();
        c1.fill = GridBagConstraints.HORIZONTAL;
        c1.weightx = 0.0;
        c1.gridwidth = 3;
        c1.gridx = 0;
        c1.gridy = 0;
        getContentPane().add(button1, c1);

        JButton button2 = new JButton("Button 2");
        GridBagConstraints c2 = new GridBagConstraints();
        c2.weightx = 0.5;
        c2.gridx = 0;
        c2.gridy = 1;
        getContentPane().add(button2, c2);

        JButton button3 = new JButton("Button 3");
        GridBagConstraints c3 = new GridBagConstraints();
        c3.weightx = 0.5;
        c3.gridx = 1;
        c3.gridy = 1;
        getContentPane().add(button3, c3);

        pack();
        setVisible(true);
    }


    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GridBagLayoutDemo();
            }
        });
    }
}

【讨论】:

  • 解决不了你的问题,因为你没有发minimal reproducible example等等只能猜测这个贴出来的代码是怎么用的。这演示了一个工作布局,类似于您需要的。您需要通过将代码与工作演示进行比较来找出代码有什么问题,或者根据您的需要采用工作演示。
  • 你为什么使用 weightx = 0.5 ?
  • 您可以删除此行。它不会更改所需的布局。请注意列约束,以及使用GridBagConstraints 的新实例添加每个按钮的事实@
【解决方案2】:
gbc.weightx = 1;

你告诉布局给每个组件额外的空间。所以基本上每个组件都变成了框架大小的一半。

您真的只想为第二个按钮设置该约束,因此它会占用所有剩余空间。

阅读 Swing 教程中有关如何使用 GridBagLayout 的部分,该部分解释了应如何使用 weightx/y 约束。

另外,一个更简单的解决方案是使用FlowLayout。使用FlowLayout 创建一个面板。将按钮添加到面板。然后将面板添加到框架的BorderLayout.PAGE_START

【讨论】:

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