增加配置文件功能

This commit is contained in:
许轲 2023-08-21 01:21:24 +08:00
parent d4d7cdfc68
commit e9ebcfc649
5 changed files with 63 additions and 3 deletions

1
.gitignore vendored
View File

@ -36,3 +36,4 @@ build/
/JNotepad/
/src/main/JNotepad.java
/.idea/
/project.txt

View File

@ -0,0 +1,9 @@
package org.jcnc.jnotepad.Interface;
import java.util.Properties;
public interface ConfigInterface {
void showErrorAlert();
Properties readPropertiesFromFile();
void initializePropertiesFile();
}

View File

@ -4,18 +4,19 @@ import atlantafx.base.theme.PrimerLight;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.jcnc.jnotepad.constants.Constants;
import org.jcnc.jnotepad.controller.manager.Controller;
import org.jcnc.jnotepad.init.Config;
import org.jcnc.jnotepad.ui.LineNumberTextArea;
import org.jcnc.jnotepad.view.init.View;
import org.jcnc.jnotepad.view.manager.ViewManager;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -30,6 +31,12 @@ public class LunchApp extends Application {
@Override
public void start(Stage primaryStage) {
Config config = new Config();
Properties properties = config.readPropertiesFromFile();
String title = properties.getProperty("title", "JNotepad");
view = new View();
Pane root = new Pane();
@ -37,14 +44,13 @@ public class LunchApp extends Application {
double width = Constants.SCREEN_WIDTH;
double length = Constants.SCREEN_LENGTH;
String name = Constants.APP_NAME;
String icon = Constants.APP_ICON;
scene = new Scene(root, width, length);
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm());
primaryStage.setTitle(name);
primaryStage.setTitle(title);
primaryStage.setWidth(width);
primaryStage.setHeight(length);
primaryStage.setScene(scene);

View File

@ -9,4 +9,8 @@ public class Constants {
public static final double SCREEN_LENGTH = 600; //高度
public static final String APP_NAME = "JNotepad"; //名字
public static final String APP_ICON = "/img/icon.png"; //logo地址
//配置文件
public static final String PROPERTY_FILE_NAME = "project.txt"; //配置文件名字
}

View File

@ -0,0 +1,40 @@
package org.jcnc.jnotepad.init;
import javafx.scene.control.Alert;
import org.jcnc.jnotepad.Interface.ConfigInterface;
import java.io.*;
import java.util.Properties;
import static org.jcnc.jnotepad.constants.Constants.PROPERTY_FILE_NAME;
public class Config implements ConfigInterface {
public Properties readPropertiesFromFile() {
Properties properties = new Properties();
try (InputStream inputStream = new FileInputStream(PROPERTY_FILE_NAME)) {
properties.load(inputStream);
} catch (IOException e) {
initializePropertiesFile();
}
return properties;
}
public void initializePropertiesFile() {
Properties defaultProperties = new Properties();
defaultProperties.setProperty("title", "JNotepad");
try (OutputStream outputStream = new FileOutputStream(PROPERTY_FILE_NAME)) {
defaultProperties.store(outputStream, "JNotepad Properties");
} catch (IOException e) {
showErrorAlert();
}
}
public void showErrorAlert() {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText("文件读写错误");
alert.setContentText("文件读写错误");
alert.showAndWait();
}
}