From f424584caa35ec14a3655c572816e989e9e464ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E8=BD=B2?= Date: Thu, 10 Aug 2023 15:26:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=90=8E=E5=8F=B0=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E5=8A=A0=E8=BD=BD=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/jcnc/jnotepad/JNotepad.java | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) 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) -> {