1

如何删除 TitledBorder 中标题周围的空格?我的意思是这个红色边框
红色边框

一段代码:

Font f = new Font(Font.DIALOG, Font.PLAIN, 14);
Map m = f.getAttributes();
m.put(TextAttribute.BACKGROUND, Color.yellow);
Font ff = new Font(m);
Border mb = BorderFactory.createMatteBorder(20, 0, 0, 0, Color.yellow);
Border test = BorderFactory.createEmptyBorder(-2,-2,-2,-2);
Border mb6 = BorderFactory.createTitledBorder(mb, "Title", TitledBorder.CENTER, TitledBorder.TOP, ff, Color.DARK_GRAY);
mb6 = BorderFactory.createCompoundBorder(test, mb6);
Border mb2 = BorderFactory.createMatteBorder(1, 0, 0, 0, Color.gray);
mb2 = BorderFactory.createCompoundBorder(test,mb2);
Border mb3 = BorderFactory.createCompoundBorder(mb6, mb2);
Border mb4 = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray);
Border mb5 = BorderFactory.createCompoundBorder(mb4, mb3);
modeSetPanel.setBackground(Color.red);
modeSetPanel.setBorder(mb5);
4

2 回答 2

4

如果您不关心边框,您可以在设置选项卡面板中添加一个 JPanel。在此 JPanel 中,您可以添加另一个带有 BorderLayout 的 JPanel。在带有 BorderLayout 的那个里面添加两个 JPanel:北一个是黄色背景,中间一个是红色背景。只需在黄色的 JPanel 中添加一个标有“标题”的 JLabel,在红色的 JPanel 中添加您的控件。

标签周围的红色边框消失了。

这是一个没有任何边界且在 Nimbus 外观和感觉中的示例:

样本...

于 2012-05-04T12:34:40.610 回答
2

事实上这不是一个答案,但评论中的图像是不可能的......

我试过这段代码:

import java.awt.Color;
import java.awt.Font;
import java.awt.font.TextAttribute;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class RedBorderProblem extends JFrame {

    public RedBorderProblem() {
        final JPanel modeSetPanel = new JPanel();

        final Font f = new Font( Font.DIALOG, Font.PLAIN, 14 );
        final Map m = f.getAttributes();
        m.put( TextAttribute.BACKGROUND, Color.yellow );
        final Font ff = new Font( m );
        final Border mb = BorderFactory.createMatteBorder( 20, 0, 0, 0, Color.yellow );
        final Border test = BorderFactory.createEmptyBorder( -2, -2, -2, -2 );
        Border mb6 = BorderFactory.createTitledBorder( mb, "Title", TitledBorder.CENTER, TitledBorder.TOP, ff,
                Color.DARK_GRAY );
        mb6 = BorderFactory.createCompoundBorder( test, mb6 );
        Border mb2 = BorderFactory.createMatteBorder( 1, 0, 0, 0, Color.gray );
        mb2 = BorderFactory.createCompoundBorder( test, mb2 );
        final Border mb3 = BorderFactory.createCompoundBorder( mb6, mb2 );
        final Border mb4 = BorderFactory.createMatteBorder( 1, 1, 1, 1, Color.gray );
        final Border mb5 = BorderFactory.createCompoundBorder( mb4, mb3 );
        modeSetPanel.setBackground( Color.red );
        modeSetPanel.setBorder( mb5 );

        this.add( modeSetPanel );

        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        this.pack();
        this.setVisible( true );
    }

    public static void main( final String[] args ) {
        new RedBorderProblem();
    }

}

结果是

在此处输入图像描述

于 2012-05-04T12:56:41.393 回答