From dc4f37623ac482bc6218693505317df86ed7ed33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E8=BD=B2?= Date: Fri, 11 Aug 2023 02:30:54 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .idea/uiDesigner.xml | 124 ++++++++++++++ package.txt | 2 +- .../java/org/jcnc/jnotepad/Constants.java | 12 ++ src/main/java/org/jcnc/jnotepad/MainApp.java | 70 ++++++++ .../java/org/jcnc/jnotepad/ViewManager.java | 92 +++++++++++ .../controller.java} | 156 +++--------------- .../java/org/jcnc/jnotepad/view/view.java | 33 ++++ 8 files changed, 359 insertions(+), 131 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/org/jcnc/jnotepad/Constants.java create mode 100644 src/main/java/org/jcnc/jnotepad/MainApp.java create mode 100644 src/main/java/org/jcnc/jnotepad/ViewManager.java rename src/main/java/org/jcnc/jnotepad/{JNotepad.java => controller/controller.java} (62%) create mode 100644 src/main/java/org/jcnc/jnotepad/view/view.java diff --git a/.gitignore b/.gitignore index 1367b22..5cf1ae1 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ build/ ### Mac OS ### .DS_Store /JNotepad/ +/src/main/JNotepad.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/package.txt b/package.txt index e73e37b..3b23ef1 100644 --- a/package.txt +++ b/package.txt @@ -4,5 +4,5 @@ jpackage ` -m org.jcnc.jnotepad/org.jcnc.jnotepad.JNotepad ` --runtime-image .\target\JNotepad\ ` --icon src/main/resources/img/icon.ico ` - --app-version 1.1.5 ` + --app-version 1.1.6 ` --vendor "JCNC" diff --git a/src/main/java/org/jcnc/jnotepad/Constants.java b/src/main/java/org/jcnc/jnotepad/Constants.java new file mode 100644 index 0000000..e5adb40 --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/Constants.java @@ -0,0 +1,12 @@ +package org.jcnc.jnotepad; + +/** + * GameConstants持有所有共享信息的全局变量 + */ + +public class Constants { + + public static final double SCREEN_WIDTH = 800; + public static final double SCREEN_LENGTH = 600; + +} diff --git a/src/main/java/org/jcnc/jnotepad/MainApp.java b/src/main/java/org/jcnc/jnotepad/MainApp.java new file mode 100644 index 0000000..f1e1bf0 --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/MainApp.java @@ -0,0 +1,70 @@ +package org.jcnc.jnotepad; + +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.control.Tab; +import javafx.scene.control.TextArea; +import javafx.scene.image.Image; +import javafx.scene.layout.Pane; +import javafx.stage.Stage; +import org.jcnc.jnotepad.controller.controller; +import org.jcnc.jnotepad.view.view; + +import java.util.List; +import java.util.Objects; + +import static org.jcnc.jnotepad.ViewManager.tabPane; +import static org.jcnc.jnotepad.controller.controller.updateStatusLabel; + +/** + * 启动器 + */ + +public class MainApp extends Application { + public static boolean isRelevance = true; + + @Override + public void start(Stage primaryStage) { + + Pane root = new Pane(); + + double width = Constants.SCREEN_WIDTH; + double length = Constants.SCREEN_LENGTH; + + Scene scene = new Scene(root, width, length); + + primaryStage.setTitle("JNotepad"); + primaryStage.setWidth(width); + primaryStage.setHeight(length); + primaryStage.setScene(scene); + primaryStage.getIcons().add(new Image((Objects.requireNonNull(getClass().getResource("/img/icon.png"))).toString())); + primaryStage.show(); + + ViewManager poolGame = ViewManager.getInstance(scene); + + poolGame.initScreen(scene); + + initApp(); + + + } + + private void initApp() { + List rawParameters = getParameters().getRaw(); + + TextArea textArea = controller.openRelevance(rawParameters); + if (isRelevance) { + Tab tab = new Tab("新建文件 " + ++ViewManager.tabIndex); // 创建新的Tab页 + tab.setContent(textArea); + tabPane.getTabs().add(tab); + tabPane.getSelectionModel().select(tab); + updateStatusLabel(textArea); + } + view.initItem(); + view.initTabPane(); + } + + public static void main(String[] args) { + launch(args); + } +} diff --git a/src/main/java/org/jcnc/jnotepad/ViewManager.java b/src/main/java/org/jcnc/jnotepad/ViewManager.java new file mode 100644 index 0000000..d5ff363 --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/ViewManager.java @@ -0,0 +1,92 @@ +package org.jcnc.jnotepad; + +import javafx.geometry.Insets; +import javafx.scene.Scene; +import javafx.scene.control.*; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.HBox; + + +/** + * 视图管理类 + */ +public class ViewManager { + + public static Label encodingLabel; // 新增属性用于显示文本编码 + + public static int tabIndex = 0; + + // 定义菜单栏 + public static MenuBar menuBar; + public static Menu fileMenu; + public static MenuItem newItem, openItem, saveItem, saveAsItem; + + // 定义主界面 + public static BorderPane root; + + // 定义多个Tab页 + public static TabPane tabPane; + + // 定义状态栏 + public static Label statusLabel; + + private static ViewManager instance = null; + + + public static ViewManager getInstance(Scene scene) { + if(instance == null) { + instance = new ViewManager(scene); + } + + return instance; + } + + /** + * 构造函数,设置场景和根布局 + * @param scene 场景 + */ + private ViewManager(Scene scene){ + + root = new BorderPane(); + + scene.setRoot(root); + } + + public void initScreen(Scene scene){ + // 创建菜单栏并添加菜单项 + menuBar = new MenuBar(); + fileMenu = new Menu("文件"); + newItem = new MenuItem("新建"); + openItem = new MenuItem("打开"); + saveItem = new MenuItem("保存"); + saveAsItem = new MenuItem("另存为"); + fileMenu.getItems().addAll(newItem, openItem, saveItem, saveAsItem); + menuBar.getMenus().add(fileMenu); + + // 创建主界面 + root = new BorderPane(); + root.setTop(menuBar); + + // 创建Tab页和文本编辑区 + tabPane = new TabPane(); + root.setCenter(tabPane); + + // 创建状态栏 + statusLabel = new Label("行: 1 \t列: 1 \t字数: 0 "); + + + encodingLabel = new Label(); // 创建新的标签用于显示编码信息 + HBox statusBox = new HBox(statusLabel, encodingLabel); // 使用 HBox 放置状态标签和编码标签 + root.setBottom(statusBox); + BorderPane.setMargin(statusBox, new Insets(5, 10, 5, 10)); + + scene.setRoot(root); + + + } + + + + + +} diff --git a/src/main/java/org/jcnc/jnotepad/JNotepad.java b/src/main/java/org/jcnc/jnotepad/controller/controller.java similarity index 62% rename from src/main/java/org/jcnc/jnotepad/JNotepad.java rename to src/main/java/org/jcnc/jnotepad/controller/controller.java index bb6a7a9..a387758 100644 --- a/src/main/java/org/jcnc/jnotepad/JNotepad.java +++ b/src/main/java/org/jcnc/jnotepad/controller/controller.java @@ -1,118 +1,23 @@ -package org.jcnc.jnotepad; +package org.jcnc.jnotepad.controller; -import javafx.application.Application; import javafx.concurrent.Task; import javafx.event.ActionEvent; import javafx.event.EventHandler; -import javafx.geometry.Insets; -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.scene.control.Tab; +import javafx.scene.control.TextArea; import javafx.stage.FileChooser; -import javafx.stage.Stage; +import org.jcnc.jnotepad.MainApp; import java.io.*; import java.util.List; -import java.util.Objects; -public class JNotepad extends Application { - String Title = "JNotepad"; - Label encodingLabel; // 新增属性用于显示文本编码 +import static org.jcnc.jnotepad.ViewManager.*; - // 定义菜单栏 - MenuBar menuBar; - Menu fileMenu; - MenuItem newItem, openItem, saveItem, saveAsItem; +public class controller { - // 定义主界面 - BorderPane root; - - // 定义多个Tab页 - TabPane tabPane; - int tabIndex = 0; - - // 定义状态栏 - Label statusLabel; - boolean isRelevance = true; - - @Override - public void start(Stage primaryStage) { - primaryStage.setTitle(Title); - - // 创建菜单栏并添加菜单项 - menuBar = new MenuBar(); - fileMenu = new Menu("文件"); - newItem = new MenuItem("新建"); - openItem = new MenuItem("打开"); - saveItem = new MenuItem("保存"); - saveAsItem = new MenuItem("另存为"); - fileMenu.getItems().addAll(newItem, openItem, saveItem, saveAsItem); - menuBar.getMenus().add(fileMenu); - - // 为菜单项添加事件处理器 - newItem.setOnAction(new NewFileEventHandler()); - openItem.setOnAction(new OpenFileEventHandler()); - saveItem.setOnAction(new SaveFileEventHandler()); - saveAsItem.setOnAction(new SaveAsFileEventHandler()); - - // 创建主界面 - root = new BorderPane(); - root.setTop(menuBar); - - // 创建Tab页和文本编辑区 - tabPane = new TabPane(); - root.setCenter(tabPane); - - // 创建状态栏 - statusLabel = new Label("行: 1 \t列: 1 \t字数: 0 "); - tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> { - if (newTab != null) { - TextArea textArea = (TextArea) newTab.getContent(); - updateStatusLabel(textArea); - } - - if (newTab != null) { - TextArea textArea = (TextArea) newTab.getContent(); - - // Update status label - textArea.caretPositionProperty().addListener((caretObservable, oldPosition, newPosition) -> { - updateStatusLabel(textArea); - }); - - // Update encoding label - updateEncodingLabel(textArea.getText()); - } - }); - 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(); - - TextArea textArea = openRelevance(rawParameters); - - if (isRelevance) { - Tab tab = new Tab("新建文件 " + ++tabIndex); // 创建新的Tab页 - tab.setContent(textArea); - tabPane.getTabs().add(tab); - tabPane.getSelectionModel().select(tab); - updateStatusLabel(textArea); - } - - // 创建场景并设置主界面 - Scene scene = new Scene(root, 800, 600); - primaryStage.setScene(scene); - - primaryStage.getIcons().add(new Image((Objects.requireNonNull(getClass().getResource("/img/icon.png"))).toString())); - - primaryStage.show(); - } //关联文件打开 - private TextArea openRelevance(List rawParameters) { + public static TextArea openRelevance(List rawParameters) { if (!rawParameters.isEmpty()) { String filePath = rawParameters.get(0); openAssociatedFile(filePath); @@ -133,7 +38,7 @@ public class JNotepad extends Application { } // 新建文件事件处理器 - private class NewFileEventHandler implements EventHandler { + public static class NewFileEventHandler implements EventHandler { @Override public void handle(ActionEvent event) { TextArea textArea = new TextArea(); // 创建新的文本编辑区 @@ -148,7 +53,7 @@ public class JNotepad extends Application { } // 打开文件事件处理器 - private class OpenFileEventHandler implements EventHandler { + public static class OpenFileEventHandler implements EventHandler { @Override public void handle(ActionEvent event) { FileChooser fileChooser = new FileChooser(); @@ -178,7 +83,7 @@ public class JNotepad extends Application { } - private void AutoSave(TextArea textArea) { + public static void AutoSave(TextArea textArea) { // 在创建文本编辑区后添加文本变更监听器 textArea.textProperty().addListener((observable, oldValue, newValue) -> { Tab tab = tabPane.getSelectionModel().getSelectedItem(); @@ -190,8 +95,7 @@ public class JNotepad extends Application { writer.write(newValue); // 写入新的文本内容 writer.flush(); writer.close(); - } catch (IOException e) { - e.printStackTrace(); + } catch (IOException ignored) { } } } @@ -199,7 +103,7 @@ public class JNotepad extends Application { } // 保存文件事件处理器 - private class SaveFileEventHandler implements EventHandler { + public static class SaveFileEventHandler implements EventHandler { @Override public void handle(ActionEvent event) { Tab selectedTab = tabPane.getSelectionModel().getSelectedItem(); @@ -217,8 +121,7 @@ public class JNotepad extends Application { writer.write(text); // 写入文件内容 writer.flush(); writer.close(); - } catch (IOException e) { - e.printStackTrace(); + } catch (IOException ignored) { } } } @@ -226,7 +129,7 @@ public class JNotepad extends Application { } // 另存为文件事件处理器 - private class SaveAsFileEventHandler implements EventHandler { + public static class SaveAsFileEventHandler implements EventHandler { @Override public void handle(ActionEvent event) { saveAsFile(); @@ -234,7 +137,7 @@ public class JNotepad extends Application { } // 另存为文件方法 - private void saveAsFile() { + public static void saveAsFile() { Tab selectedTab = tabPane.getSelectionModel().getSelectedItem(); if (selectedTab != null) { FileChooser fileChooser = new FileChooser(); @@ -252,14 +155,13 @@ public class JNotepad extends Application { writer.close(); selectedTab.setText(file.getName()); // 更新Tab页标签上的文件名 selectedTab.setUserData(file); // 将文件对象保存到Tab页的UserData中 - } catch (IOException e) { - e.printStackTrace(); + } catch (IOException ignored) { } } } } - private void updateStatusLabel(TextArea textArea) { + public static void updateStatusLabel(TextArea textArea) { int caretPosition = textArea.getCaretPosition(); int row = getRow(caretPosition, textArea.getText()); int column = getColumn(caretPosition, textArea.getText()); @@ -269,21 +171,20 @@ public class JNotepad extends Application { } //关联文件打开 - private void openAssociatedFile(String filePath) { + public static void openAssociatedFile(String filePath) { File file = new File(filePath); if (file.exists() && file.isFile()) { try { - isRelevance = false; + MainApp.isRelevance = false; getTXT(file);// 读取文件 updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); // 更新文本编码信息 - } catch (IOException e) { - e.printStackTrace(); + } catch (IOException ignored) { } } } // 读取文件 - private void getTXT(File file) throws IOException { + public static void getTXT(File file) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; StringBuilder textBuilder = new StringBuilder(); @@ -305,12 +206,12 @@ public class JNotepad extends Application { updateStatusLabel(textArea); } - private void updateEncodingLabel(String text) { + public static void updateEncodingLabel(String text) { String encoding = detectEncoding(text); encodingLabel.setText("\t编码: " + encoding); } - private boolean isEncodingValid(String text, String encoding) { + public static boolean isEncodingValid(String text, String encoding) { // 编码有效性检查 // 使用指定的编码解码文本,并检查是否出现异常来判断编码是否有效 try { @@ -322,7 +223,7 @@ public class JNotepad extends Application { } } - private String detectEncoding(String text) { + public static String detectEncoding(String text) { // 使用不同的编码(如UTF-8、ISO-8859-1等)来解码文本,并检查是否出现异常来判断编码 String[] possibleEncodings = {"UTF-8", "ISO-8859-1", "UTF-16"}; for (String encoding : possibleEncodings) { @@ -335,18 +236,13 @@ public class JNotepad extends Application { } - // 获取光标所在行数 - private int getRow(int caretPosition, String text) { + public static int getRow(int caretPosition, String text) { return text.substring(0, caretPosition).split("\n").length; } // 获取光标所在列数 - private int getColumn(int caretPosition, String text) { + public static int getColumn(int caretPosition, String text) { return caretPosition - text.lastIndexOf("\n", caretPosition - 1); } - - public static void main(String[] args) { - launch(args); - } } diff --git a/src/main/java/org/jcnc/jnotepad/view/view.java b/src/main/java/org/jcnc/jnotepad/view/view.java new file mode 100644 index 0000000..0c11a98 --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/view/view.java @@ -0,0 +1,33 @@ +package org.jcnc.jnotepad.view; + +import javafx.scene.control.TextArea; +import org.jcnc.jnotepad.controller.controller; + +import static org.jcnc.jnotepad.ViewManager.*; +import static org.jcnc.jnotepad.controller.controller.updateStatusLabel; + +public class view { + + public static void initItem() { + // 为菜单项添加事件处理器 + newItem.setOnAction(new controller.NewFileEventHandler()); + openItem.setOnAction(new controller.OpenFileEventHandler()); + saveItem.setOnAction(new controller.SaveFileEventHandler()); + saveAsItem.setOnAction(new controller.SaveAsFileEventHandler()); + } + + public static void initTabPane() { + tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> { + if (newTab != null) { + TextArea textArea = (TextArea) newTab.getContent(); + updateStatusLabel(textArea); + + // Update status label + textArea.caretPositionProperty().addListener((caretObservable, oldPosition, newPosition) -> updateStatusLabel(textArea)); + + // Update encoding label + controller.updateEncodingLabel(textArea.getText()); + } + }); + } +}