0

Ajavax.swing.border.TitledBorder在边框边缘引入了 2 个像素的间距。这很烦人,因为它破坏了与周围组件的对齐。

你如何删除这个间距?

我正在寻找一种适用于任何外观和感觉的解决方案。

4

2 回答 2

6

不幸的是,这个边缘宽度在 TitledBorder 类中是硬编码的。所以你不能删除这个间距。

public class TitledBorder extends AbstractBorder
{
    //...

    // Space between the border and the component's edge
    static protected final int EDGE_SPACING = 2;
}

但是您可以尝试扩展此类(覆盖方法“void paintBorder(Component, Graphics, int, int, int, int)”或者可能是“Insets getBorderInsets(Component, Insets)”)或从头开始实现自己的边框。

于 2011-10-26T18:43:03.830 回答
2

这不是您问题的真正答案,而是一个建议:如果您想保持组件之间的对齐,那么:

  • 不要使用边框
  • 整个对话框或框架只使用一个面板
  • 使用LayoutManager确保正确对齐的 a (建议:DesignGridLayout,但也有其他好的 LayoutManagers)
  • 如果您需要在视觉上对组件进行分组(与标题边框相同的方式),则引入带有 aJLabel和水平的行JSeparator(这是 Karsten Lentszch 的建议,来自 JGoodies 成名)
于 2011-10-27T08:11:12.420 回答