将 JPanel 上的组件放在前面(Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7010453/
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-10-30 18:16:21  来源:igfitidea点击:

Bring a component on a JPanel to front (Java)

javaswingfocusactionlistener

提问by David

In VB, you can use zOrder. In .Net, it's .SetChildIndex.

在 VB 中,您可以使用zOrder. 在 .Net 中,它是.SetChildIndex.

Before you ask, no I'm not using a layout manager in this case. If you have two components on top of each other, how do you change the order after they have already been displayed?

在你问之前,不,在这种情况下我没有使用布局管理器。如果您有两个相互叠加的组件,在它们已经显示之后如何更改顺序?

I have a button that slightly overlaps on top of another component (label), due to the lack of space. I added the JLabel to the form beforethe button, and when the form loads, it looks fine. However when the user clicks the button, the JLabel goes to the back, making a chunk of it disappear. Is there a way to keep it to the front? I have tried putting label.grabFocus()in the button's ActionListener but it did not work.

由于空间不足,我有一个按钮与另一个组件(标签)的顶部略微重叠。我在按钮之前的表单中添加了 JLabel ,当表单加载时,它看起来很好。但是,当用户单击该按钮时,JLabel 会移到后面,使其中的一大块消失。有没有办法让它保持在前面?我试过放入label.grabFocus()按钮的 ActionListener 但它没有用。

采纳答案by camickr

When compnents overlap on the panel then you need to tell the panel so it can make sure it repaints the components in their proper ZOrder:

当组件在面板上重叠时,您需要告诉面板以确保它以正确的 ZOrder 重新绘制组件:

You do this by overriding the isOptimizedDrawingEnabled()method of the JPanel to return false.

您可以通过覆盖isOptimizedDrawingEnabled()JPanel的方法来返回false

回答by Dragon8

you can also use the LayeredPane of your JFrame. With JFrame.getLayeredPane() you get a reference of the pane in use.

您还可以使用 JFrame 的 LayeredPane。使用 JFrame.getLayeredPane() 可以获得正在使用的窗格的引用。

the add method of LayeredPane has up to 2 additional int parameters. the first specified the Layer of the component you'd add and the second contols the order inside this layer.

LayeredPane 的 add 方法最多有 2 个额外的 int 参数。第一个指定您要添加的组件的层,第二个控制该层内的顺序。

You can find more infos at http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html#layeredpane

您可以在http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html#layeredpane找到更多信息

回答by sasidhar

Firstly, this appears to be a poor design. Though label.repaint()works, its not the way you should do it. You are not specifying explicitly how these components overlap one on the other. For that a better approach would be Layered Panes. The java's counterpart for zOrder is Layered Pane. This link should help you: http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

首先,这似乎是一个糟糕的设计。虽然label.repaint()有效,但这不是你应该做的。您没有明确指定这些组件如何相互重叠。为此,更好的方法是分层窗格。zOrder 的 java 对应物是分层窗格。此链接应该可以帮助您:http: //download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

just tell me if you have any problem.

如果您有任何问题,请告诉我。

回答by Adrien Clerc

It isn't that obvious to make overlapping components behave in the right way. The most supported way is to use an OverlayLayout within your panel.

让重叠组件以正确的方式运行并不是那么明显。最受支持的方法是在面板中使用 OverlayLayout。

回答by Intern87

I'm not sure, but would repaint work? bit of a hack if it does, maybe someone else could find a better way...

我不确定,但是重新粉刷会起作用吗?如果确实如此,那就有点黑客了,也许其他人可以找到更好的方法......