为了更改所有TitledBorder字体,我正在使用UIManager:
UIManager.put("TitledBorder.font", new Font("Tahoma", Font.BOLD, 11));
但是要如何在TitledBorder.border
属性中设置,只更改边框的颜色(或甚至边框的宽度)呢?
谢谢!
就像使用UIManager
一次性更改所有TitledBorder
字体一样,要更改TitledBorder
边框,请使用此函数:
UIManager.put("TitledBorder.border", new LineBorder(new Color(200,200,200), 1));
LineBorder
对象来实现,该对象在构造函数中接受颜色和宽度,就像您提出的要求一样。好的,您可以在TitledBorder中指定任何属性。以下是Swing TitledBorder的完全定制示例:
public static void main ( String[] args )
{
LineBorder border = new LineBorder ( Color.RED, 3, true );
TitledBorder tborder = new TitledBorder ( border, "Titled border", TitledBorder.CENTER,
TitledBorder.DEFAULT_POSITION, new Font ( "Arial", Font.BOLD, 14 ), Color.BLUE );
JFrame frame = new JFrame ();
JLabel label = new JLabel ( "Some content label" );
label.setBorder ( BorderFactory
.createCompoundBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ),
BorderFactory.createCompoundBorder ( tborder,
BorderFactory.createEmptyBorder ( 15, 15, 15, 15 ) ) ) );
frame.add ( label );
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setVisible ( true );
}