JavaFX FileChooser文件選擇器、DirectoryChooser目錄選擇器


參考:https://www.yiibai.com/javafx/javafx_filechooser.html

參考:https://blog.csdn.net/dorma_bin/article/details/78856952

創建一個窗口,在窗口中放置兩個按鍵:“Choose File”與“Choose Folder”。

當“Choose File”按鍵發生鼠標點擊事件,打開文件選擇器。如果用戶選擇了某一個文件,並點擊“打開”,在控制台輸出該文件的絕對路徑。

當“Choose Folder”按鍵發生鼠標點擊事件,打開目錄選擇器。如果用戶選擇了某一個文件,並點擊“選擇文件夾”,在控制台輸出該文件的絕對路徑。

 1 import java.io.File;
 2 
 3 import javafx.application.Application;
 4 import javafx.event.ActionEvent;
 5 import javafx.event.EventHandler;
 6 import javafx.geometry.Insets;
 7 import javafx.geometry.Pos;
 8 import javafx.scene.Scene;
 9 import javafx.scene.control.Button;
10 import javafx.scene.layout.GridPane;
11 import javafx.stage.DirectoryChooser;
12 import javafx.stage.FileChooser;
13 import javafx.stage.FileChooser.ExtensionFilter;
14 import javafx.stage.Stage;
15 
16 public class Main extends Application {
17 
18     public static void main(String[] args) {
19         launch(args);
20     }
21     
22     @Override
23     public void start(Stage primaryStage) throws Exception {
24         // Create a pane to hold a button
25         GridPane pane = new GridPane();
26         pane.setStyle("-fx-border-color: green;");
27         pane.setAlignment(Pos.CENTER);
28         pane.setPadding(new Insets(10, 10, 10, 10));
29         pane.setHgap(10);
30         pane.setVgap(10);
31         
32         // Create a button to choose a file
33         Button btChooseFile = new Button("Choose File");
34         pane.add(btChooseFile, 0, 0);
35         
36         // Create a button to choose a directory
37         Button btChooseDirectory = new Button("Choose Folder");
38         pane.add(btChooseDirectory, 1, 0);
39                 
40         // Set the primary stage properties
41         primaryStage.setScene(new Scene(pane, 400, 200));
42         primaryStage.setTitle("Starting...");
43         primaryStage.setResizable(false);
44         primaryStage.show();
45         
46         // 
47         btChooseFile.setOnAction(new EventHandler<ActionEvent>() {
48             @Override
49             public void handle(ActionEvent event) {
50                 FileChooser fileChooser = new FileChooser();
51                 fileChooser.setTitle("Choose File");
52 //                fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
53 //                fileChooser.getExtensionFilters().add(new ExtensionFilter("All Files", "*.*"));
54                 fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Text Files", "*.txt"), new ExtensionFilter("All Files", "*.*"));
55                 File file = fileChooser.showOpenDialog(primaryStage);
56                 if (file != null) {
57                     System.out.println(file.getAbsolutePath());
58                 }
59             }
60         });
61         
62         btChooseDirectory.setOnAction(new EventHandler<ActionEvent>() {
63             @Override
64             public void handle(ActionEvent event) {
65                 DirectoryChooser directoryChooser = new DirectoryChooser();
66                 directoryChooser.setTitle("Choose Folder");
67                 File directory = directoryChooser.showDialog(new Stage());
68                 if (directory != null) {
69                     System.out.println(directory.getAbsolutePath());
70                 }
71             }
72         });
73     }
74 }

 

 

運行程序的UI:

 

點擊按鍵“Choose File”,控制台的輸出(有異常?),以及UI:

Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.

 

 

選擇某一個文件,並點擊按鍵“打開”,控制台輸出:

J:\PrtSc\20190321\33.png

 

 

點擊按鍵“Choose Folder”,控制台的輸出(有異常?),以及UI:

Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.
Qt: Untested Windows version 10.0 detected!
log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
log4cplus:ERROR Please initialize the log4cplus system properly.

 

 

選擇某一個文件夾,並點擊按鍵“選擇文件夾”,控制台輸出:

J:\PrtSc\20190321

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM