简体   繁体   English

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

[英]Bring a component on a JPanel to front (Java)

In VB, you can use zOrder .在 VB 中,您可以使用zOrder In.Net, it's .SetChildIndex .在.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 before the button, and when the form loads, it looks fine.我在按钮之前将 JLabel 添加到表单中,当表单加载时,它看起来很好。 However when the user clicks the button, the JLabel goes to the back, making a chunk of it disappear.但是,当用户单击按钮时,JLabel 会退到后面,从而使其中的一部分消失。 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.我曾尝试将label.grabFocus()放在按钮的 ActionListener 中,但它不起作用。

you can also use the LayeredPane of your JFrame.您也可以使用 JFrame 的 LayeredPane。 With JFrame.getLayeredPane() you get a reference of the pane in use.使用 JFrame.getLayeredPane() 您可以获得正在使用的窗格的参考。

the add method of LayeredPane has up to 2 additional int parameters. LayeredPane 的 add 方法最多有 2 个额外的 int 参数。 the first specified the Layer of the component you'd add and the second contols the order inside this layer.第一个指定要添加的组件的层,第二个控制该层内的顺序。

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找到更多信息

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 .为此,您可以重写 JPanel 的isOptimizedDrawingEnabled()方法以返回false

Firstly, this appears to be a poor design.首先,这似乎是一个糟糕的设计。 Though label.repaint() works, its not the way you should do it.虽然label.repaint()有效,但它不是你应该这样做的方式。 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. zOrder 的 java 对应物是分层窗格。 This link should help you: http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html此链接应该可以帮助您: http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

just tell me if you have any problem.如果您有任何问题,请告诉我。

I'm not sure, but would repaint work?我不确定,但会重新粉刷工作吗? bit of a hack if it does, maybe someone else could find a better way...如果确实如此,那就有点hack了,也许其他人可以找到更好的方法...

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。

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

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