【问题标题】:How do I put spaces between components in a GridBagLayout?如何在 GridBagLayout 中的组件之间放置空格?
【发布时间】:2015-02-09 18:58:06
【问题描述】:

在带有 BorderLayout 的 JFrame 中,我在窗口底部有一个“控制面板”(带有按钮和东西)。在这个“控制面板”的 JPanel 中,我想使用 GridBagLayout。 这是我现在的结果。

我正在考虑将布局划分为 3 行 x 8 列的表格。 在此配置中,“+”符号应该只占一个正方形,并且所有按钮都应该填满面板。

代码是这样的:

buttonsPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

removeCost = new JButton("-");
c.gridx = 5;
c.gridy = 0;
buttonsPanel.add(removeCost, c);

addCost = new JButton("+");
c.gridx = 7;
c.gridy = 0;
buttonsPanel.add(addCost, c);

text = new JLabel("Incasso");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(text, c);

cost = new JTextArea();
cost.setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
cost.setPreferredSize(new Dimension(80, 18));
c.gridx = 5;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(cost, c);

cancel = new JButton("Cancella");
c.anchor = GridBagConstraints.LINE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 2, 2, 30);
buttonsPanel.add(cancel, c);

enter = new JButton("Accetta");
c.anchor = GridBagConstraints.LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 5;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 30, 2, 2);
buttonsPanel.add(enter, c);

我做错了什么?

【问题讨论】:

标签: java swing whitespace layout-manager gridbaglayout


【解决方案1】:

您不能创建 GridBagConstrains 的新实例。我这样做了。 我执行你的代码。请看截图。

import javax.swing.*;
import java.awt.*;

public class app {
public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel buttonsPanel = new JPanel(new GridBagLayout());
    frame.add(buttonsPanel);
    // =====================
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    JButton removeCost = new JButton("-");
    c.gridx = 5;
    c.gridy = 0;
    buttonsPanel.add(removeCost, c);

    JButton addCost = new JButton("+");
    c.gridx = 7;
    c.gridy = 0;
    buttonsPanel.add(addCost, c);

    JLabel text = new JLabel("Incasso");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 3;
    buttonsPanel.add(text, c);

    JTextArea cost = new JTextArea();
    cost.setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
    cost.setPreferredSize(new Dimension(80, 18));
    c.gridx = 5;
    c.gridy = 1;
    c.gridwidth = 3;
    buttonsPanel.add(cost, c);

    JButton cancel = new JButton("Cancella");
    c.anchor = GridBagConstraints.LINE_START;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 3;
    c.insets = new Insets(0, 2, 2, 30);
    buttonsPanel.add(cancel, c);

    JButton enter = new JButton("Accetta");
    c.anchor = GridBagConstraints.LINE_END;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 5;
    c.gridy = 2;
    c.gridwidth = 3;
    c.insets = new Insets(0, 30, 2, 2);
    buttonsPanel.add(enter, c);

    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setSize(new Dimension(400, 400));
}
}

你需要有这种行为吗?

【讨论】:

  • 我没有创建 GridBagConstrains 的新实例。这很奇怪。这是相同的代码,但它在您的计算机上“更好”。我无法理解...
  • 什么是框架布局?可以使用 Look&Feel 吗?尝试粘贴它:尝试 { UIManager.setLookAndFeel("DefaultMetal"); } 捕捉 (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } 捕捉 (UnsupportedLookAndFeelException e) { e.printStackTrace(); }
【解决方案2】:

每次将 GridBagConstraints (c = new GridBagConstraints) 用作 add 方法的参数时,都需要创建一个新实例。

【讨论】: