JPanel 翻转,FlowLayout 未按预期工作
前几天这段代码可以工作。 我更改了一些内容并重新运行它,但现在它无法按预期工作。 显然,我更改的某些内容改变了行为,但我已经返回并恢复了所有这些更改,但它仍然不起作用。 忽略这一点信息(首先),为什么此代码不在 JPanel 内放置 15x15 的 JLabels 网格?
gameBoard.setLayout(new FlowLayout());
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
JLabel tile = new JLabel("");
tile.setHorizontalAlignment(SwingConstants.CENTER);
tile.setPreferredSize(new Dimension(27, 27));
tile.setBorder(new EtchedBorder());
tile.setEnabled(false);
gameBoard.add(tile);
}
}
gameBoard 是通过 NetBeans 的 GUI Builder 定义的 JPanel。 它具有首选尺寸、最大尺寸(与首选尺寸相同)。 水平/垂直调整大小选项被禁用,但是当此代码运行时,每个按钮都会水平连续延伸而不会中断。
如果我理解正确,FlowLayout 应该在元素到达其包含元素的末尾时对其进行包装。 今天之前就发生过这种情况,我真的不知道为什么这种行为停止了?
The other day this code was working. I changed some things and re-ran it and now it doesn't work as intended. Obviously something I changed altered the behaviour, but I have gone back and reverted all of those changes and it still doesn't work. Disregarding that bit of information (to start), why does this code not place a 15x15 grid of JLabels inside of a JPanel?
gameBoard.setLayout(new FlowLayout());
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
JLabel tile = new JLabel("");
tile.setHorizontalAlignment(SwingConstants.CENTER);
tile.setPreferredSize(new Dimension(27, 27));
tile.setBorder(new EtchedBorder());
tile.setEnabled(false);
gameBoard.add(tile);
}
}
gameBoard is a JPanel defined through NetBeans' GUI Builder. It has a preferred size, a maximum size (same as preferred). The horizontal/vertical resizable options are disabled, yet when this code runs every button extends horizontally in a row without ever breaking.
If I understand correctly, FlowLayout is supposed to wrap elements when they reach the end of their containing element. This was happening before today, I don't really know why this behaviour stopped?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 GridLayout 会更好。
由于使用新的外观、不同的字体或某些 Java VM 升级,流程布局可能已发生变化。 不建议为此用途定义流布局,因为您依赖于可能因虚拟机或外观而异的基础大小。
It would be much better to use a GridLayout.
The flow layout may have changed due to using a new look and feel, different fonts or some Java VM upgrade. Defining a flow layout for this use is not recommended as you're reliant on underlying sizes that can vary across virtual machines or look and feels.
为了授人以渔:
修订控制系统很棒。 甚至您的系统也会定期备份源。
一种有用的调试技术是删除一些东西,直到它起作用为止。 设置边框是否会导致代码中断。 换句话说,您可以编写一个简单的程序来演示该问题吗?
In the interests of teaching a man how to fish:
Revision control systems are great. Even your system is making a back up of your source at frequent intervals.
A useful debugging technique is to take away things until it does work. Does setting a border cause you code to break. Going the other way, can you write a trivial program that demonstrates the problem.
我发现使用 TableLayout 取得了巨大的成功和控制,我会推荐给任何人。 您可以使用特定数量的像素定义列和行,也可以仅对每列和/或行使用
TableLayout.PREFERRED
。 非常漂亮和灵活的布局。 我们到处都用它。I've found great success and control using TableLayout and I would recommend it to anyone. You can define the columns and rows using specific number of pixels, or you can just use
TableLayout.PREFERRED
for each column and/or row. Very nice and flexible Layout. We use it everywhere.我想到了。 显然我必须在代码本身中设置preferredSize。 仅在 Netbeans 属性窗口中设置它是不够的。 奇怪的。
I figured it out. Apparently I had to set the preferredSize within the code itself. Just setting it in the Netbeans properties window is not enough. Strange.