在Mac OS X 10.8.2上为我运行的文件:
import java.io.File;import javax.swing.JFileChooser;public class JFileChooserTest{ public static void main(String[] args) { System.out.println("file.encoding=" + System.getProperty("file.encoding")); String path; if(args.length > 0) path = args[0]; else path = System.getProperty("user.dir", "."); File dir = new File(path); JFileChooser jfc = new JFileChooser(dir); int result = jfc.showOpenDialog(null); switch(result) { case JFileChooser.CANCEL_OPTION: System.out.println("User cancelled OPEN dialog."); break; case JFileChooser.APPROVE_OPTION: System.out.println("User chose file: " + jfc.getSelectedFile()); break; case JFileChooser.ERROR_OPTION: System.out.println("User encountered an error"); break; default: System.out.println("Confused"); break; } System.exit(0); }}
这是一个示例运行:
$ java -showversion JFileChooserTest java version "1.7.0_09"Java(TM) SE Runtime Environment (build 1.7.0_09-b05)Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)file.encoding=UTF-8User chose file: /.../测试文件.txt
这是另一个示例运行:
$ java -showversion -Dfile.encoding=ISO-8859-1 JFileChooserTest java version "1.7.0_09"Java(TM) SE Runtime Environment (build 1.7.0_09-b05)Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)file.encoding=ISO-8859-1User chose file: /.../????.txt
在这两种情况下,文件选择对话框均会正确显示文件名(测试文件.txt)。
请注意,using
java.awt.FileDialog将为您提供大多数Mac
OS用户用来查看的特定于平台的文件对话框。尽管它不是严格意义上的Swing(并且具有很小的功能集),但它可能比
JFileChooser诸如OPEN和SAVE对话框之类的要优越。(它也显示汉字,而我的系统没有问题)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)