diff --git a/src/main/java/org/jcnc/jnotepad/interfaces/CustomDialogAble.java b/src/main/java/org/jcnc/jnotepad/interfaces/CustomDialogAble.java new file mode 100644 index 0000000..7965135 --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/interfaces/CustomDialogAble.java @@ -0,0 +1,38 @@ +package org.jcnc.jnotepad.interfaces; + +import javafx.scene.image.Image; + +/** + * 自定义提示框的接口。 + * + *

此接口定义了自定义提示框的基本行为,包括设置图标、处理确认和取消按钮操作以及获取提示框类型。

+ * + * @author luke + */ +public interface CustomDialogAble { + + /** + * 设置提示框的图标。 + * + * @param iconImage 图标图像 + */ + void setIconImage(Image iconImage); + + /** + * 处理确认按钮的操作。子类必须实现此方法以定义确认按钮的行为。 + */ + void handleConfirmAction(); + + /** + * 获取提示框类型。 + * + * @return 提示框类型的描述 + */ + String getAlertType(); + + /** + * 处理取消按钮的操作。默认情况下,此方法为空,子类可以选择性地实现它。 + */ + default void handleCancelAction() { + } +} diff --git a/src/main/java/org/jcnc/jnotepad/ui/module/alert/AbstractDialog.java b/src/main/java/org/jcnc/jnotepad/ui/module/alert/AbstractDialog.java new file mode 100644 index 0000000..5a8a67a --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/ui/module/alert/AbstractDialog.java @@ -0,0 +1,139 @@ +package org.jcnc.jnotepad.ui.module.alert; + +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; +import javafx.stage.Modality; +import javafx.stage.Stage; +import org.jcnc.jnotepad.interfaces.CustomDialogAble; + +/** + * 自定义提示框的抽象基类 + * + *

该类用于创建自定义的提示框窗口,包括图标、消息文本和确认、取消按钮。

+ * + * @author luke + */ +public abstract class AbstractDialog extends Stage implements CustomDialogAble { + + private ImageView iconImageView; + private final String customText; + + /** + * 构造一个自定义提示框。 + * + * @param message 提示框中显示的消息文本 + * @param customText 自定义的文本 + * @param width 提示框的宽度 + * @param height 提示框的高度 + */ + protected AbstractDialog(String message, String customText, double width, double height) { + this.customText = customText; + setTitle(getAlertType()); + setResizable(false); + initModality(Modality.APPLICATION_MODAL); + + BorderPane borderPane = createLayout(message); + Scene scene = new Scene(borderPane, width, height); + + setScene(scene); + } + + /** + * 处理取消按钮的操作。 + */ + @Override + public void handleCancelAction() { + close(); + } + + /** + * 创建提示框的布局。 + * + * @param message 提示框中显示的消息文本 + * @return BorderPane 布局容器 + */ + private BorderPane createLayout(String message) { + BorderPane borderPane = new BorderPane(); + iconImageView = new ImageView(); + + HBox iconBox = new HBox(iconImageView); + iconBox.setPadding(new Insets(10, 10, 10, 10)); + VBox vbox = new VBox(10); + vbox.setAlignment(Pos.CENTER); + + Label label = new Label(message); + + // 自定义文本 + Label customTextLabel = new Label(customText); + + Button confirmButton = createButton("确认", this::handleConfirmAction); + Button cancelButton = createButton("取消", this::handleCancelAction); + + HBox hBox = new HBox(10, confirmButton, cancelButton); + hBox.setAlignment(Pos.CENTER_RIGHT); + hBox.setPadding(new Insets(10, 10, 10, 10)); + vbox.getChildren().addAll(label, customTextLabel, hBox); + + borderPane.setLeft(iconBox); + borderPane.setCenter(vbox); + borderPane.setBottom(hBox); + + return borderPane; + } + + /** + * 设置提示框的图标。 + * + * @param iconImage 图标图像 + */ + @Override + public void setIconImage(Image iconImage) { + iconImageView.setImage(iconImage); + } + + /** + * 创建按钮。 + * + * @param text 按钮文本 + * @param action 按钮点击时的操作 + * @return Button 按钮控件 + */ + private Button createButton(String text, Runnable action) { + Button button = new Button(text); + button.setOnAction(e -> action.run()); + return button; + } + + /** + * 获取提示框类型的抽象方法,子类应该实现该方法以返回具体的提示框类型。 + * + * @return 提示框类型 + */ + @Override + public abstract String getAlertType(); + + /** + * 处理确认按钮的操作,子类必须实现。 + */ + @Override + public abstract void handleConfirmAction(); + + /** + * 构造一个自定义提示框,使用默认大小。 + * + * @param message 提示框中显示的消息文本 + * @param customText 自定义的文本 + */ + protected AbstractDialog(String message, String customText) { + // 使用默认的宽度和高度 + this(message, customText, 350, 150); + } +} diff --git a/src/main/java/org/jcnc/jnotepad/ui/module/alert/ErrorDialogAble.java b/src/main/java/org/jcnc/jnotepad/ui/module/alert/ErrorDialogAble.java new file mode 100644 index 0000000..49485fb --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/ui/module/alert/ErrorDialogAble.java @@ -0,0 +1,50 @@ +package org.jcnc.jnotepad.ui.module.alert; + +/** + * 错误对话框 + * + *

用于显示错误信息的自定义对话框,包含图标、消息文本和确认按钮。

+ * + * @author luke + */ +class ErrorDialogAble extends AbstractDialog { + + /** + * 构造一个错误对话框。 + * + * @param title 对话框中显示的错误消息 + * @param customText 自定义文本 + * @param width 对话框的宽度 + * @param height 对话框的高度 + */ + protected ErrorDialogAble(String title, String customText, double width, double height) { + super(title, customText, width, height); + } + + /** + * 构造一个错误对话框,使用默认大小。 + * + * @param title 对话框中显示的错误消息 + * @param customText 自定义文本 + */ + public ErrorDialogAble(String title, String customText) { + super(title, customText); + } + + /** + * 获取对话框类型。 + * + * @return 对话框类型为"错误提示" + */ + @Override + public String getAlertType() { + return "错误提示"; + } + + /** + * 处理确认按钮的操作,子类可以在这里添加具体的处理逻辑。 + */ + @Override + public void handleConfirmAction() { + } +} diff --git a/src/main/java/org/jcnc/jnotepad/ui/module/alert/InformationDialogAble.java b/src/main/java/org/jcnc/jnotepad/ui/module/alert/InformationDialogAble.java new file mode 100644 index 0000000..13db3aa --- /dev/null +++ b/src/main/java/org/jcnc/jnotepad/ui/module/alert/InformationDialogAble.java @@ -0,0 +1,48 @@ +package org.jcnc.jnotepad.ui.module.alert; + +/** + * 信息提示对话框类,继承自AbstractCustomAlert,用于显示信息提示。 + * + * @author luke + */ +public class InformationDialogAble extends AbstractDialog { + + /** + * 构造一个信息提示对话框。 + * + * @param title 提示信息 + * @param customText 自定义文本 + * @param width 对话框宽度 + * @param height 对话框高度 + */ + protected InformationDialogAble(String title, String customText, double width, double height) { + super(title, customText, width, height); + } + + /** + * 构造一个信息提示对话框。 + * + * @param title 提示信息 + * @param number 编号 + */ + public InformationDialogAble(String title, String number) { + super(title, number); + } + + /** + * 获取对话框类型。 + * + * @return 对话框类型字符串,此处为"信息提示" + */ + @Override + public String getAlertType() { + return "信息提示"; + } + + /** + * 处理确认操作的方法。 + */ + @Override + public void handleConfirmAction() { + } +}