diff --git a/src/main/java/org/jcnc/jnotepad/JNotepad.java b/src/main/java/org/jcnc/jnotepad/JNotepad.java index 2e90a29..56edb66 100644 --- a/src/main/java/org/jcnc/jnotepad/JNotepad.java +++ b/src/main/java/org/jcnc/jnotepad/JNotepad.java @@ -1,6 +1,7 @@ package org.jcnc.jnotepad; import javafx.application.Application; +import javafx.concurrent.Task; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; @@ -153,17 +154,30 @@ public class JNotepad extends Application { FileChooser fileChooser = new FileChooser(); File file = fileChooser.showOpenDialog(null); if (file != null) { - try { - getTXT(file); - updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); // 更新文本编码信息 - } catch (IOException e) { - e.printStackTrace(); - } + Task openFileTask = new Task<>() { + @Override + protected Void call() throws Exception { + getTXT(file); + updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); + return null; + } + }; + + openFileTask.setOnSucceeded(e -> { + // 在任务完成后的操作(更新界面等) + }); + + openFileTask.setOnFailed(e -> { + // 在任务失败时的操作(处理异常等) + }); + + Thread thread = new Thread(openFileTask); + thread.start(); } - } } + private void AutoSave(TextArea textArea) { // 在创建文本编辑区后添加文本变更监听器 textArea.textProperty().addListener((observable, oldValue, newValue) -> {