java - 如何在 GridBagLayout 中的组件之间放置空格?

标签 java swing whitespace layout-manager gridbaglayout

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

enter image description here

我正在考虑将布局划分为 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);

我做错了什么?

最佳答案

您无法创建 GridBagConstrains 的新实例。我就这么做了。 我执行你的代码。请看截图。 enter image description here

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));
}
}

你需要有这种行为吗?

关于java - 如何在 GridBagLayout 中的组件之间放置空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28417216/

相关文章:

java - 简单的 P2P 服务器,对象从服务器中继后损坏

java - java代码是如何编译的

java - 从 paintComponent 方法调整大小

c - scanf() 格式字符串中的空白

html - 如何给溢出的文本最少 2 行的空间?

java - OpenGL 中场景中其他对象的反射

java - 将 ArrayList 中的名称写入 XML 文件

java - 如何在GUI中创建多个框架或窗口?

java - CardLayout性能?

php - 修剪 PHP 正则表达式中的子字符串