当前位置:   article > 正文

JavaFX FileChooser文件选择器、DirectoryChooser目录选择器

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

运行程序的UI:

点击按键“Choose File”,控制台的输出(有异常?),以及UI:

  1. Qt: Untested Windows version 10.0 detected!
  2. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  3. log4cplus:ERROR Please initialize the log4cplus system properly.
  4. Qt: Untested Windows version 10.0 detected!
  5. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  6. log4cplus:ERROR Please initialize the log4cplus system properly.
  7. Qt: Untested Windows version 10.0 detected!
  8. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  9. log4cplus:ERROR Please initialize the log4cplus system properly.
  10. Qt: Untested Windows version 10.0 detected!
  11. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  12. log4cplus:ERROR Please initialize the log4cplus system properly.
  13. Qt: Untested Windows version 10.0 detected!
  14. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  15. log4cplus:ERROR Please initialize the log4cplus system properly.
  16. Qt: Untested Windows version 10.0 detected!
  17. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  18. log4cplus:ERROR Please initialize the log4cplus system properly.
  19. Qt: Untested Windows version 10.0 detected!
  20. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  21. log4cplus:ERROR Please initialize the log4cplus system properly.
  22. Qt: Untested Windows version 10.0 detected!
  23. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  24. log4cplus:ERROR Please initialize the log4cplus system properly.
  25. Qt: Untested Windows version 10.0 detected!
  26. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  27. log4cplus:ERROR Please initialize the log4cplus system properly.
  28. Qt: Untested Windows version 10.0 detected!
  29. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  30. log4cplus:ERROR Please initialize the log4cplus system properly.
  31. Qt: Untested Windows version 10.0 detected!
  32. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  33. log4cplus:ERROR Please initialize the log4cplus system properly.
  34. Qt: Untested Windows version 10.0 detected!
  35. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  36. log4cplus:ERROR Please initialize the log4cplus system properly.
  37. Qt: Untested Windows version 10.0 detected!
  38. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  39. log4cplus:ERROR Please initialize the log4cplus system properly.
  40. Qt: Untested Windows version 10.0 detected!
  41. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  42. log4cplus:ERROR Please initialize the log4cplus system properly.
  43. Qt: Untested Windows version 10.0 detected!
  44. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  45. log4cplus:ERROR Please initialize the log4cplus system properly.
  46. Qt: Untested Windows version 10.0 detected!
  47. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  48. log4cplus:ERROR Please initialize the log4cplus system properly.
  49. Qt: Untested Windows version 10.0 detected!
  50. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  51. log4cplus:ERROR Please initialize the log4cplus system properly.
  52. Qt: Untested Windows version 10.0 detected!
  53. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  54. log4cplus:ERROR Please initialize the log4cplus system properly.
  55. Qt: Untested Windows version 10.0 detected!
  56. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  57. log4cplus:ERROR Please initialize the log4cplus system properly.
  58. Qt: Untested Windows version 10.0 detected!
  59. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  60. log4cplus:ERROR Please initialize the log4cplus system properly.

选择某一个文件,并点击按键“打开”,控制台输出:

J:\PrtSc\20190321\33.png

点击按键“Choose Folder”,控制台的输出(有异常?),以及UI:

  1. Qt: Untested Windows version 10.0 detected!
  2. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  3. log4cplus:ERROR Please initialize the log4cplus system properly.
  4. Qt: Untested Windows version 10.0 detected!
  5. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  6. log4cplus:ERROR Please initialize the log4cplus system properly.
  7. Qt: Untested Windows version 10.0 detected!
  8. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  9. log4cplus:ERROR Please initialize the log4cplus system properly.
  10. Qt: Untested Windows version 10.0 detected!
  11. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  12. log4cplus:ERROR Please initialize the log4cplus system properly.
  13. Qt: Untested Windows version 10.0 detected!
  14. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  15. log4cplus:ERROR Please initialize the log4cplus system properly.
  16. Qt: Untested Windows version 10.0 detected!
  17. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  18. log4cplus:ERROR Please initialize the log4cplus system properly.
  19. Qt: Untested Windows version 10.0 detected!
  20. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  21. log4cplus:ERROR Please initialize the log4cplus system properly.
  22. Qt: Untested Windows version 10.0 detected!
  23. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  24. log4cplus:ERROR Please initialize the log4cplus system properly.
  25. Qt: Untested Windows version 10.0 detected!
  26. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  27. log4cplus:ERROR Please initialize the log4cplus system properly.
  28. Qt: Untested Windows version 10.0 detected!
  29. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  30. log4cplus:ERROR Please initialize the log4cplus system properly.
  31. Qt: Untested Windows version 10.0 detected!
  32. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  33. log4cplus:ERROR Please initialize the log4cplus system properly.
  34. Qt: Untested Windows version 10.0 detected!
  35. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  36. log4cplus:ERROR Please initialize the log4cplus system properly.
  37. Qt: Untested Windows version 10.0 detected!
  38. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  39. log4cplus:ERROR Please initialize the log4cplus system properly.
  40. Qt: Untested Windows version 10.0 detected!
  41. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  42. log4cplus:ERROR Please initialize the log4cplus system properly.
  43. Qt: Untested Windows version 10.0 detected!
  44. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  45. log4cplus:ERROR Please initialize the log4cplus system properly.
  46. Qt: Untested Windows version 10.0 detected!
  47. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  48. log4cplus:ERROR Please initialize the log4cplus system properly.
  49. Qt: Untested Windows version 10.0 detected!
  50. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  51. log4cplus:ERROR Please initialize the log4cplus system properly.
  52. Qt: Untested Windows version 10.0 detected!
  53. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  54. log4cplus:ERROR Please initialize the log4cplus system properly.
  55. Qt: Untested Windows version 10.0 detected!
  56. log4cplus:ERROR No appenders could be found for logger (AdSyncNamespace).
  57. log4cplus:ERROR Please initialize the log4cplus system properly.

选择某一个文件夹,并点击按键“选择文件夹”,控制台输出:

J:\PrtSc\20190321
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/107193
推荐阅读
  

闽ICP备14008679号