两个 JPanel 重叠?

发布于 2024-10-14 04:44:48 字数 232 浏览 2 评论 0原文

这是我正在处理的代码: http://pastie.org/1501054 当您运行此代码时,对于由于某些原因,两个面板重叠。为什么会这样呢?我有什么办法可以修复它吗?

作为参数提供的 ActionListener 与程序的这一部分无关。

另外,在哪里可以找到使用 Eclipse 的优秀 swing 教程?

This is the code I'm dealing with: http://pastie.org/1501054 When you run this, for some reason, the two panels overlap. Why is this so? Any way I can fix it?

The ActionListener that is provided as an argument is irrelevant to this part of the program.

Also, where can I find a good swing tutorial that uses Eclipse?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空‖城人不在 2024-10-21 04:44:48

当我运行您的代码时,我没有看到重叠的窗格。我不知道你是否可以将你看到的效果的屏幕截图贴出来。

至于Eclipse中的教程。我建议在 Eclipse 中使用 Windows Builder pro 来构建 Java GUI。这是一个优秀的产品(免费),并且网站上有非常好的文档。

I am not seeing overlapping panes when I run your code. I wonder if you can post a screenshot of the effect that you see.

As for tutorials in Eclipse. I would suggest using Windows Builder pro in Eclipse for building Java GUI. This is an excellent product (free) and very good documentation on the site.

横笛休吹塞上声 2024-10-21 04:44:48

虽然我不确定您希望最终结果是什么样子,但这里有一些我的建议。

JFrame contentPane 的代码片段中,

warningPanel.add(warningLabel);
contentPane.add(warningPanel);
pack(); 

在将警告面板添加到作为 JFrame.getContentPane() 返回的容器的

默认情况下 JFrame 使用 BorderLayout,因此

contentPane.add()

相同

contentPane .add(, BorderLayout.CENTER)

您还以相同的方式添加 mainPanel,并且不能有两个具有相同约束的组件,因此请将 warningPanel 设置为 BorderLayout.NORTH

contentPane.add(warningPanel, BorderLayout.NORTH)

并且还要删除该代码片段中对 pack() 的调用,因为您稍后会在代码中调用它。

希望这有帮助。


PS

至于在 Eclipse 中构建 GUI,上一个关于 Eclipse GUI Builder 插件 的问题可能有用。我无法谈论在 Eclipse 中使用 Swing 的教程,但快速的 google 搜索可以找到 本教程使用 Eclipse 可视化编辑器项目

While I'm not sure what you want the final result to look like, here's a few of my suggestions.

In the snippet where you add the warning panel to the JFrame

warningPanel.add(warningLabel);
contentPane.add(warningPanel);
pack(); 

contentPane being the container returned by JFrame.getContentPane()

JFrames by default use the BorderLayout, and so,

contentPane.add(<someComponent>)

is identical to

contentPane.add(<someComponent>, BorderLayout.CENTER)

You also add the mainPanel the same way, and you can't have two components with the same constraints, so instead set the warningPanel to BorderLayout.NORTH

contentPane.add(warningPanel, BorderLayout.NORTH)

And also remove the call to pack() in that code snippet, since you call it later on in your code.

Hope this helps.


PS

As for GUI building in Eclipse, this previous question on Eclipse GUI Builder plugins maybe of use. I can't speak for tutorials on using Swing in Eclipse, but a quick google search digs up this tutorial using the Eclipse Visual Editor project

故事和酒 2024-10-21 04:44:48

当您说您想要一个好的 Eclipse Swing 教程时,您想要 GUI 构建器还是 Swing 教程? Swing 就是关于布局的。一旦你把它记下来,那就是小菜一碟了。只需从流程布局开始。
如果您需要 GUI 构建器,请使用 Netbeans。太不可思议了!

至于重叠,不是重叠,而是覆盖。默认位置 BorderLayout.CENTER 中只能有一个面板。
如果您希望它们并排,请执行 getContentPane().setLayout(new FlowLayout());
或者只是 add(panel, BorderLayout.SOUTH)

When you say you want a good swing tutorial for eclipse, do you want a GUI builder or a tutorial on Swing? Swing is all about layouts. Once you get that down, it's a piece of cake. Just start with flow layouts.
If you want a GUI builder, use Netbeans. It's incredible!

As for the overlap, it's not an overlap, but an overwrite. You can only have one panel in BorderLayout.CENTER, the default location.
If you want them side by side, do getContentPane().setLayout(new FlowLayout());
Or just add(panel, BorderLayout.SOUTH)

友谊不毕业 2024-10-21 04:44:48

是的,我看到警告标签重叠在第一行上,

这是因为您没有指定内容窗格的布局。
这是修复它的方法

contentPane.setLayout(new GridLayout(2,1));

,在下面添加这一行

Container contentPane = getContentPane();

,我认为您应该将警告面板更改为 gridlayout 1 和 1 而不是 0 和 1,因为您仍然有 1 行和 1 列

Yes I see the warning label is over lapping the first row

That is because You didnt specify the layout for your contentpane.
Heres how you fix it

contentPane.setLayout(new GridLayout(2,1));

add this line under

Container contentPane = getContentPane();

and I think you should change your warning panel to gridlayout 1 and 1 not 0 and 1 because you still have 1 row and 1 column

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文