From 3e0f00423470accf7ac0cb27249a1bce17064981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E8=BD=B2?= Date: Thu, 10 Aug 2023 09:55:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=96=E7=A0=81=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/encodings.xml | 1 + src/main/java/org/jcnc/jnotepad/JNotepad.java | 49 +++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/.idea/encodings.xml b/.idea/encodings.xml index aa00ffa..6328d62 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -3,5 +3,6 @@ + \ No newline at end of file diff --git a/src/main/java/org/jcnc/jnotepad/JNotepad.java b/src/main/java/org/jcnc/jnotepad/JNotepad.java index cc30043..ff9d097 100644 --- a/src/main/java/org/jcnc/jnotepad/JNotepad.java +++ b/src/main/java/org/jcnc/jnotepad/JNotepad.java @@ -8,6 +8,7 @@ import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.layout.BorderPane; +import javafx.scene.layout.HBox; import javafx.stage.FileChooser; import javafx.stage.Stage; @@ -17,6 +18,9 @@ import java.util.Objects; public class JNotepad extends Application { String Title = "JNotepad"; + private String lastDetectedEncoding = "未知"; + + Label encodingLabel; // 新增属性用于显示文本编码 // 定义菜单栏 MenuBar menuBar; @@ -63,9 +67,12 @@ public class JNotepad extends Application { root.setCenter(tabPane); // 创建状态栏 - statusLabel = new Label("行: 1 \t列: 1 \t字数: 0"); - root.setBottom(statusLabel); - BorderPane.setMargin(statusLabel, new Insets(5, 10, 5, 10)); + statusLabel = new Label("行: 1 \t列: 1 \t字数: 0 \t编码: "); + encodingLabel = new Label(); // 创建新的标签用于显示编码信息 + HBox statusBox = new HBox(statusLabel, encodingLabel); // 使用 HBox 放置状态标签和编码标签 + root.setBottom(statusBox); + BorderPane.setMargin(statusBox, new Insets(5, 10, 5, 10)); + List rawParameters = getParameters().getRaw(); @@ -76,6 +83,13 @@ public class JNotepad extends Application { } TextArea textArea = new TextArea(); // 创建新的文本编辑区 + + // 添加文本变更监听器 + textArea.textProperty().addListener((observable, oldValue, newValue) -> { + // 更新状态栏信息 + updateStatusLabel(textArea); + updateEncodingLabel(textArea.getText()); // 更新文本编码信息 + }); autoSave(textArea); // 自动保存 if (a) { @@ -87,6 +101,8 @@ public class JNotepad extends Application { } + + // 创建场景并设置主界面 Scene scene = new Scene(root, 800, 600); primaryStage.setScene(scene); @@ -212,7 +228,7 @@ public class JNotepad extends Application { int row = getRow(textArea.getCaretPosition(), textArea.getText()); int column = getColumn(textArea.getCaretPosition(), textArea.getText()); int length = textArea.getLength(); - statusLabel.setText("行: " + row + " \t列: " + column + " \t字数: " + length); + statusLabel.setText("行: " + row + " \t列: " + column + " \t字数: " + length+"\t"); }); } @@ -251,6 +267,31 @@ public class JNotepad extends Application { updateStatusLabel(textArea); } + 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("编码: " + detectedEncoding); + } + + 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) { + return false; + } + } + + // 获取光标所在行数 private int getRow(int caretPosition, String text) {