From 7363e5749f6ecb818cf04f3163a34188b0360c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E8=BD=B2?= Date: Thu, 10 Aug 2023 14:55:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E6=9C=AC=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/jcnc/jnotepad/JNotepad.java | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/jcnc/jnotepad/JNotepad.java b/src/main/java/org/jcnc/jnotepad/JNotepad.java index 5afc8ec..cd07547 100644 --- a/src/main/java/org/jcnc/jnotepad/JNotepad.java +++ b/src/main/java/org/jcnc/jnotepad/JNotepad.java @@ -34,7 +34,7 @@ public class JNotepad extends Application { // 定义状态栏 Label statusLabel; - boolean a = true; + boolean isRelevance = true; @Override public void start(Stage primaryStage) { @@ -65,7 +65,7 @@ public class JNotepad extends Application { root.setCenter(tabPane); // 创建状态栏 - statusLabel = new Label("行: 1 \t列: 1 \t字数: 0 \t编码: "); + statusLabel = new Label("行: 1 \t列: 1 \t字数: 0 "); encodingLabel = new Label(); // 创建新的标签用于显示编码信息 HBox statusBox = new HBox(statusLabel, encodingLabel); // 使用 HBox 放置状态标签和编码标签 root.setBottom(statusBox); @@ -82,18 +82,16 @@ public class JNotepad extends Application { TextArea textArea = new TextArea(); // 创建新的文本编辑区 + updateEncodingLabel(textArea.getText()); // 更新文本编码信息 + // 添加文本变更监听器 textArea.textProperty().addListener((observable, oldValue, newValue) -> { // 更新状态栏信息 updateStatusLabel(textArea); - // 更新编码信息 - //updateEncodingLabel(newValue); - updateEncodingLabel(textArea.getText()); // 更新文本编码信息 - }); - autoSave(textArea); // 自动保存 + AutoSave(textArea); // 自动保存 - if (a) { + if (isRelevance) { Tab tab = new Tab("新建文件 " + ++tabIndex); // 创建新的Tab页 tab.setContent(textArea); tabPane.getTabs().add(tab); @@ -139,10 +137,11 @@ public class JNotepad extends Application { e.printStackTrace(); } } + } } - private void autoSave(TextArea textArea) { + private void AutoSave(TextArea textArea) { // 在创建文本编辑区后添加文本变更监听器 textArea.textProperty().addListener((observable, oldValue, newValue) -> { Tab tab = tabPane.getSelectionModel().getSelectedItem(); @@ -209,7 +208,7 @@ public class JNotepad extends Application { try { BufferedWriter writer = new BufferedWriter(new FileWriter(file)); TextArea textArea = (TextArea) selectedTab.getContent(); // 获取当前Tab页的文本编辑区 - autoSave(textArea);// 自动保存 + AutoSave(textArea);// 自动保存 String text = textArea.getText(); writer.write(text); // 写入文件内容 writer.flush(); @@ -233,11 +232,12 @@ public class JNotepad extends Application { }); } + //关联文件打开 private void openAssociatedFile(String filePath) { File file = new File(filePath); if (file.exists() && file.isFile()) { try { - a = false; + isRelevance = false; getTXT(file);// 读取文件 updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); // 更新文本编码信息 } catch (IOException e) { @@ -258,7 +258,7 @@ public class JNotepad extends Application { String text = textBuilder.toString(); TextArea textArea = new TextArea(text); // 创建新的文本编辑区 - autoSave(textArea); // 自动保存 + AutoSave(textArea); // 自动保存 Tab tab = new Tab(file.getName()); // 创建新的Tab页 tab.setContent(textArea); @@ -270,29 +270,35 @@ public class JNotepad extends Application { } private void updateEncodingLabel(String text) { - String[] commonEncodings = {"UTF-8", "ISO-8859-1", "UTF-16", "US-ASCII"}; // 常见编码 - String detectedEncoding = ""; // 默认值 - - for (String encoding : commonEncodings) { - if (isEncodingValid(text, encoding)) { - detectedEncoding = encoding; - break; - } - } - - encodingLabel.setText("\t编码: "+detectedEncoding); + String encoding = detectEncoding(text); + // 假设您有一个名为"encodingLabel"的标签控件来显示编码信息 + encodingLabel.setText("\t编码: " + encoding); } private boolean isEncodingValid(String text, String encoding) { + // 在这里实现您的编码有效性检查逻辑 + // 比如,您可以尝试使用指定的编码解码文本,并检查是否出现异常来判断编码是否有效 try { byte[] bytes = text.getBytes(encoding); String decodedText = new String(bytes, encoding); return text.equals(decodedText); - } catch (Exception e) { + } catch (UnsupportedEncodingException e) { return false; } } + private String detectEncoding(String text) { + // 使用不同的编码(如UTF-8、ISO-8859-1等)来解码文本,并检查是否出现异常来判断编码 + String[] possibleEncodings = {"UTF-8", "ISO-8859-1", "UTF-16"}; + for (String encoding : possibleEncodings) { + if (isEncodingValid(text, encoding)) { + System.out.println("正在检测"); + return encoding; + } + } + return "未知"; + } + // 获取光标所在行数