增加配置文件功能
This commit is contained in:
parent
d4d7cdfc68
commit
e9ebcfc649
1
.gitignore
vendored
1
.gitignore
vendored
@ -36,3 +36,4 @@ build/
|
|||||||
/JNotepad/
|
/JNotepad/
|
||||||
/src/main/JNotepad.java
|
/src/main/JNotepad.java
|
||||||
/.idea/
|
/.idea/
|
||||||
|
/project.txt
|
||||||
|
|||||||
@ -0,0 +1,9 @@
|
|||||||
|
package org.jcnc.jnotepad.Interface;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public interface ConfigInterface {
|
||||||
|
void showErrorAlert();
|
||||||
|
Properties readPropertiesFromFile();
|
||||||
|
void initializePropertiesFile();
|
||||||
|
}
|
||||||
@ -4,18 +4,19 @@ import atlantafx.base.theme.PrimerLight;
|
|||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
|
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import javafx.scene.layout.Pane;
|
import javafx.scene.layout.Pane;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import org.jcnc.jnotepad.constants.Constants;
|
import org.jcnc.jnotepad.constants.Constants;
|
||||||
import org.jcnc.jnotepad.controller.manager.Controller;
|
import org.jcnc.jnotepad.controller.manager.Controller;
|
||||||
|
import org.jcnc.jnotepad.init.Config;
|
||||||
import org.jcnc.jnotepad.ui.LineNumberTextArea;
|
import org.jcnc.jnotepad.ui.LineNumberTextArea;
|
||||||
import org.jcnc.jnotepad.view.init.View;
|
import org.jcnc.jnotepad.view.init.View;
|
||||||
import org.jcnc.jnotepad.view.manager.ViewManager;
|
import org.jcnc.jnotepad.view.manager.ViewManager;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Properties;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@ -30,6 +31,12 @@ public class LunchApp extends Application {
|
|||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) {
|
public void start(Stage primaryStage) {
|
||||||
|
|
||||||
|
Config config = new Config();
|
||||||
|
|
||||||
|
Properties properties = config.readPropertiesFromFile();
|
||||||
|
String title = properties.getProperty("title", "JNotepad");
|
||||||
|
|
||||||
|
|
||||||
view = new View();
|
view = new View();
|
||||||
|
|
||||||
Pane root = new Pane();
|
Pane root = new Pane();
|
||||||
@ -37,14 +44,13 @@ public class LunchApp extends Application {
|
|||||||
|
|
||||||
double width = Constants.SCREEN_WIDTH;
|
double width = Constants.SCREEN_WIDTH;
|
||||||
double length = Constants.SCREEN_LENGTH;
|
double length = Constants.SCREEN_LENGTH;
|
||||||
String name = Constants.APP_NAME;
|
|
||||||
String icon = Constants.APP_ICON;
|
String icon = Constants.APP_ICON;
|
||||||
|
|
||||||
scene = new Scene(root, width, length);
|
scene = new Scene(root, width, length);
|
||||||
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
|
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
|
||||||
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm());
|
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/styles.css")).toExternalForm());
|
||||||
|
|
||||||
primaryStage.setTitle(name);
|
primaryStage.setTitle(title);
|
||||||
primaryStage.setWidth(width);
|
primaryStage.setWidth(width);
|
||||||
primaryStage.setHeight(length);
|
primaryStage.setHeight(length);
|
||||||
primaryStage.setScene(scene);
|
primaryStage.setScene(scene);
|
||||||
|
|||||||
@ -9,4 +9,8 @@ public class Constants {
|
|||||||
public static final double SCREEN_LENGTH = 600; //高度
|
public static final double SCREEN_LENGTH = 600; //高度
|
||||||
public static final String APP_NAME = "JNotepad"; //名字
|
public static final String APP_NAME = "JNotepad"; //名字
|
||||||
public static final String APP_ICON = "/img/icon.png"; //logo地址
|
public static final String APP_ICON = "/img/icon.png"; //logo地址
|
||||||
|
|
||||||
|
//配置文件
|
||||||
|
public static final String PROPERTY_FILE_NAME = "project.txt"; //配置文件名字
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
40
src/main/java/org/jcnc/jnotepad/init/Config.java
Normal file
40
src/main/java/org/jcnc/jnotepad/init/Config.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user