2012-03-07 23 views
1

我得到这个运行时错误,我想使java文件选择器看起来像Windows之一。NullPointerException当使用WindowsFileChooserUI

错误代码:

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) 

代码:

JFileChooser fico = new JFileChooser(); 
WindowsFileChooserUI wui = new WindowsFileChooserUI(fico); 
wui.installUI(fico); 
int returnVal = fico.showOpenDialog(null); 
+0

我刚刚在你的问题中执行了4行代码,并没有例外。你能指定你的问题还是发送更多的代码? – Juvanis 2012-03-07 03:56:04

回答

3

当UI对象初始化时,它试图读取,它预期存在的UI经理(一些用户界面默认的FileChooser.viewMenuIcon属性),它总是Windows下的大号& F,但没有下金属大号& F.

Firstl存在y,警告。在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(); 

现在你有两个不同的大号& Fs的,您可以使用两个组件。

//First chooser opens with windows L&F 
windowsChooser.showOpenDialog(null); 

//Second chooser uses default L&F 
anotherChooserWithOriginalLaf.showOpenDialog(null); 
+1

OMG我爱你很多! +1 – PulsePanda 2012-08-08 20:56:17

+0

很好的回答,先生! – 2013-08-15 03:41:30

相关问题