版权声明:就是码字也不容易啊 https://blog.csdn.net/qq_40946921/article/details/85047685
在不久之前,我遇到一个问题,同学问我,记事本的打开与保存应该怎么写,当时我这样想,应该可以根据File类获得路径的子文件,用搜索算法构造一个目录树,然后再做个图形界面就行,没什么问题的话这样做应该可以达到效果,但是今天我突然发现JavaFX里面原本就有这个东西,不需要我们再重复造轮子了。
文件选择器——FileChooser
包:import javafx.stage.FileChooser;
1、打开文件——File showOpenDialog(Window)
在window窗口打开文件选择器,返回被选择文件的一个File对象,得到File我们也就可以对文件进行处理。
代码示例
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import java.io.File;
public class Test extends Application {
public void start(Stage stage){
Button btOpen=new Button("打开文件");
Scene scene=new Scene(btOpen);
FileChooser chooser=new FileChooser();
chooser.setInitialDirectory(new File("C:\\Users\\Administrator\\Pictures")); //设置初始路径,默认为我的电脑
chooser.setTitle("打开图片"); //设置窗口标题,默认为“打开”
chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG", "*.jpg"),
new FileChooser.ExtensionFilter("PNG", "*.png"));
//筛选文件扩展
btOpen.setOnMouseClicked(e->{
try{
System.out.println(chooser.showOpenDialog(stage).getAbsolutePath()); //chooser.showOpenDialog(stage)得到File对象
}catch (NullPointerException ex){}
});
stage.setScene(scene);
stage.show();
}
}
多种类型:
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("图片", "*.jpg","*.png","*.gif")
多个文件:
List list=chooser.showOpenMultipleDialog(stage);
使用
2、保存文件 ——File showSaveDialog(Window)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import java.io.File;
public class Test extends Application {
public void start(Stage stage){
Button btOpen=new Button("保存图片");
Scene scene=new Scene(btOpen);
FileChooser chooser=new FileChooser();
chooser.setInitialDirectory(new File("C:\\Users\\Administrator\\Pictures")); //设置初始路径,默认为我的电脑
chooser.setTitle("保存图片"); //设置窗口标题,默认为“打开”
chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG", "*.jpg"),
new FileChooser.ExtensionFilter("PNG", "*.png"));
//筛选文件扩展
btOpen.setOnMouseClicked(e->{
try{
System.out.println(chooser.showSaveDialog(stage).getAbsolutePath()); //chooser.showOpenDialog(stage)得到File对象
}catch (NullPointerException ex){}
});
stage.setScene(scene);
stage.show();
}
}
文件夹选择器——DirectoryChooser
包:import javafx.stage.DirectoryChooser;
用法同FileChooser差不多
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import java.io.File;
public class Test extends Application {
public void start(Stage stage){
Button btOpen=new Button("打开文件夹");
Scene scene=new Scene(btOpen);
DirectoryChooser chooser=new DirectoryChooser();
chooser.setInitialDirectory(new File("C:\\Users\\Administrator\\Pictures")); //设置初始路径,默认为我的电脑
chooser.setTitle("打开文件夹"); //设置窗口标题
btOpen.setOnMouseClicked(e->{
try{
System.out.println(chooser.showDialog(stage)); //chooser.showDialog(stage)得到File对象
}catch (NullPointerException ex){}
});
stage.setScene(scene);
stage.show();
}
}