简体   繁体   English

如何在Java Swing中重叠面板?

[英]How to overlap panels in Java Swing?

I have a screen in my application. 我的应用程序中有一个屏幕。 The layout of the screen is shown in the attached image file. 屏幕的布局显示在随附的图像文件中。 在此处输入图片说明

I have to add upto 5 labels in Panel1111 . 我必须在Panel1111最多添加5个标签。 But, When I try to add labels in Panel1111 , the Panel11 resizes and Panel12 shifts downwards to give space to Panel11 . 但是,当我尝试在Panel1111中添加标签时, Panel11会调整大小,而Panel12会向下移动以为Panel11留出空间。

I want to overlap content of Panel1111 on Panel12. 我想在Panel12上重叠Panel1111的内容。

How can I achieve it? 我该如何实现?

Layout details: 布局细节:

  1. Panel1 : BorderLayout Panel1:BorderLayout
  2. Panel11: OverLayLayout Panel11:OverLayLayout
  3. Panel111: GridBagLayout Panel111:GridBagLayout

Using JLayeredPane. 使用JLayeredPane。 Go to Oracle Java website, and go through the tutorial: How to Use Layered Panes 转到Oracle Java网站,并完成以下教程: 如何使用分层窗格

Java's Layout Managers by default try to show all information that is inside them. Java的布局管理器默认情况下会尝试显示其中的所有信息。 If you say you want two panels to overlap, this essentially means that the lower one cannot be seen fully, and also not interacted with in the hidden/overlapped part. 如果您说要使两个面板重叠,这实际上意味着下面的面板不能完全看到,也不能在隐藏/重叠的部分中进行交互。 Then, this part of the panel doesn't make sense any more. 然后,面板的这一部分不再有意义。 So you should probably rethink your GUI. 因此,您可能应该重新考虑您的GUI。

If you want it to overlap only at certain times, and the user can define when it should overlap and when not, then you'll need to handle that manually by using no Layout Manager at all, but position the elements yourself. 如果您希望它仅在特定时间重叠,并且用户可以定义何时应该重叠以及何时不重叠,那么您将需要通过完全不使用布局管理器来手动处理,而自己放置元素。 Oracle provides some hints how to do that: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html . Oracle提供了一些有关如何执行此操作的提示: http : //docs.oracle.com/javase/tutorial/uiswing/layout/none.html In the end, you might end up writing your own, custom Layout Manager to handle the resizing of the panels. 最后,您可能最终会编写自己的自定义布局管理器来处理面板的调整大小。

Note: only the the layout of Panel 1 must be manually managed. 注意:只有面板1的布局必须手动管理。 The other panels can likely be handled by a LayoutManager again. 其他面板可能会再次由LayoutManager处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM