该UIManager.getSystemLookAndFeelClassName()方法返回Swing应用程序的当前系统外观。SwingUtilities.updateComponentTreeUI()设置LAF后,请不要忘记调用该方法以更新当前应用程序的外观。
package org.nhooo.example.swing; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.WindowConstants; public class SystemLAFDemo { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(250, 250); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setTitle("System LAF Demo"); try { // 将系统外观用于Swing应用程序 String className = UIManager.getSystemLookAndFeelClassName(); System.out.println("className = " + className); UIManager.setLookAndFeel(className); } catch (Exception e) { e.printStackTrace(); } SwingUtilities.updateComponentTreeUI(frame); frame.setVisible(true); } }