个性化阅读
专注于IT技术分析

JavaFX FileChooser

JavaFX文件选择器使用户可以从文件系统浏览文件。 javafx.stage.FileChooser类表示FileChooser。可以通过实例化FileChooser类来创建它。它包含两种主要方法。

正如我们在现代应用程序中看到的那样, 向用户显示了两种类型的对话框, 一种用于打开文件, 另一种用于保存文件。在每种情况下, 用户都需要浏览文件的位置并为文件指定名称。

FileChooser类提供两种类型的方法,

  1. showOpenDialog()
  2. showSaveDialog()

以下代码实现了showSaveDialog()方法。

范例1:

package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class FileChooserExample extends Application{

	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		
		FileChooser file = new FileChooser();
		file.setTitle("Open File");
		file.showOpenDialog(primaryStage);
		
		HBox root = new HBox();
		
		root.setSpacing(20);
	
		Scene scene = new Scene(root, 350, 100);
		primaryStage.setScene(scene);
		primaryStage.setTitle("FileChooser Example");
		primaryStage.show();
		
	}
	public static void main(String[] args) {
		launch(args);
	}

}

上面的代码向用户显示以下对话框, 提示用户浏览需要打开的文件的位置。

JavaFX FileChooser

范例2:

以下代码向用户显示了Label, TextField和Button。单击浏览按钮将打开一个打开的文件对话框。

package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class FileChooserExample extends Application{

	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		Label label = new Label("File:");
		TextField tf= new TextField();
		Button btn = new Button("Browse");
		btn.setOnAction(e->
		{
			FileChooser file = new FileChooser();
			file.setTitle("Open File");
			file.showOpenDialog(primaryStage);
		});
		HBox root = new HBox();
	//root.getChildren().add(file);
		
		root.setSpacing(20);
		root.getChildren().addAll(label, tf, btn);
		Scene scene = new Scene(root, 350, 100);
		primaryStage.setScene(scene);
		primaryStage.setTitle("FileChooser Example");
		primaryStage.show();
		
	}
	public static void main(String[] args) {
		launch(args);
	}

}

输出:

JavaFX FileChooser 1

保存文件

以下代码显示了用于保存文件的对话框。

package application;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class MainClass extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage)
{
	Button btn = new Button("SAVE");
	btn.setOnAction(e->
	{
		FileChooser file = new FileChooser();
		file.setTitle("Save Image");
		//System.out.println(pic.getId());
		File file1 = file.showSaveDialog(primaryStage);
		System.out.println(file1);
	});
	
	StackPane root = new StackPane();
	Scene scene = new Scene(root, 200, 300);
	primaryStage.setScene(scene);
	root.getChildren().add(btn);
	primaryStage.show();
}

}

输出:

JavaFX FileChooser 2
JavaFX FileChooser 3
JavaFX FileChooser 4
赞(0)
未经允许不得转载:srcmini » JavaFX FileChooser

相关推荐

评论 抢沙发

评论前必须登录!