DirectoryChooser类是JavaFX的一部分。DirectoryChooser类显示一个目录选择器对话框,允许用户选择特定的目录。打开目录对话框可能总是导致无操作,即返回空文件。DirectoryChooser类继承对象类。
null
类的构造函数:
- DirectoryChooser() :创建目录选择器的新对象。
常用方法:
方法 | 解释 |
---|---|
getInitialDirectory() | 返回目录选择器的initialDirectory。 |
getTitle() | 返回目录选择器的标题。 |
setInitialDirectory(文件val) | 设置属性initialDirectory的值 |
setTitle(字符串t) | 设置目录选择器的标题。 |
showDialog(窗口w) | 显示一个新的目录选择对话框。 |
下面的程序演示了DirectoryChooser类的使用:
- 用于创建DirectoryChooser并将其添加到后台的Java程序: 在这个程序中,我们将创建一个名为 直选器 .创建一个名为 标签 还有一个名为 按钮 .创建一个EventHandler,以便在按下按钮时处理事件。按下按钮时,会出现目录选择器对话框,所选目录将显示为文本 标签 .将标签和按钮添加到 Vbox 并添加 VBox 将场景添加到舞台,然后调用 show() 函数来显示最终结果。
// Java Program to create DirectoryChooser
// and add it to the stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.geometry.*;
import
javafx.scene.paint.*;
import
javafx.scene.canvas.*;
import
javafx.scene.text.*;
import
javafx.scene.Group;
import
javafx.scene.shape.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.collections.*;
import
java.io.*;
import
javafx.stage.DirectoryChooser;
public
class
DirectoryChooser_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"DirectoryChooser"
);
// create a Directory chooser
DirectoryChooser dir_chooser =
new
DirectoryChooser();
// create a Label
Label label =
new
Label(
"no files selected"
);
// create a Button
Button button =
new
Button(
"Show"
);
// create an Event Handler
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
// get the file selected
File file = dir_chooser.showDialog(stage);
if
(file !=
null
) {
label.setText(file.getAbsolutePath() +
" selected"
);
}
}
};
button.setOnAction(event);
// create a VBox
VBox vbox =
new
VBox(
30
, label, button);
// set Alignment
vbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(vbox,
800
,
500
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
- 用于创建DirectoryChooser、设置标题、初始目录并将其添加到后台的Java程序: 在这个程序中,我们将创建一个名为 直选器 .创建一个名为 标签 还有一个名为 按钮 .使用 setTitle() 和 setInitialDirectory() 作用我们将创建一个EventHandler,在按下按钮时处理事件。按下该按钮时,将出现目录选择器对话框,所选目录将在对话框中显示为文本 标签 .将标签和按钮添加到 Vbox 并添加 VBox 将场景添加到舞台,然后调用 show() 函数来显示最终结果。
// Java Program to create DirectoryChooser,
// set title, initial directory
// and add it to the stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.geometry.*;
import
javafx.scene.paint.*;
import
javafx.scene.canvas.*;
import
javafx.scene.text.*;
import
javafx.scene.Group;
import
javafx.scene.shape.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.collections.*;
import
java.io.*;
import
javafx.stage.DirectoryChooser;
public
class
DirectoryChooser_2
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"DirectoryChooser"
);
// create a Directory chooser
DirectoryChooser dir_chooser =
new
DirectoryChooser();
// set title
dir_chooser.setTitle(
"Select directory"
);
// set initial directory
dir_chooser.setInitialDirectory(
new
File(
"e:\"
));
// create a Label
Label label =
new
Label(
"no files selected"
);
// create a Button
Button button =
new
Button(
"Show"
);
// create an Event Handler
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
// get the file selected
File file = dir_chooser.showDialog(stage);
if
(file !=
null
) {
label.setText(file.getAbsolutePath() +
" selected"
);
}
}
};
button.setOnAction(event);
// create a VBox
VBox vbox =
new
VBox(
30
, label, button);
// set Alignment
vbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(vbox,
800
,
500
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注: 以上程序可能无法在联机IDE中运行,请使用脱机编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/DirectoryChooser.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END