删除TitledBorder周围的间距

Emm*_*urg 0 java swing titled-border

A javax.swing.border.TitledBorder在边框边缘周围引入2个像素的间距.这非常令人讨厌,因为它打破了与周围组件的对齐.

你怎么删除这个间距?

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

sva*_*aor 6

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

public class TitledBorder extends AbstractBorder
{
    //...

    // Space between the border and the component's edge
    static protected final int EDGE_SPACING = 2;
}
Run Code Online (Sandbox Code Playgroud)

但是你可以尝试扩展这个类(覆盖方法"void paintBorder(Component,Graphics,int,int,int,int)"或者可以是"Insets getBorderInsets(Component,Insets)")或者从头开始实现你自己的边框.