如何在运行时更改秋千应用程序的外观? [英] How to change swing applicaiton's look and feel at runtime?

查看:68
本文介绍了如何在运行时更改秋千应用程序的外观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个SwingUtilities.updateComponentTreeUI(Component c)方法,但是它不能完美地工作.例如,我有一个JFileChooser,当前外观是Windows,然后使用SwingUtilities.updateComponentTreeUI(mainWindow)将外观更改为Nimbus,并且主窗口的样式正确更改,但是当我使用JFileChooser.showOpenDialog(Component parent)方法,它仍然在Windows外观中.如果我显示使用JPopupMenu.show(Component invoker, int x, int y)方法的弹出对话框,也会发生同样的情况.这个问题有解决方案吗?

I know that there's a SwingUtilities.updateComponentTreeUI(Component c) method but it doesn't work perfectly. For example, I have a JFileChooser and the current look and feel is Windows, then I change the look and feel to Nimbus with SwingUtilities.updateComponentTreeUI(mainWindow), and the main window's style is changed correctly, but when I show the file chooser with the JFileChooser.showOpenDialog(Component parent) method, it's still in Windows look and feel. The same happens if I show a popup dialog with the JPopupMenu.show(Component invoker, int x, int y) method. Any solution to this issue?

推荐答案

调用SwingUtilities.updateComponentTreeUI(mainWindow)只会更新mainWindow下的Swing层次结构中的Swing组件.

Calling SwingUtilities.updateComponentTreeUI(mainWindow) will only update the Swing components in the Swing hierarchy under mainWindow.

如果将JFileChooser存储在代码中的某个位置(例如,在类的字段中)而不显示JFileChooser,则选择器将不会通过SwingUtilities.updateComponentTreeUI(mainWindow)调用进行更新.您可以通过在UIManager上自己添加一个侦听器并在外观改变时从该侦听器调用SwingUtilities.updateComponentTreeUI(myStoredFileChooser)来解决此问题.

If you store the JFileChooser somewhere in your code (e.g. in a field of a class) without showing the JFileChooser the chooser will not be updated by the SwingUtilities.updateComponentTreeUI(mainWindow) call. You can work around this by adding a listener to the UIManager yourself and call SwingUtilities.updateComponentTreeUI(myStoredFileChooser) from that listener when the look-and-feel is changed.

确保您不会因此而造成内存泄漏,例如让该侦听器仅对JFileChooser具有WeakReference(因为UIManager的生存期等于JVM的生存期)

Make sure you do not create a memory leak with this, e.g. let that listener only have a WeakReference to the JFileChooser (as the lifetime of the UIManager equals the lifetime of the JVM)

这篇关于如何在运行时更改秋千应用程序的外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆