将JLabel置于JPanel上 [英] Centering a JLabel on a JPanel
问题描述
我正在使用NetBeans GUI构建器来处理我的布局(我对LayoutManagers很糟糕)并且我正在尝试放置一个简单的JLabel,以便它始终在其父JPanel中居中(水平)。理想情况下,即使JPanel被调整大小,这也会保持正确,但是如果这是一个疯狂的编码量,那么当JPanel首次创建时,它就足以居中。
I'm using the NetBeans GUI builder to handle my layout (I'm terrible with LayoutManagers) and am trying to place a simple JLabel so that it is always centered (horizontally) inside its parent JPanel. Ideally, this would maintain true even if the JPanel was resized, but if that's a crazy amount of coding than it is sufficient to just be centered when the JPanel is first created.
我自己很难尝试处理布局,但由于NetBeans GUI Builder自动生成不可变代码,因此我无法弄清楚如何进行这种中心化,而且我无法在线找到任何帮助我的东西。
I'm bad enough trying to handle layouts myself, but since the NetBeans GUI Builder autogenerates immutable code, it's been impossible for me to figure out how to do this centering, and I haven't been able to find anything online to help me.
感谢任何能引导我朝正确方向前进的人!
Thanks to anybody who can steer me in the right direction!
推荐答案
使用 Borderlayout ,你可以放任何 JComponents 到CENTER区域。有关示例,请参阅 Stack Overflow问题的答案摆脱JPanels之间的差距 。这应该有效。
By using Borderlayout, you can put any of JComponents to the CENTER area. For an example, see an answer to Stack Overflow question Get rid of the gap between JPanels. This should work.
这篇关于将JLabel置于JPanel上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!