swing 使用自定义L&F
示例
public class CustomLookAndFeel { 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 custom L&F as a current application L&F UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel" ); } 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(); } //现在我们可以创建一些漂亮的用户界面 //这只是一个带有单个按钮的小样本框架 final JFrame frame = new JFrame (); final JPanel content = new JPanel ( new FlowLayout () ); content.setBorder(BorderFactory.createEmptyBorder( 50, 50, 50, 50 ) ); content.add( new JButton ( "Metal button" ) ); frame.setContentPane( content ); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); } } ); } }
您可以在此处的主题中找到可用的SwingL&F的巨大列表:Java外观(L&F)
请记住,此时某些L&F可能已经过时了。