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
JavaFX FileChooser文件选择器、DirectoryChooser目录选择器的更多相关文章
- Java开发桌面程序学习(五)——文件选择器和目录选择器的使用
选择器的使用 DirectoryChooser目录选择器官方文档 FileChooser文件选择器官方文档 文件选择器的使用 JavaFx中有个FileChoser,可以打开一个对话框来选择文件 Fi ...
- JavaFX FileChooser文件选择器,缓存上一次打开的目录
例1:点击按钮Choose File打开文件选择器,并打开指定的目录.这是通过final void setInitialDirectory(final File value)方法实现的. 1 impo ...
- CSS 简介、 选择器、组合选择器
#CSS 装饰器引入<!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- CSS选择器,CSS3选择器
CSS选择器 ------->CSS选择器优先级 1>通配符选择器(CSS2) 选择所有元素,所有浏览器都支持. *{ margin:0; padding:o; } 2>元素选择器( ...
- CSS 简介、语法、派生选择器、id 选择器、类选择器、属性选择器
CSS 概述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与表现 ...
- jQuery选择器与CSS选择器
1. 通过位置选择的几个操作: :first:默认情况下是相对整个页面来说的第一个,如:li:first表示整个页面的第一个li元素,而ul li:first表示整个页面的第一个li元素,并且是在ul ...
- CSS选择器详解(二)通用选择器和高级选择器
目录 通用选择器 高级选择器 子选择器 相邻兄弟选择器 属性选择器 通用选择器 通用选择器可以选择页面上的所有元素,并对它们应用样式,用 * 来表示. 语法: * { property1: value ...
- jQuery事件篇---过滤选择器 & 表单选择器
内容提纲: 过滤选择器 1.基本过滤器 2.内容过滤器 3.可见性过滤器 4.子元素过滤器 5.其他方法 表单选择器 6.常规选择器 7.表单选择器 8.表单过滤器 发文不易,转载请注明出处! 过滤选 ...
- 什么是 jQuery 和jQuery的基本选择器,层级选择器,基本筛选器
jQuery是什么? [1] jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2] jQuery是继prototype ...
随机推荐
- laravel Application实例化后两个方法
laravel容器初始化registerBaseServiceProviders方法 上篇讲解了laravel容器的基本使用和原理,这篇继续Application构造方法中的registerBaseS ...
- 【jmespath】—2. 进阶 List and Slice Projections
Projections翻译的话说实话我也不知道翻成什么比较准确,姑且就叫"投影"吧,不过带来的功能确实非常的强大.好用. 首先,我先说下我的理解: 通配符 * 通配符生成的是一个l ...
- Stack (30)(模拟栈,输出中间数用set)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- Spark应用开发-关联分析
在机器学习中,常用的主题有分类,回归,聚类和关联分析.而关联分析,在实际中的应用场景,有部分是用于商品零售的分析.在Spark中有相应的案例 在关联分析中,有一些概念要熟悉. 频繁项集,关联规则,支持 ...
- 14 el-dialog 基本结构
1 dialogVisible父组件提供,:visible.sync直接修改父组件的dialogVisible,会报错,需要加上before-close属性 <template> < ...
- Cassandra使用 —— 一个气象站的例子
使用场景: Cassandra非常适合存储时序类型的数据,本文我们使用一个气象站的例子(该气象站每分钟需要存储一条温度数据). 一.方案1:每个设备占用一行 这个方案的思路就是给每个数据源创建一行,比 ...
- leetcode刷题-89格雷编码
题目 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.即使有多个不同答案,你也只需要返回其中一种. 格雷编码序列 ...
- Selenium-WebDriver安装
一.chrome浏览器: 根据chrome浏览器版本,下载对应的驱动 chromedriver版本 支持的Chrome版本 v2.37 v64-66 v2.36 v63-65 v2.35 v62-64 ...
- 分别用canvas和css3的transform做出钟表的效果
两种方式实际上在js上的原理都是一样的.都是获取时间对象,再获取时间对象的时分秒,时分秒乘以其旋转一刻度(一秒.一分.一小时)对应的角度.css3中要赋值于transform:rotate(角度),c ...
- Python实现自动生成小学四则运算题目
Github地址: https://github.com/guoyuyi/gyy.github.io/blob/%E4%BD%9C%E4%B8%9A1/zy1.py 题目描述: 通过python语言编 ...