gpt4 book ai didi

Java(垂直)调整随机间隙的大小

转载 作者:行者123 更新时间:2023-11-30 03:58:09 32 4
gpt4 key购买 nike

我有以下代码:

public Frame() {
super();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new GridLayout(0, 3));
for (int i = 0; i < 30; i++) {
add(new JLabel("label"));
}

}

所有这些都工作得很好,直到您开始垂直调整框架大小。
由于某种原因,它会在标签上方和下方产生间隙,当放大框架时,间隙会再次增大和缩小。

这一点最值得注意的是,当您进一步向下拉框架的下侧时,标签的顶线会不断上下移动。

为了澄清,these are the problem areas .
红色的两个区域也已填充,而我希望它们为空。

知道如何摆脱这个空白空间吗?
谢谢

最佳答案

For some reason it creates a gap above and under the labels that grows and shrinks again when enlarging the frame.

当您打包框架时,使用 GridLayout 时组件会以其首选大小显示。

但是,当您增加框架的大小时,GridLayout 会尝试填充所有可用空间。这意味着所有组件都会增长。

I'm not sure if this is the same when horizontally resizing, at the very least it isn't that obvious.

这并不明显,因为您只有一列标签。将 GridLayout 更改为多列,看看会发生什么。

how to fix this?

使用不同的布局管理器。也许是 BoxLayout 或 GridBagLayout。请参阅 Layout Managers 上的 Swing 教程了解更多信息和示例。

关于Java(垂直)调整随机间隙的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22597259/

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