我使用 swing 创建了一个文件选择器,以仅从 Windows 平台中选择图像。我曾经FileFilter
这样做过。accept()
从类中重写的方法的代码FileFilter
是:
public boolean accept(File f) {
System.out.println("File is : " + f.getName() + " " + f);
if(f.isDirectory())
return true;
String ext = getExtension(f); // returns extensions of the files.
if(ext.equals("jpg") || ext.equals("jpeg") || ext.equals("gif") || ext.equals("tif") || ext.equals("tiff"))
return true;
else {
return false;
}
MyComputer
当从 Windows 桌面选择时,它会挂起。以下是我为 MyComputer 获得的条目:
File is ::{20D04FE0-3AEA-1069-A2D8-08002B30309D} Computer
以下是我收到的错误消息:
File is ::{20D04FE0-3AEA-1069-A2D8-08002B30309D} Computer
File is lenovo C:\Users\lenovo
File is ::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C} Network
File is ::{031E4825-7B94-4DC3-B131-E946B44C8DD5} Libraries
File is 4s.txt C:\Users\lenovo\Desktop\4s.txt
File is cs.bat - Shortcut.lnk C:\Users\lenovo\Desktop\cs.bat - Shortcut.lnk
File is kaushik.txt C:\Users\lenovo\Desktop\kaushik.txt
File is rel.txt C:\Users\lenovo\Desktop\rel.txt
File is SQL Developer.lnk C:\Users\lenovo\Desktop\SQL Developer.lnk
File is Get Started With Oracle Database 11g Express Edition .lnk C:\Users\Public\Desktop\Get Started With Oracle Database 11g Express Edition .lnk
双击myComputer后显示如下:
File is ::{416651E4-9C3C-11D9-8BDE-F66BAD1E3F3A} ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{416651E4-9C3C-11D9-8BDE-F66BAD1E3F3A}
Exception in thread "Basic L&F File Loading Thread" java.lang.NullPointerException
at gui.ImageFilter.accept(ImageFilter.java:26)
at javax.swing.JFileChooser.accept(JFileChooser.java:1576)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread$1.call(BasicDirectoryModel.java:230)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread$1.call(BasicDirectoryModel.java:216)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at sun.awt.shell.Win32ShellFolderManager2$ComInvoker$3.run(Win32ShellFolderManager2.java:458)
at java.lang.Thread.run(Thread.java:619)
但是,当我在初始化后立即添加另一个if
条件时:ext
if (ext == null)
return false;
它运行良好。我的问题是,如果MyComputer
是一个目录,为什么它会受到null
测试的困扰?它不应该true
在目录测试中返回吗?另一方面,如果它不是一个目录(这使它成为一个文件,对吗?),那么它如何仍然返回true
并被文件选择器打开?还是他们有其他特殊的角度?
提前谢谢!