重构代码
This commit is contained in:
parent
ac028ee3fb
commit
dc4f37623a
1
.gitignore
vendored
1
.gitignore
vendored
@ -37,3 +37,4 @@ build/
|
|||||||
### Mac OS ###
|
### Mac OS ###
|
||||||
.DS_Store
|
.DS_Store
|
||||||
/JNotepad/
|
/JNotepad/
|
||||||
|
/src/main/JNotepad.java
|
||||||
|
|||||||
124
.idea/uiDesigner.xml
generated
Normal file
124
.idea/uiDesigner.xml
generated
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@ -4,5 +4,5 @@ jpackage `
|
|||||||
-m org.jcnc.jnotepad/org.jcnc.jnotepad.JNotepad `
|
-m org.jcnc.jnotepad/org.jcnc.jnotepad.JNotepad `
|
||||||
--runtime-image .\target\JNotepad\ `
|
--runtime-image .\target\JNotepad\ `
|
||||||
--icon src/main/resources/img/icon.ico `
|
--icon src/main/resources/img/icon.ico `
|
||||||
--app-version 1.1.5 `
|
--app-version 1.1.6 `
|
||||||
--vendor "JCNC"
|
--vendor "JCNC"
|
||||||
|
|||||||
12
src/main/java/org/jcnc/jnotepad/Constants.java
Normal file
12
src/main/java/org/jcnc/jnotepad/Constants.java
Normal file
@ -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;
|
||||||
|
|
||||||
|
}
|
||||||
70
src/main/java/org/jcnc/jnotepad/MainApp.java
Normal file
70
src/main/java/org/jcnc/jnotepad/MainApp.java
Normal file
@ -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<String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
92
src/main/java/org/jcnc/jnotepad/ViewManager.java
Normal file
92
src/main/java/org/jcnc/jnotepad/ViewManager.java
Normal file
@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,118 +1,23 @@
|
|||||||
package org.jcnc.jnotepad;
|
package org.jcnc.jnotepad.controller;
|
||||||
|
|
||||||
import javafx.application.Application;
|
|
||||||
import javafx.concurrent.Task;
|
import javafx.concurrent.Task;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
import javafx.geometry.Insets;
|
import javafx.scene.control.Tab;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.control.TextArea;
|
||||||
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.FileChooser;
|
||||||
import javafx.stage.Stage;
|
import org.jcnc.jnotepad.MainApp;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class JNotepad extends Application {
|
import static org.jcnc.jnotepad.ViewManager.*;
|
||||||
String Title = "JNotepad";
|
|
||||||
Label encodingLabel; // 新增属性用于显示文本编码
|
|
||||||
|
|
||||||
// 定义菜单栏
|
public class controller {
|
||||||
MenuBar menuBar;
|
|
||||||
Menu fileMenu;
|
|
||||||
MenuItem newItem, openItem, saveItem, saveAsItem;
|
|
||||||
|
|
||||||
// 定义主界面
|
|
||||||
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<String> 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<String> rawParameters) {
|
public static TextArea openRelevance(List<String> rawParameters) {
|
||||||
if (!rawParameters.isEmpty()) {
|
if (!rawParameters.isEmpty()) {
|
||||||
String filePath = rawParameters.get(0);
|
String filePath = rawParameters.get(0);
|
||||||
openAssociatedFile(filePath);
|
openAssociatedFile(filePath);
|
||||||
@ -133,7 +38,7 @@ public class JNotepad extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 新建文件事件处理器
|
// 新建文件事件处理器
|
||||||
private class NewFileEventHandler implements EventHandler<ActionEvent> {
|
public static class NewFileEventHandler implements EventHandler<ActionEvent> {
|
||||||
@Override
|
@Override
|
||||||
public void handle(ActionEvent event) {
|
public void handle(ActionEvent event) {
|
||||||
TextArea textArea = new TextArea(); // 创建新的文本编辑区
|
TextArea textArea = new TextArea(); // 创建新的文本编辑区
|
||||||
@ -148,7 +53,7 @@ public class JNotepad extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 打开文件事件处理器
|
// 打开文件事件处理器
|
||||||
private class OpenFileEventHandler implements EventHandler<ActionEvent> {
|
public static class OpenFileEventHandler implements EventHandler<ActionEvent> {
|
||||||
@Override
|
@Override
|
||||||
public void handle(ActionEvent event) {
|
public void handle(ActionEvent event) {
|
||||||
FileChooser fileChooser = new FileChooser();
|
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) -> {
|
textArea.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
Tab tab = tabPane.getSelectionModel().getSelectedItem();
|
Tab tab = tabPane.getSelectionModel().getSelectedItem();
|
||||||
@ -190,8 +95,7 @@ public class JNotepad extends Application {
|
|||||||
writer.write(newValue); // 写入新的文本内容
|
writer.write(newValue); // 写入新的文本内容
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException ignored) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -199,7 +103,7 @@ public class JNotepad extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 保存文件事件处理器
|
// 保存文件事件处理器
|
||||||
private class SaveFileEventHandler implements EventHandler<ActionEvent> {
|
public static class SaveFileEventHandler implements EventHandler<ActionEvent> {
|
||||||
@Override
|
@Override
|
||||||
public void handle(ActionEvent event) {
|
public void handle(ActionEvent event) {
|
||||||
Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
|
Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
|
||||||
@ -217,8 +121,7 @@ public class JNotepad extends Application {
|
|||||||
writer.write(text); // 写入文件内容
|
writer.write(text); // 写入文件内容
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException ignored) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,7 +129,7 @@ public class JNotepad extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 另存为文件事件处理器
|
// 另存为文件事件处理器
|
||||||
private class SaveAsFileEventHandler implements EventHandler<ActionEvent> {
|
public static class SaveAsFileEventHandler implements EventHandler<ActionEvent> {
|
||||||
@Override
|
@Override
|
||||||
public void handle(ActionEvent event) {
|
public void handle(ActionEvent event) {
|
||||||
saveAsFile();
|
saveAsFile();
|
||||||
@ -234,7 +137,7 @@ public class JNotepad extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 另存为文件方法
|
// 另存为文件方法
|
||||||
private void saveAsFile() {
|
public static void saveAsFile() {
|
||||||
Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
|
Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
|
||||||
if (selectedTab != null) {
|
if (selectedTab != null) {
|
||||||
FileChooser fileChooser = new FileChooser();
|
FileChooser fileChooser = new FileChooser();
|
||||||
@ -252,14 +155,13 @@ public class JNotepad extends Application {
|
|||||||
writer.close();
|
writer.close();
|
||||||
selectedTab.setText(file.getName()); // 更新Tab页标签上的文件名
|
selectedTab.setText(file.getName()); // 更新Tab页标签上的文件名
|
||||||
selectedTab.setUserData(file); // 将文件对象保存到Tab页的UserData中
|
selectedTab.setUserData(file); // 将文件对象保存到Tab页的UserData中
|
||||||
} catch (IOException e) {
|
} catch (IOException ignored) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateStatusLabel(TextArea textArea) {
|
public static void updateStatusLabel(TextArea textArea) {
|
||||||
int caretPosition = textArea.getCaretPosition();
|
int caretPosition = textArea.getCaretPosition();
|
||||||
int row = getRow(caretPosition, textArea.getText());
|
int row = getRow(caretPosition, textArea.getText());
|
||||||
int column = getColumn(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);
|
File file = new File(filePath);
|
||||||
if (file.exists() && file.isFile()) {
|
if (file.exists() && file.isFile()) {
|
||||||
try {
|
try {
|
||||||
isRelevance = false;
|
MainApp.isRelevance = false;
|
||||||
getTXT(file);// 读取文件
|
getTXT(file);// 读取文件
|
||||||
updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); // 更新文本编码信息
|
updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); // 更新文本编码信息
|
||||||
} catch (IOException e) {
|
} catch (IOException ignored) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取文件
|
// 读取文件
|
||||||
private void getTXT(File file) throws IOException {
|
public static void getTXT(File file) throws IOException {
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
String line;
|
String line;
|
||||||
StringBuilder textBuilder = new StringBuilder();
|
StringBuilder textBuilder = new StringBuilder();
|
||||||
@ -305,12 +206,12 @@ public class JNotepad extends Application {
|
|||||||
updateStatusLabel(textArea);
|
updateStatusLabel(textArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateEncodingLabel(String text) {
|
public static void updateEncodingLabel(String text) {
|
||||||
String encoding = detectEncoding(text);
|
String encoding = detectEncoding(text);
|
||||||
encodingLabel.setText("\t编码: " + encoding);
|
encodingLabel.setText("\t编码: " + encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isEncodingValid(String text, String encoding) {
|
public static boolean isEncodingValid(String text, String encoding) {
|
||||||
// 编码有效性检查
|
// 编码有效性检查
|
||||||
// 使用指定的编码解码文本,并检查是否出现异常来判断编码是否有效
|
// 使用指定的编码解码文本,并检查是否出现异常来判断编码是否有效
|
||||||
try {
|
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等)来解码文本,并检查是否出现异常来判断编码
|
// 使用不同的编码(如UTF-8、ISO-8859-1等)来解码文本,并检查是否出现异常来判断编码
|
||||||
String[] possibleEncodings = {"UTF-8", "ISO-8859-1", "UTF-16"};
|
String[] possibleEncodings = {"UTF-8", "ISO-8859-1", "UTF-16"};
|
||||||
for (String encoding : possibleEncodings) {
|
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;
|
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);
|
return caretPosition - text.lastIndexOf("\n", caretPosition - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
launch(args);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
33
src/main/java/org/jcnc/jnotepad/view/view.java
Normal file
33
src/main/java/org/jcnc/jnotepad/view/view.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user