swing 使用系统L&F
示例
Swing支持许多本地L&F。
您始终可以轻松安装一个,而无需调用特定的L&F类:
public class SystemLookAndFeel
{
public static void main ( final String[] args )
{
// L&F installation should be performed within EDT (Event Dispatch Thread)
//这对于避免任何UI问题,异常甚至死锁都很重要
SwingUtilities.invokeLater( new Runnable ()
{
@Override
public void run ()
{
// Process of L&F installation might throw multiple exceptions
//处理或忽略它们始终取决于您
//在大多数情况下,您永远不会遇到任何这些
try
{
// Installing native L&F as a current application L&F
// We do not know what exactly L&F class is, it is provided by the UIManager
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName() );
}
catch ( final ClassNotFoundException e )
{
// L&F class was not found
e.printStackTrace();
}
catch ( final InstantiationException e )
{
// Exception while instantiating L&F class
e.printStackTrace();
}
catch ( final IllegalAccessException e )
{
//无法访问类或初始化程序
e.printStackTrace();
}
catch ( final UnsupportedLookAndFeelException e )
{
// L&F is not supported on the current system
e.printStackTrace();
}
//现在我们可以创建一些本机外观的UI
//这只是一个带有单个按钮的小样本框架
final JFrame frame = new JFrame ();
final JPanel content = new JPanel ( new FlowLayout () );
content.setBorder(BorderFactory.createEmptyBorder( 50, 50, 50, 50 ) );
content.add( new JButton ( "Native-looking button" ) );
frame.setContentPane( content );
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
} );
}
}这些是JDK支持的本机L&F(OS->L&F):
*这些L&F由系统供应商提供,实际L&F类名称可能会有所不同