我知道有一个 SwingUtilities.updateComponentTreeUI(Component c)
方法,但它不能完美工作。例如,我有一个 JFileChooser,当前的外观是 Windows,然后我使用 SwingUtilities.updateComponentTreeUI(mainWindow) 将外观更改为 Nimbus,并且 main窗口的样式已正确更改,但是当我使用 JFileChooser.showOpenDialog(Componentparent)
方法显示文件选择器时,它仍然是 Windows 外观。如果我使用 JPopupMenu.show(Component invoker, int x, int y)
方法显示弹出对话框,也会发生同样的情况。
这个问题有解决办法吗?
最佳答案
假设 value
是新外观的类名,以下是更新所有窗口和子组件的代码片段:
public static void updateLAF(String value) {
if (UIManager.getLookAndFeel().getClass().getName().equals(value)) {
return;
}
try {
UIManager.setLookAndFeel(value);
for (Frame frame : Frame.getFrames()) {
updateLAFRecursively(frame);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void updateLAFRecursively(Window window) {
for (Window childWindow : window.getOwnedWindows()) {
updateLAFRecursively(childWindow);
}
SwingUtilities.updateComponentTreeUI(window);
}
关于java - 如何在运行时更改 Swing 应用程序的外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16165394/