【问题标题】:JAVAFX-8 CANCEL ACTION Close the stage, using Dialog from controlfx in Javafx?JAVAFX-8 CANCEL ACTION 关闭舞台,在 Javafx 中使用来自 controlfx 的对话框?
【发布时间】:2014-07-22 14:27:49
【问题描述】:

我在我的 JavaFX 应用程序中使用来自 ControlFx 的对话框。但是在单击取消按钮时它会关闭应用程序。

package testing;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.controlsfx.control.action.Action;
import org.controlsfx.dialog.Dialog;
import org.controlsfx.dialog.Dialogs;

public class NewFXMain extends Application {

@Override
public void start(Stage primaryStage) {
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
    primaryStage.setOnCloseRequest(new EventHandler() {
        public void handle(Event t) {
            Action response = Dialogs.create()
                    .owner(new Stage())
                    .title("Exit ??")
                    .masthead("Do you want to Exit ??")
                    .actions(Dialog.Actions.OK, Dialog.Actions.CANCEL)
                    .showConfirm();

            if (response == Dialog.Actions.OK) {
                primaryStage.hide();
                System.exit(0);
// ... user chose OK
            } else if (response == Dialog.Actions.CANCEL){

            }

        }
    });

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

是我以错误的方式实现它还是 conrolfx 中的错误?尝试过的警告对话框也同样发生在那里。我还尝试了其他带有“是”、“否”和“取消”操作的对话框。我在 ubuntu 14.04 上使用带有 jdk 8 的 netbeans 8.0。

【问题讨论】:

    标签: java user-interface javafx-8 controlsfx


    【解决方案1】:

    好的,我通过互联网搜索得到了这个,只需要消费我的事件,primarystage 不会退出。

    primaryStage.setOnCloseRequest(new EventHandler() {
        public void handle(Event t) {
            t.consume();
            Action response = Dialogs.create()
                    .owner(new Stage())
                    .title("Exit ??")
                    .masthead("Do you want to Exit ??")
                    .actions(Dialog.Actions.OK, Dialog.Actions.CANCEL)
                    .showConfirm();
    
            if (response == Dialog.Actions.OK) {
                primaryStage.close();
                System.exit(0);
    
            } else if (response == Dialog.Actions.CANCEL){
    
            }
    
        }
    });
    

    看起来像一个非常小而愚蠢的错误。 :P

    【讨论】:

    • 如何不让您陷入无限循环的关闭请求?说 if(response != Dialog.Actions.OK) t.consume() else System.exit(0); 不是更好吗? (当然,我对 JavaFX 的了解仍然非常有限,我很可能是错的)。
    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2013-05-14
    • 2018-09-06
    • 1970-01-01
    相关资源
    最近更新 更多