Java - 控制 JPanels 的 Z 顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3763136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:34:50  来源:igfitidea点击:

Java - control Z order of JPanels

javaswinggraphicsawtz-index

提问by tybro0103

In short, my need is to have a background Image in my java app, and upon some event, create some other graphics on top of that image.

简而言之,我的需要是在我的 java 应用程序中有一个背景图像,并且在发生某些事件时,在该图像之上创建一些其他图形。

I was thinking I would use a JPanel to draw the background image in, add it at to my JFrame the start of program, and then add other JPanels on top of that upon certain events. The problem is Swing gives the JPanels added first the highest Z index, so what should be my background is showing up on top of everything.

我在想我会使用 JPanel 来绘制背景图像,在程序开始时将它添加到我的 JFrame 中,然后在某些事件的顶部添加其他 JPanel。问题是 Swing 为首先添加的 JPanels 提供了最高的 Z 索引,所以我的背景应该是显示在所有内容之上。

Is there any way to control the Z index/order of the JPanels, or am I going about this completely wrong?

有什么方法可以控制 JPanel 的 Z 索引/顺序,还是我完全错误?

采纳答案by Colin Hebert

You can use the setComponentZOrder()to handle Z-Order in your application.

您可以使用setComponentZOrder()来处理应用程序中的 Z-Order。



Resources :

资源 :

回答by Avall

Sounds strange to add mutiple JPanels and use z-order. I would suggest you either simple add ONE JPanel with the paintComponent(Graphics g) method overwritten, where you draw the correct image according to your events.

添加多个 JPanel 并使用 z-order 听起来很奇怪。我建议您简单地添加一个 JPanel 并覆盖 paintComponent(Graphics g) 方法,您可以在其中根据事件绘制正确的图像。

Or use the CardLayout, and add JLabels (with different images), then when your event triggers, use

或者使用 CardLayout,并添加 JLabels(具有不同的图像),然后当您的事件触发时,使用

CardLayout cl = (CardLayout)getLayout();
cl.show(this, "card3");

to show the correct JLabel.

显示正确的 JLabel。

回答by camickr

I was thinking I would use a JPanel to draw the background image in, add it at to my JFrame the start of program,

我想我会使用 JPanel 来绘制背景图像,在程序开始时将它添加到我的 JFrame 中,

Sounds reasonable.

听起来很合理。

and then add other JPanels on top of that upon certain events. The problem is Swing gives the JPanels added first the highest Z index, so what should be my background is showing up on top of everything.

然后在某些事件的基础上添加其他 JPanel。问题是 Swing 为首先添加的 JPanels 提供了最高的 Z 索引,所以我的背景应该是显示在所有内容之上。

Adding a panel to a panel is just like adding another component to the panel. The child component will be painted on top of the parent panel. There is no need to play around with Z-Order.

向面板添加面板就像向面板添加另一个组件一样。子组件将绘制在父面板的顶部。没有必要玩弄 Z-Order。

The key is to set a layout manager on the panel with the image. Then each panel you add to the image panel must be made non-opaque so that the background image will be painted.

关键是在带有图像的面板上设置布局管理器。然后,您添加到图像面板的每个面板都必须是非不透明的,以便绘制背景图像。

Maybe the Background Panelcan help you out. It automatically makes any component added directly to the panel non-opaque.

也许背景面板可以帮助你。它会自动使直接添加到面板的任何组件变得不透明。

回答by wolfcastle

The JLayeredPaneis designed for just this purpose. It allows you to control the Z-order of components.

JLayeredPane的是专为刚刚这一目的。它允许您控制组件的 Z 顺序。