UIManager 使用标题边框颜色和宽度

4

为了更改所有TitledBorder字体,我正在使用UIManager:

UIManager.put("TitledBorder.font", new Font("Tahoma", Font.BOLD, 11));

但是要如何在TitledBorder.border 属性中设置,只更改边框的颜色(或甚至边框的宽度)呢?

谢谢!

2个回答

8

就像使用UIManager一次性更改所有TitledBorder字体一样,要更改TitledBorder边框,请使用此函数:

UIManager.put("TitledBorder.border", new LineBorder(new Color(200,200,200), 1));

它将使用第二个参数传递的边框对象来改变(设置)边框属性。所有边框类型(甚至是工厂类)的描述都可以在此处找到:http://docs.oracle.com/javase/tutorial/uiswing/components/border.html
此示例通过传递一个LineBorder对象来实现,该对象在构造函数中接受颜色和宽度,就像您提出的要求一样。

5

好的,您可以在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 );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接