This commit is contained in:
许轲 2023-08-09 14:46:03 +08:00
parent 069ab02599
commit 8aa5b4966c
2 changed files with 56 additions and 25 deletions

View File

@ -12,6 +12,7 @@ import javafx.stage.FileChooser;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.*; import java.io.*;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class JNotepad extends Application { public class JNotepad extends Application {
@ -31,6 +32,7 @@ public class JNotepad extends Application {
// 定义状态栏 // 定义状态栏
Label statusLabel; Label statusLabel;
boolean a = true;
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
@ -65,14 +67,25 @@ public class JNotepad extends Application {
root.setBottom(statusLabel); root.setBottom(statusLabel);
BorderPane.setMargin(statusLabel, new Insets(5, 10, 5, 10)); BorderPane.setMargin(statusLabel, new Insets(5, 10, 5, 10));
List<String> rawParameters = getParameters().getRaw();
//关联文件打开
if (!rawParameters.isEmpty()) {
String filePath = rawParameters.get(0);
openAssociatedFile(filePath);
}
TextArea textArea = new TextArea(); // 创建新的文本编辑区 TextArea textArea = new TextArea(); // 创建新的文本编辑区
autoSave(textArea); // 自动保存 autoSave(textArea); // 自动保存
Tab tab = new Tab("新建文件 " + ++tabIndex); // 创建新的Tab页 if (a) {
tab.setContent(textArea); Tab tab = new Tab("新建文件 " + ++tabIndex); // 创建新的Tab页
tabPane.getTabs().add(tab); tab.setContent(textArea);
tabPane.getSelectionModel().select(tab); tabPane.getTabs().add(tab);
updateStatusLabel(textArea); tabPane.getSelectionModel().select(tab);
updateStatusLabel(textArea);
}
// 创建场景并设置主界面 // 创建场景并设置主界面
Scene scene = new Scene(root, 800, 600); Scene scene = new Scene(root, 800, 600);
@ -104,25 +117,7 @@ public class JNotepad extends Application {
File file = fileChooser.showOpenDialog(null); File file = fileChooser.showOpenDialog(null);
if (file != null) { if (file != null) {
try { try {
BufferedReader reader = new BufferedReader(new FileReader(file)); getTXT(file);
String line;
StringBuilder textBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
textBuilder.append(line).append("\n"); // 读取文件内容
}
reader.close();
String text = textBuilder.toString();
TextArea textArea = new TextArea(text); // 创建新的文本编辑区
autoSave(textArea); // 自动保存
Tab tab = new Tab(file.getName()); // 创建新的Tab页
tab.setContent(textArea);
tab.setUserData(file); // 将文件对象保存到Tab页的UserData中
tabPane.getTabs().add(tab);
tabPane.getSelectionModel().select(tab);
updateStatusLabel(textArea);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -221,6 +216,42 @@ public class JNotepad extends Application {
}); });
} }
private void openAssociatedFile(String filePath) {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
try {
a = false;
getTXT(file);// 读取文件
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 读取文件
private void getTXT(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder textBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
textBuilder.append(line).append("\n"); // 读取文件内容
}
reader.close();
String text = textBuilder.toString();
TextArea textArea = new TextArea(text); // 创建新的文本编辑区
autoSave(textArea); // 自动保存
Tab tab = new Tab(file.getName()); // 创建新的Tab页
tab.setContent(textArea);
tab.setUserData(file); // 将文件对象保存到Tab页的UserData中
tabPane.getTabs().add(tab);
tabPane.getSelectionModel().select(tab);
updateStatusLabel(textArea);
}
// 获取光标所在行数 // 获取光标所在行数
private int getRow(int caretPosition, String text) { private int getRow(int caretPosition, String text) {
return text.substring(0, caretPosition).split("\n").length; return text.substring(0, caretPosition).split("\n").length;

View File

@ -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.3 ` --app-version 1.1.4 `
--vendor "JCNC" --vendor "JCNC"