将 JFoenix JFXDialogLayout 警报通知代码重构为更简单的形式,以便它可以重新用于其他 类

Refactor JFoenix JFXDialogLayout alert notification code to a simpler form so that it can be reused for other classes

如何重构以下代码,以便只有 deleteButton.setOnAction(deleteEvent -> {//only this code varies} 中的代码发生变化。其他一切都将保持不变,但当我从另一个 class 调用 class 时,lambda 表达式中的代码块会不时变化。通过 lambda 表达式的代码块应该是一个 void 方法。

public class A {    

    public void test() {
        // ensure that user can't close the alert
        Stage primaryStage = (Stage) RootLayoutController.getRootLayout().getScene().getWindow();
        JFXAlert<javafx.scene.control.ButtonType> alert = new JFXAlert<>(primaryStage);
        alert.initModality(Modality.APPLICATION_MODAL);
        alert.setOverlayClose(false);

        //create font awesome icon
        String ICON = "\uf071";
        Label labelIcon = new Label(ICON); 
        labelIcon.setStyle("-fx-font-family: 'FontAwesome'; -fx-font-size: 60px; -fx-text-fill: #D34336;");
        labelIcon.setPadding(new Insets(0,5,0,0));

        // Create the content of the JFXAlert with JFXDialogLayout
        JFXDialogLayout layout = new JFXDialogLayout();
        Label labelHeading = new Label("Alert Notification");
        Label labelBody = new Label("Are you sure you want to delete this?");
        layout.setHeading(labelHeading);
        layout.setBody(new VBox(new HBox(labelIcon, labelBody)));

        // Buttons get added into the actions section of the layout.
        JFXButton deleteButton = new JFXButton("Delete");
        deleteButton.setDefaultButton(true);
        deleteButton.setOnAction(deleteEvent -> {
            //only this block of code changes

            alert.hideWithAnimation();
        });

        JFXButton cancelButton = new JFXButton("Cancel");
        cancelButton.setCancelButton(true);
        cancelButton.setOnAction(closeEvent -> alert.hideWithAnimation());

        layout.setActions(deleteButton, cancelButton);
        alert.setContent(layout); 
        alert.showAndWait();      
    }
}

从你的问题中不能完全清楚你想要完成什么,但我会大胆尝试一下。

如果您希望能够将代码块传递给 deleteButton.setOnAction() 方法,您可以使用 Interface 并将该接口的实现传递给 A class。然后将该引用传递给 onAction lambda 的内部方法。

这是一个非常简单的示例,说明如何执行此操作:

Main.java:

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Action button
        Button btnDoSomething = new Button("Do something...");
        btnDoSomething.setOnAction(e -> doTheThings(new ImplDoSomething()));

        Button btnDoSomethingElse = new Button("Do something else...");
        btnDoSomethingElse.setOnAction(e -> doTheThings(new ImplDoSomethingElse()));

        VBox mainPane = new VBox(5);
        mainPane.setAlignment(Pos.CENTER);
        mainPane.setPadding(new Insets(10));
        mainPane.getChildren().addAll(btnDoSomething, btnDoSomethingElse);

        primaryStage.setScene(new Scene(mainPane));

        primaryStage.show();

    }

    private void doTheThings(IParameterMethod parameterMethod) {

        parameterMethod.call();

    }
}

IParameterMethod.java接口:

public interface IParameterMethod {

    void call();

}

然后你可以创建尽可能多的 classes 来实现那个接口,每个都有自己的 call() 方法,允许你执行不同的代码。

ImplDoSomething.java

public class ImplDoSomething implements IParameterMethod {

    @Override
    public void call() {
        System.out.println("Doing something!");
    }
}

ImplDoSomethingElse.java:

public class ImplDoSomethingElse implements IParameterMethod {

    @Override
    public void call() {
        System.out.println("Doing something else!");
    }
}

这应该很容易适应您的项目。