javafx切换页面
Platform.runLater(new Runnable() { // 主线程中完成
@Override
public void run() {
btn_login.setDisable(false); // 还原按钮
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("diractivity/activity_choise_dir.fxml")); // 静态读取,无法获取controller
if (loader == null) {
return;
}
GridPane rootLayout = loader.load();
// Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds(); //获取屏幕宽高
Scene scene = new Scene(rootLayout, 562, 342);
primaryStage.setScene(scene);
// primaryStage.setMaximized(true); //全屏
DirChoiseController dirChoiseController = loader.getController();
dirChoiseController.init(primaryStage); //为了controller中使用primaryStage
} catch (Exception e) {
e.printStackTrace();
}
}
});
自定义对话框
private void showChoiseDialog() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("chosiedialog/dialog_choise.fxml")); // 静态读取,无法获取controller
try {
if (loader == null) {
return;
}
GridPane rootLayout = loader.load();
JFXAlert alert = new JFXAlert(primaryStage);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setOverlayClose(false);
alert.setContent(rootLayout);
alert.show();
DialogChoiseController dialogChoiseController = loader.getController();
dialogChoiseController.init(primaryStage);
dialogChoiseController.setAlert(alert);
} catch (IOException e) {
e.printStackTrace();
}
}
选择系统文件/文件夹功能
- DirectoryChooser 选择文件夹
- FileChooser 选择文件
@FXML
public void onInput2Click(MouseEvent mouseEvent) {
Node source = (Node) mouseEvent.getSource();
Window theStage = source.getScene().getWindow();
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("选择文件夹路径二");
File file = directoryChooser.showDialog(theStage);
if (file != null) {
tf_dir2.setText(file.getPath());
}
}