gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:23:42 24 4
gpt4 key购买 nike

在带有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com