增加img
This commit is contained in:
parent
cb749074c0
commit
8854fa6191
@ -6,9 +6,8 @@ package org.jcnc.jnotepad;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final double SCREEN_WIDTH = 800;
|
||||
public static final double SCREEN_LENGTH = 600;
|
||||
|
||||
public static final String APP_NAME = "JNotepad";
|
||||
public static final String APP_ICON = "/img/icon.png";
|
||||
public static final double SCREEN_WIDTH = 800; //宽度
|
||||
public static final double SCREEN_LENGTH = 600; //高度
|
||||
public static final String APP_NAME = "JNotepad"; //名字
|
||||
public static final String APP_ICON = "/img/icon.png"; //logo地址
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ 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 org.jcnc.jnotepad.view.View;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@ -16,22 +16,17 @@ 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;
|
||||
String name = Constants.APP_NAME;
|
||||
String icon =Constants.APP_ICON;
|
||||
String icon = Constants.APP_ICON;
|
||||
|
||||
Scene scene = new Scene(root, width, length);
|
||||
|
||||
@ -46,24 +41,26 @@ public class MainApp extends Application {
|
||||
|
||||
viewManager.initScreen(scene);
|
||||
|
||||
// 初始化应用程序
|
||||
initApp();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void initApp() {
|
||||
List<String> rawParameters = getParameters().getRaw();
|
||||
|
||||
// 打开关联文件并创建文本区域
|
||||
TextArea textArea = Controller.openAssociatedFileAndCreateTextArea(rawParameters);
|
||||
if (isRelevance) {
|
||||
Tab tab = new Tab("新建文件 " + ++ViewManager.tabIndex); // 创建新的Tab页
|
||||
// 创建新标签页并添加到标签栏
|
||||
Tab tab = new Tab("新建文件 " + ++ViewManager.tabIndex);
|
||||
tab.setContent(textArea);
|
||||
tabPane.getTabs().add(tab);
|
||||
tabPane.getSelectionModel().select(tab);
|
||||
updateStatusLabel(textArea);
|
||||
}
|
||||
view.initItem();
|
||||
view.initTabPane();
|
||||
// 初始化菜单项和标签栏
|
||||
View.initItem();
|
||||
View.initTabPane();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@ -13,13 +13,21 @@ import java.util.List;
|
||||
|
||||
import static org.jcnc.jnotepad.ViewManager.*;
|
||||
|
||||
/**
|
||||
* 控制器类负责处理与用户界面的交互,并实现相关事件处理逻辑。
|
||||
*/
|
||||
public class Controller {
|
||||
|
||||
//关联文件打开
|
||||
/**
|
||||
* 打开关联文件并创建一个文本编辑区。
|
||||
*
|
||||
* @param rawParameters 文件路径参数的列表
|
||||
* @return 创建的文本编辑区
|
||||
*/
|
||||
public static TextArea openAssociatedFileAndCreateTextArea(List<String> rawParameters) {
|
||||
if (!rawParameters.isEmpty()) {
|
||||
String filePath = rawParameters.get(0);
|
||||
openAssociatedFile(filePath);
|
||||
openAssociatedFile(filePath); //// 调用关联文件打开方法
|
||||
}
|
||||
|
||||
TextArea textArea = new TextArea(); // 创建新的文本编辑区
|
||||
@ -81,9 +89,9 @@ public class Controller {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 自动保存方法
|
||||
public static void AutoSave(TextArea textArea) {
|
||||
// 在创建文本编辑区后添加文本变更监听器
|
||||
// 当文本编辑区内容发生变化时,自动保存文本到文件
|
||||
textArea.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||
Tab tab = tabPane.getSelectionModel().getSelectedItem();
|
||||
if (tab != null) {
|
||||
@ -95,6 +103,7 @@ public class Controller {
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (IOException ignored) {
|
||||
// 处理异常,忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -160,6 +169,7 @@ public class Controller {
|
||||
}
|
||||
}
|
||||
|
||||
// 更新状态栏标签信息
|
||||
public static void updateStatusLabel(TextArea textArea) {
|
||||
int caretPosition = textArea.getCaretPosition();
|
||||
int row = getRow(caretPosition, textArea.getText());
|
||||
@ -169,26 +179,28 @@ public class Controller {
|
||||
System.out.println(" 正在监测字数");
|
||||
}
|
||||
|
||||
//关联文件打开
|
||||
// 关联文件打开
|
||||
public static void openAssociatedFile(String filePath) {
|
||||
// 根据给定的文件路径打开关联文件
|
||||
File file = new File(filePath);
|
||||
if (file.exists() && file.isFile()) {
|
||||
try {
|
||||
MainApp.isRelevance = false;
|
||||
getTXT(file);// 读取文件
|
||||
getTXT(file);// 调用读取文件方法
|
||||
updateEncodingLabel(((TextArea) tabPane.getSelectionModel().getSelectedItem().getContent()).getText()); // 更新文本编码信息
|
||||
} catch (IOException ignored) {
|
||||
// 处理异常,忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 读取文件
|
||||
// 读取文件并创建文本编辑区
|
||||
public static 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"); // 读取文件内容
|
||||
textBuilder.append(line).append("\n"); // 逐行读取文件内容
|
||||
}
|
||||
reader.close();
|
||||
String text = textBuilder.toString();
|
||||
@ -204,12 +216,12 @@ public class Controller {
|
||||
tabPane.getSelectionModel().select(tab);
|
||||
updateStatusLabel(textArea);
|
||||
}
|
||||
|
||||
// 更新文本编码标签信息
|
||||
public static void updateEncodingLabel(String text) {
|
||||
String encoding = detectEncoding(text);
|
||||
encodingLabel.setText("\t编码: " + encoding);
|
||||
}
|
||||
|
||||
// 判断编码是否有效
|
||||
public static boolean isEncodingValid(String text, String encoding) {
|
||||
// 编码有效性检查
|
||||
// 使用指定的编码解码文本,并检查是否出现异常来判断编码是否有效
|
||||
@ -221,7 +233,7 @@ public class Controller {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 检测文本编码
|
||||
public static String detectEncoding(String text) {
|
||||
// 使用不同的编码(如UTF-8、ISO-8859-1等)来解码文本,并检查是否出现异常来判断编码
|
||||
String[] possibleEncodings = {"UTF-8", "ISO-8859-1", "UTF-16"};
|
||||
@ -234,7 +246,6 @@ public class Controller {
|
||||
return "未知";
|
||||
}
|
||||
|
||||
|
||||
// 获取光标所在行数
|
||||
public static int getRow(int caretPosition, String text) {
|
||||
return text.substring(0, caretPosition).split("\n").length;
|
||||
|
||||
@ -3,13 +3,18 @@ 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.ViewManager.newItem;
|
||||
import static org.jcnc.jnotepad.ViewManager.openItem;
|
||||
import static org.jcnc.jnotepad.ViewManager.saveItem;
|
||||
import static org.jcnc.jnotepad.ViewManager.saveAsItem;
|
||||
import static org.jcnc.jnotepad.ViewManager.tabPane;
|
||||
import static org.jcnc.jnotepad.controller.Controller.updateEncodingLabel;
|
||||
import static org.jcnc.jnotepad.controller.Controller.updateStatusLabel;
|
||||
|
||||
public class view {
|
||||
public class View {
|
||||
|
||||
public static void initItem() {
|
||||
// 为菜单项添加事件处理器
|
||||
// 初始化菜单项的事件处理器
|
||||
newItem.setOnAction(new Controller.NewFileEventHandler());
|
||||
openItem.setOnAction(new Controller.OpenFileEventHandler());
|
||||
saveItem.setOnAction(new Controller.SaveFileEventHandler());
|
||||
@ -19,14 +24,17 @@ public class view {
|
||||
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());
|
||||
// 更新编码标签
|
||||
updateEncodingLabel(textArea.getText());
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user