参考: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

JavaFX FileChooser文件选择器、DirectoryChooser目录选择器的更多相关文章

  1. Java开发桌面程序学习(五)——文件选择器和目录选择器的使用

    选择器的使用 DirectoryChooser目录选择器官方文档 FileChooser文件选择器官方文档 文件选择器的使用 JavaFx中有个FileChoser,可以打开一个对话框来选择文件 Fi ...

  2. JavaFX FileChooser文件选择器,缓存上一次打开的目录

    例1:点击按钮Choose File打开文件选择器,并打开指定的目录.这是通过final void setInitialDirectory(final File value)方法实现的. 1 impo ...

  3. CSS 简介、 选择器、组合选择器

    #CSS 装饰器引入<!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  4. CSS选择器,CSS3选择器

    CSS选择器 ------->CSS选择器优先级 1>通配符选择器(CSS2) 选择所有元素,所有浏览器都支持. *{ margin:0; padding:o; } 2>元素选择器( ...

  5. CSS 简介、语法、派生选择器、id 选择器、类选择器、属性选择器

    CSS 概述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与表现 ...

  6. jQuery选择器与CSS选择器

    1. 通过位置选择的几个操作: :first:默认情况下是相对整个页面来说的第一个,如:li:first表示整个页面的第一个li元素,而ul li:first表示整个页面的第一个li元素,并且是在ul ...

  7. CSS选择器详解(二)通用选择器和高级选择器

    目录 通用选择器 高级选择器 子选择器 相邻兄弟选择器 属性选择器 通用选择器 通用选择器可以选择页面上的所有元素,并对它们应用样式,用 * 来表示. 语法: * { property1: value ...

  8. jQuery事件篇---过滤选择器 & 表单选择器

    内容提纲: 过滤选择器 1.基本过滤器 2.内容过滤器 3.可见性过滤器 4.子元素过滤器 5.其他方法 表单选择器 6.常规选择器 7.表单选择器 8.表单过滤器 发文不易,转载请注明出处! 过滤选 ...

  9. 什么是 jQuery 和jQuery的基本选择器,层级选择器,基本筛选器

    jQuery是什么? [1]   jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2]   jQuery是继prototype ...

随机推荐

  1. 使用python制作趣味小游戏—投骰子

    1.模拟真实环境掷骰子 从Python标准库中调用模块:random——random中包含以各种方式生成随机数的函数 从random中引用randint这一函数——骰子都是有固定面数 from ran ...

  2. 如何使用Xdebug单步调试PHP游戏服务器

    参考文章:https://www.sourcetoad.com/resources/debugging-php-save-time-with-xdebugs-remote-autostart/ 配置参 ...

  3. Shader 的 Blend

    Blend SrcAlpha OneMinusSrcAlpha //alpha blending Blend One OneMinusSrcAlpha //premultiplied alpha bl ...

  4. 初级知识六——C#事件通知系统实现(观察者模式运用)

    观察者模式,绝对是游戏中十分重要的一种模式,运用这种模式,可以让游戏模块间的通信变得简单,耦合度也会大大降低,下面讲解如何利用C#实现事件通知系统. 补充,首先说下这个系统的实现原理,不然一头扎进去就 ...

  5. leetcode-双指针遍历

    编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 char[] 的形式给出.不要给另外的数组分配额外的空间,你必须原地修改输入数组.使用 O(1) 的额外空间解决这一问题.你可以假设 ...

  6. 自定义注解-方法重试@RetryProcess

    背景 在项目开发中,有时候会出现接口调用失败,本身调用又是异步的,如果是因为一些网络问题请求超时,总想可以重试几次把任务处理掉. 一些RPC框架,比如dubbo都是有重试机制的,但是并不是每一个项目多 ...

  7. ES6特性整理

    ESMAScript6.0 ES6 兼容 IE10+ .Chrome.Firefox 要想兼容IE10以下的有两种方法: 用 babel 工具 borwer.js ,在 script 标签里添加 ty ...

  8. 20190923-08Linux压缩和解压类 000 016

    gzip/gunzip 压缩 1.基本语法 gzip 文件 (功能描述:压缩文件,只能将文件压缩为*.gz文件) gunzip 文件.gz (功能描述:解压缩文件命令) 2.经验技巧 (1)只能压缩文 ...

  9. java 将map转为实体类

    使用反射将map转为对象,如果不使用反射的话需要一个get一个set写起来麻烦,并且不通用,所以写了一个通用的方法将map集合转为对象,直接看代码,注释也都挺清楚的 public static < ...

  10. Azure Storage 系列(五)通过Azure.Cosmos.Table 类库在.Net 上使用 Table Storage

    一,引言 上一篇文章我们在.NET 项目中添加了 “WindowsAzure.Storage” 的 NuGet 包进行操作Table 数据,但是使用的 “WindowsAzure.Storage”  ...