我收到此运行时错误,我正在尝试使 java 文件选择器看起来像 Windows 的文件选择器。
错误代码:
Exception in thread "main" java.lang.NullPointerException
at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installComponents(WindowsFileChooserUI.java:306)
at javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:173)
at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installUI(WindowsFileChooserUI.java:150)
at Main.getImg(Main.java:49)
at Main.main(Main.java:19)
Code:
JFileChooser fico = new JFileChooser();
WindowsFileChooserUI wui = new WindowsFileChooserUI(fico);
wui.installUI(fico);
int returnVal = fico.showOpenDialog(null);
当 UI 对象初始化时,它会尝试从 UI 管理器中读取一些它期望存在的 UI 默认值(FileChooser.viewMenuIcon
属性),它始终存在于 Windows L&F 下,但不存在于 Metal L&F 下。
首先,警告。在 Swing 中同时混合多个 L&F 是危险的。 Swing 实际上一次只能运行一个 L&F。
设置“特殊”文件选择器的更好方法是在应用程序启动时通过 UI 管理器初始化所有内容。
//Do this first thing in your application before any other UI code
//Switch to Windows L&F
LookAndFeel originalLaf = UIManager.getLookAndFeel();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//Create the special file chooser
JFileChooser windowsChooser = new JFileChooser();
//Flick the L&F back to the default
UIManager.setLookAndFeel(originalLaf);
//And continue on initializing the rest of your application, e.g.
JFileChooser anotherChooserWithOriginalLaf = new JFileChooser();
现在您有两个可以使用的具有两个不同 L&F 的组件。
//First chooser opens with windows L&F
windowsChooser.showOpenDialog(null);
//Second chooser uses default L&F
anotherChooserWithOriginalLaf.showOpenDialog(null);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)