如何删除TitledBorder中标题周围的空格?我的意思是这个红色边框 red border http://s019.radikal.ru/i634/1205/8a/1b40d810a4fe.jpg
一段代码:
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);
答案 0 :(得分:3)
如果您不关心边框,可以在“设置”标签面板中添加JPanel。 在这个JPanel中,您使用BorderLayout添加另一个。在BorderLayout的内部添加两个JPanels:北部的一个是黄色背景,中间的一个是红色背景。只需在黄色JPanel上添加一个说“标题”的JLabel,在红色JPanel上添加一个控件。
标签周围的红色边框消失了。
这是一个没有任何边框的例子,在Nimbus的外观&感觉:
答案 1 :(得分: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();
}
}
,结果是