参考: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. linux 下分别使用pip2、pip3

    上次切换了Python2和Python3.但是Python3并没有pip,所有在Python3下不能安装包. 下面实现在Python3 下安装pip3 1,首先安装setuptools wget -- ...

  2. 深入理解计算机系统 Start && 第一章要点

    对此书已经慕名已久了,抽空看了第1,2,3,5章,其他章节等有空闲继续看吧. 我的许多博客是给自己快速复习使用的,比如此读书后感,你可以根据我下面的建议读完原书几章再回来复习一下(或许那时候就没必要回 ...

  3. Dos简易基础及常用Dos命令

    Dos简易基础及常用Dos命令 什么是cmd? cmd是command的缩写,意指操作系统中的命令行程序,一般说的都是Windows中的Dos系统. 如何打开cmd? 键盘操作:Win + R 输入c ...

  4. Agumaster添加股票日交易爬虫画面

  5. PHP 安装 扩展时 抛出 /usr/local/Cellar/php@7.1/7.1.25/pecl 异常解决

    liugx@MacBook-Pro  ~/work/php/ext_source/php-xhprof-extension   master  make installmkdir: /usr/ ...

  6. 解决Maven的JDK版本问题

    在pom文件中添加以下代码 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins&l ...

  7. 关于跨域策略文件crossdomain.xml文件

    下载flexpaper源码修改后做成swf阅读器,要加入待阅读的swf文件,可以在flex里调用js的方法来获取swf文件的路径的方法,在js只专注获取路径就行,等着flex来调用:但这里会遇到一个问 ...

  8. 设计模式也可以这么简单(7年开发老鸟PS注释总结)

    设计模式是对大家实际工作中写的各种代码进行高层次抽象的总结,其中最出名的当属 Gang of Four (GoF) 的分类了,他们将设计模式分类为 23 种经典的模式,根据用途我们又可以分为三大类,分 ...

  9. pycharm代码中批量粘贴内容的快捷键

    windows电脑中,竖向批量复制的快捷键:Alt

  10. [LeetCode]面试题 01.06. 字符串压缩

    题目 字符串压缩.利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能.比如,字符串aabcccccaaa会变为a2b1c5a3.若"压缩"后的字符串没有变短,则返回原先 ...