📜  JavaFX |文件选择器类

📅  最后修改于: 2022-05-13 01:55:34.626000             🧑  作者: Mango

JavaFX |文件选择器类

FileChooser 类是 JavaFX 的一部分。它用于调用用于选择单个文件的文件打开对话框(showOpenDialog)、用于选择多个文件的文件打开对话框 (showOpenMultipleDialog) 和文件保存对话框(showSaveDialog)。 FileChooser 类继承 Object 类。

该类的构造函数是:

  • FileChooser() :创建一个新的 FileChooser 对话框。

常用方法:

MethodExplanation
getInitialDirectory()Returns the initial directory of the file chooser.
getTitle()Returns the title of file chooser.
setInitialDirectory(File f)Sets the initial directory of the filechooser.
setTitle(String t)Sets the title of the file chooser.
showOpenDialog(Window w)Shows a new open file selection dialog.
setInitialFileName(String n)Sets the initial file name of file chooser.
showSaveDialog(Window w)Shows a new Save file selection dialog.
getInitialFileName()Returns the initial filename of the file chooser.

下面的程序说明了 FileChooser 类的使用:

1. 创建 fileChooser 并将其添加到舞台的Java程序:在此程序中,我们将创建一个名为file_chooser的文件选择器。然后创建一个名为label的 Label 和两个名为buttonbutton1的 Button。我们将创建两个 EventHandler 来处理按下按钮或按钮1时的事件。按下按钮时,会出现一个打开的文件选择器对话框,并且选定的文件在标签中显示为文本,当按下按钮 1 时,会出现一个保存文件选择器,并且选定的文件在标签中显示为文本。将标签和按钮添加到Vbox ,并将VBox添加到Scene并将场景添加到舞台,并调用show()函数显示最终结果。

Java
// Java Program to create fileChooser
// and add it to the stage
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.text.*;
import javafx.scene.Group;
import javafx.scene.shape.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import java.io.*;
import javafx.stage.FileChooser;
  
public class FileChooser_1 extends Application {
  
// launch the application
public void start(Stage stage)
{
 
    try {
 
        // set title for the stage
        stage.setTitle("FileChooser");
 
        // create a File chooser
        FileChooser fil_chooser = new FileChooser();
 
        // create a Label
        Label label = new Label("no files selected");
 
        // create a Button
        Button button = new Button("Show open dialog");
 
        // create an Event Handler
        EventHandler event =
        new EventHandler() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showOpenDialog(stage);
 
                if (file != null) {
                     
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button.setOnAction(event);
 
        // create a Button
        Button button1 = new Button("Show save dialog");
 
        // create an Event Handler
        EventHandler event1 =
         new EventHandler() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showSaveDialog(stage);
 
                if (file != null) {
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button1.setOnAction(event1);
 
        // create a VBox
        VBox vbox = new VBox(30, label, button, button1);
 
        // set Alignment
        vbox.setAlignment(Pos.CENTER);
 
        // create a scene
        Scene scene = new Scene(vbox, 800, 500);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    catch (Exception e) {
 
        System.out.println(e.getMessage());
    }
}
 
// Main Method
public static void main(String args[])
{
 
    // launch the application
    launch(args);
}
}


Java
// Java Program to create FileChooser
// & set title, initial File
// and add it to the stage
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.text.*;
import javafx.scene.Group;
import javafx.scene.shape.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import java.io.*;
import javafx.stage.FileChooser;
  
public class FileChooser_2 extends Application {
 
// launch the application
public void start(Stage stage)
{
 
    try {
 
        // set title for the stage
        stage.setTitle("FileChooser");
 
        // create a File chooser
        FileChooser fil_chooser = new FileChooser();
 
        // set title
        fil_chooser.setTitle("Select File");
 
        // set initial File
        fil_chooser.setInitialDirectory(new File("e:\\"));
 
        // create a Label
        Label label = new Label("no files selected");
 
        // create a Button
        Button button = new Button("Show open dialog");
 
        // create an Event Handler
        EventHandler event =
        new EventHandler() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showOpenDialog(stage);
 
                if (file != null) {
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button.setOnAction(event);
 
        // create a Button
        Button button1 = new Button("Show save dialog");
 
        // create an Event Handler
        EventHandler event1 =
         new EventHandler() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showSaveDialog(stage);
 
                if (file != null) {
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button1.setOnAction(event1);
 
        // create a VBox
        VBox vbox = new VBox(30, label, button, button1);
 
        // set Alignment
        vbox.setAlignment(Pos.CENTER);
 
        // create a scene
        Scene scene = new Scene(vbox, 800, 500);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    catch (Exception e) {
 
        System.out.println(e.getMessage());
    }
}
 
// Main Method
public static void main(String args[])
{
 
    // launch the application
    launch(args);
}
}


输出:

2. 创建 FileChooser、设置标题、初始文件并将其添加到舞台的Java程序:在此程序中,我们将创建一个名为fil_chooser的文件选择器。然后创建一个名为 label 的 Label 和两个名为buttonbutton1的 Button。使用setTitle()setInitialDirectory()函数设置文件选择器的标题和初始目录。现在创建两个 EventHandler 来处理按下按钮或按钮1时的事件。当按下按钮时,会出现一个打开的文件选择器对话框,并且选定的文件在标签中显示为文本,当按下按钮 1 时,会出现一个保存文件选择器,并且选定的文件在标签中显示为文本。将标签和按钮添加到Vbox ,将VBox添加到Scene,将场景添加到stage,调用show()函数显示最终结果。

Java

// Java Program to create FileChooser
// & set title, initial File
// and add it to the stage
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.text.*;
import javafx.scene.Group;
import javafx.scene.shape.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import java.io.*;
import javafx.stage.FileChooser;
  
public class FileChooser_2 extends Application {
 
// launch the application
public void start(Stage stage)
{
 
    try {
 
        // set title for the stage
        stage.setTitle("FileChooser");
 
        // create a File chooser
        FileChooser fil_chooser = new FileChooser();
 
        // set title
        fil_chooser.setTitle("Select File");
 
        // set initial File
        fil_chooser.setInitialDirectory(new File("e:\\"));
 
        // create a Label
        Label label = new Label("no files selected");
 
        // create a Button
        Button button = new Button("Show open dialog");
 
        // create an Event Handler
        EventHandler event =
        new EventHandler() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showOpenDialog(stage);
 
                if (file != null) {
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button.setOnAction(event);
 
        // create a Button
        Button button1 = new Button("Show save dialog");
 
        // create an Event Handler
        EventHandler event1 =
         new EventHandler() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showSaveDialog(stage);
 
                if (file != null) {
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button1.setOnAction(event1);
 
        // create a VBox
        VBox vbox = new VBox(30, label, button, button1);
 
        // set Alignment
        vbox.setAlignment(Pos.CENTER);
 
        // create a scene
        Scene scene = new Scene(vbox, 800, 500);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    catch (Exception e) {
 
        System.out.println(e.getMessage());
    }
}
 
// Main Method
public static void main(String args[])
{
 
    // launch the application
    launch(args);
}
}

输出:

注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html