猿问

如何为 JavaFX 异常对话框的按钮设置动作事件

我现在正在通过 JavaFX 做一个简单的数独游戏。现在我在对话上遇到了一些困难。我创建了两个场景,菜单场景只包含“新游戏”和“继续”按钮,主场景包含数独游戏。在主场景中,我创建了一个检查按钮来检查玩家的答案是否正确,如果不正确,则在此处显示像此img这样的对话框,如果正确,则可能会在此处显示此img。


然后我发现CONFIRMATION ALERT非常相似。我需要更改的是按钮的文本和它的动作,而当单击重试返回游戏场景并单击退出返回主场景时。


现在我知道如何为警报框中的按钮设置操作,但是我有一些新问题,我不知道如何ventHandler<ActionEvent>在语句中调用。


这是我的两个警报框代码


(源代码来自https://code.makery.ch/blog/javafx-dialogs-official/)


Alert right = new Alert(AlertType.CONFIRMATION);

right.setTitle("Checking Result");

right.setHeaderText(null);

right.setContentText("Your answer is correct. Would you like to start

again");

ButtonType restart = new ButtonType("Restart");

ButtonType quit = new ButtonType("Quit");

right.getButtonTypes().setAll(restart, quit);


Alert wrong = new Alert(AlertType.CONFIRMATION);

wrong.setTitle("Checking Result");

wrong.setHeaderText(null);

wrong.setContentText("Your answer is incorrect. Would you like to try 

again");

ButtonType retry = new ButtonType("Retry");

wrong.getButtonTypes().setAll(retry, quit);

动作代码


Optional<ButtonType> result = right.showAndWait();

if (result.isPresent() && result.get() == quit) {

stage.setScene(main_frame);

}else if(result.isPresent() && result.get() == 

restart) {// call the actionevent clears}


Optional<ButtonType> result = wrong.showAndWait();

if (result.isPresent() && result.get() == quit) {

stage.setScene(main_frame);

}else if(result.isPresent() && result.get() == 

retry) {// call the actionevent clears}

事件处理程序的代码


final EventHandler<ActionEvent> clears = new EventHandler<ActionEvent>() {

            @Override

            public void handle(final ActionEvent event) {

                for (int i = 0; i < 9; i++) {

                    for (int j = 0; j < 9; j++) {

                        if (digging_array[i][j] == 1) {

                            sudoku[i][j].setText(Integer.toString(final_Array[i][j]));

                        } else {

                            sudoku[i][j].setText("");

                        }

                    }

                }

            }

        };


BIG阳
浏览 177回答 2
2回答

噜噜哒

您确实更改了有关right警报的按钮类型。您的最后一行不会更改wrong警报按钮。替换right为wrong将针对正确的警报,从而更改其按钮。可以通过多种方式检查按下了哪个按钮。摘自官方文档(https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.html):选项 1:“传统”方法Optional<ButtonType> result = alert.showAndWait();if (result.isPresent() && result.get() == ButtonType.OK) {&nbsp; &nbsp; formatSystem();}选项 2:传统 + Optional 方法alert.showAndWait().ifPresent(response -> {&nbsp; &nbsp; if (response == ButtonType.OK) {&nbsp; &nbsp; &nbsp; &nbsp; formatSystem();&nbsp; &nbsp; }});选项 3:完全 lambda 方法alert.showAndWait()&nbsp; &nbsp; &nbsp;.filter(response -> response == ButtonType.OK)&nbsp; &nbsp; &nbsp;.ifPresent(response -> formatSystem());而不是使用ButtonType.OK您需要使用您的自定义按钮。编辑在您的示例中,您必须像这样修改代码:void clear() {&nbsp; &nbsp; for (int i = 0; i < 9; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 9; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (digging_array[i][j] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sudoku[i][j].setText(Integer.toString(final_Array[i][j]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sudoku[i][j].setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}Optional<ButtonType> result = right.showAndWait();if (result.isPresent() && result.get() == quit) {&nbsp; &nbsp; stage.setScene(main_frame);} else if(result.isPresent() && result.get() == restart) {&nbsp; &nbsp; clear()}Optional<ButtonType> result = wrong.showAndWait();if (result.isPresent() && result.get() == quit) {&nbsp; &nbsp; stage.setScene(main_frame);} else if(result.isPresent() && result.get() == retry) {&nbsp; &nbsp; clear()}

回首忆惘然

在链接的教程中,有一个关于如何设置自定义操作的示例(我将其缩短了一点):Alert alert = new Alert(AlertType.CONFIRMATION);alert.setTitle("Confirmation Dialog with Custom Actions");ButtonType buttonTypeOne = new ButtonType("One");ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);Optional<ButtonType> result = alert.showAndWait();if (result.get() == buttonTypeOne){&nbsp; &nbsp; // ... user chose "One"} else {&nbsp; &nbsp; // ... user chose CANCEL or closed the dialog}您可以通过result.get()并检查按下了哪个按钮(buttonTypeOne,buttonTypeCancel,...)来获取结果(用户单击的内容)。当用户按下“One”时,您现在可以在 if 语句的第一个主体中执行某些操作。在您的代码中,您错过了showAndWait()电话。例如,如果用户是对的,你应该这样做:Observable<ButtonType> rightResult = right.showAndWait();if (rightResult.isPresent()) {&nbsp; &nbsp; if (rightResult.get() == restart) { //because "restart" is the variable name for your custom button type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// some action, method call, ...&nbsp; &nbsp; } else { // In this case "quit"&nbsp; &nbsp; }}请注意,这可能不是最优雅的方式(双重 if 语句)。@Others 可以随意编辑我的答案并采用更好的方法来做到这一点。
随时随地看视频慕课网APP

相关分类

Java
我要回答