JavaFX FileChooser文件选择器

创建一个窗口,在窗口中间放置一个按键,当按键发生鼠标点击事件,打开文件选择器。如果用户选择了某一个文件,并点击“打开”,在控制台输出该文件的绝对路径。

 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.FileChooser;
12 import javafx.stage.FileChooser.ExtensionFilter;
13 import javafx.stage.Stage;
14 
15 public class Main extends Application {
16 
17     public static void main(String[] args) {
18         launch(args);
19     }
20     
21     @Override
22     public void start(Stage primaryStage) throws Exception {
23         // Create a pane to hold a button
24         GridPane pane = new GridPane();
25         pane.setStyle("-fx-border-color: green;");
26         pane.setAlignment(Pos.CENTER);
27         pane.setPadding(new Insets(10, 10, 10, 10));
28         pane.setHgap(10);
29         pane.setVgap(10);
30         
31         // Create a button
32         Button btOpen = new Button("Open");
33         pane.add(btOpen, 0, 0);
34                 
35         // Set the primary stage properties
36         primaryStage.setScene(new Scene(pane, 400, 200));
37         primaryStage.setTitle("Starting...");
38         primaryStage.setResizable(false);
39         primaryStage.show();
40         
41         btOpen.setOnAction(new EventHandler<ActionEvent>() {
42             @Override
43             public void handle(ActionEvent event) {
44                 FileChooser fileChooser = new FileChooser();
45                 fileChooser.setTitle("Open");
46                 // 第47~48行代码的作用等效于第49行代码的作用,用于设置扩展名过滤器。
47 //                fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
48 //                fileChooser.getExtensionFilters().add(new ExtensionFilter("All Files", "*.*"));
49                 fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Text Files", "*.txt"), new ExtensionFilter("All Files", "*.*"));
50                 File file = fileChooser.showOpenDialog(primaryStage);
51                 if (file != null) {
52                     System.out.println(file.getAbsolutePath());
53                 }
54             }
55         });
56     }
57 }

控制台的输出:(有异常)

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\Nobody.png

UI:

猜你喜欢

转载自www.cnblogs.com/Satu/p/10820144.html
今日推荐