0

如何重构以下代码,以便仅deleteButton.setOnAction(deleteEvent -> {//only this code varies}更改代码。其他一切都将保持不变,但当我从另一个类调用该类时,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();      
    }
}
4

1 回答 1

1

从您的问题中尚不完全清楚您要完成的工作,但我会猛烈抨击它。

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

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

主.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();

}

然后,您可以创建任意数量的类来实现该接口,每个类都有自己的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!");
    }
}

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

于 2018-06-03T04:09:45.907 回答