删除 TitledBorder 周围的间距
javax.swing.border.TitledBorder
在边框边缘周围引入 2 个像素的间距。这非常烦人,因为它破坏了与周围组件的对齐。
如何去掉这个间距?
我正在寻找适合任何外观和感觉的解决方案。
A javax.swing.border.TitledBorder
introduces a spacing of 2 pixels around the edges of the border. This is quite annoying because it breaks the alignment with the components around.
How do you remove this spacing?
I'm looking for a solution that works for any look and feel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这个边缘宽度是硬编码在 TitledBorder 类中的。所以你不能删除这个间距。
但是您可以尝试扩展此类(覆盖方法“void PaintBorder(Component, Graphics, int, int, int, int)”或可能是“Insets getBorderInsets(Component, Insets)”)或从头开始实现您自己的边框。
Unfortunately this edge width is hardcoded in TitledBorder class. So you can not remove this spacing.
But you can try to extend this class (override methods "void paintBorder(Component, Graphics, int, int, int, int)" or may be "Insets getBorderInsets(Component, Insets)") or implement your own border from scratch.
这不是对您问题的真正答案,而是一个建议:如果您想保持组件之间的对齐,那么:
LayoutManager
来确保正确对齐(建议:DesignGridLayout,但也有其他好的布局管理器)JSeparator
(这是来自 JGoodies 名气的 Karsten Lentszch 的建议)That's not a real answer to your question but rather a suggestion: if you want to keep alignments across components then:
LayoutManager
that ensures correct alignment (suggestion:DesignGridLayout
, but there are other good LayoutManagers as well)JLabel
and a horizontalJSeparator
(this one is a suggestion of Karsten Lentszch, from the JGoodies fame)