FileDialog使用方法
(2011-01-02 10:09:08)
FileDialog使用方法:
FileDialog fd=new FileDialog(new Frame(),"测试",FileDialog.LOAD);
FilenameFilter ff=new FilenameFilter(){
public boolean accept(File dir, String name) {
if (name.endsWith("jpg")){
return true;
}
return false;
}
};
fd.setFilenameFilter(ff);
fd.setVisible( true);
System.out.println(fd.getDirectory()+fd.getFile());
但在Windows中FileDialog + FilenameFilter无法正常工作, jdoc的原注释为:Filename filters do not function in Sun's reference implementation for Microsoft Windows.
2.FileDialog + FilenameFilter可以用JFileChooser + javax.swing.filechooser.FileFilter 来代替,jdoc中的例子如下:
JFileChooser
chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in
FileChooserDemo,
// under the demo/jfc directory in the Java 2 SDK,
Standard Edition.
ExampleFileFilter filter = new
ExampleFileFilter();
filter.addExtension( "jpg");
filter.addExtension( "gif");
filter.setDescription( "JPG
& GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println( "You chose to
open this file: "
+
chooser.getSelectedFile().getName());
}
转应用实例:
JFileChooser
filechooser =
new JFileChooser();//创建文件选择器
filechooser.setCurrentDirectory( new File("."));//设置当前目录
filechooser.setAcceptAllFileFilterUsed( false);
//显示所有文件
filechooser.addChoosableFileFilter( new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return true;
}
public String getDescription() {
return "所有文件(*.*)";
}
});
//显示JAVA源文件
filechooser.setFileFilter( new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) { //设定可用的文件的后缀名
if(f.getName().endsWith(".java")||f.isDirectory()){
return true;
}
return false;
}
public String getDescription() {
return "JAVA源程序(*.java)";
}
});
FileDialog fd=new FileDialog(new Frame(),"测试",FileDialog.LOAD);
FilenameFilter ff=new FilenameFilter(){
fd.setVisible(
System.out.println(fd.getDirectory()+fd.getFile());
但在Windows中FileDialog + FilenameFilter无法正常工作, jdoc的原注释为:Filename filters do not function in Sun's reference implementation for Microsoft Windows.
2.FileDialog + FilenameFilter可以用JFileChooser + javax.swing.filechooser.FileFilter 来代替,jdoc中的例子如下:
转应用实例:
后一篇:Linux下的聊天