在 JPanel 上居中 JLabel [英] Centering a JLabel on a JPanel

查看:49
本文介绍了在 JPanel 上居中 JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 区域.例如,请参阅对堆栈溢出问题的回答摆脱 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.

这篇关于在 JPanel 上居中 JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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