将JPanel上的组件放在前面(Java) [英] Bring a component on a JPanel to front (Java)

查看:140
本文介绍了将JPanel上的组件放在前面(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在你问之前,我没有使用在这种情况下,布局经理。如果你有两个组件在另一个之上,你将如何改变它们已经被显示的顺序?



我有一个按钮,略微重叠在另一个之上组件(标签),由于缺乏空间。我在按钮之前添加了JLabel到表单,当表单加载时,它看起来很好。但是,当用户点击按钮时,JLabel会后退,使其大部分消失。有没有办法把它保持在前面?我已经尝试在按钮的ActionListener中放置 label.grabFocus(),但它没有成功。 解决方案当组件在面板上重叠的时候,你需要告诉面板,这样它可以确保它重新绘制组件在他们正确的ZOrder:



你通过覆盖JPanel的 isOptimizedDrawingEnabled()方法来返回 false


In VB, you can use zOrder. In .Net, it's .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. 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.

解决方案

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:

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

这篇关于将JPanel上的组件放在前面(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆