JAVAFX-8取消操作使用JAVAFX中controlfx的对话框关闭舞台?
我正在JavaFX应用程序中使用ControlFx中的对话框。但单击“取消”按钮,它将关闭应用程序JAVAFX-8取消操作使用JAVAFX中controlfx的对话框关闭舞台?,java,user-interface,javafx-8,controlsfx,Java,User Interface,Javafx 8,Controlsfx,我正在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.co
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);
}
}
封装测试;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.event;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;
导入org.controlsfx.control.action.action;
导入org.controlsfx.dialog.dialog;
导入org.controlsfx.dialog.Dialogs;
公共类NewFXMain扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
按钮btn=新按钮();
btn.setText(“说‘你好,世界’”);
btn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
System.out.println(“你好,世界!”);
}
});
StackPane root=新的StackPane();
root.getChildren().add(btn);
场景=新场景(根,300,250);
setTitle(“你好,世界!”);
初级阶段。场景(场景);
primaryStage.show();
setOnCloseRequest(新的EventHandler()){
公共无效句柄(事件t){
操作响应=对话框。创建()
.owner(新阶段())
.标题(“退出”)
.masthead(“是否要退出?”)
.actions(Dialog.actions.OK,Dialog.actions.CANCEL)
.showConfirm();
if(response==Dialog.Actions.OK){
primaryStage.hide();
系统出口(0);
//…用户选择“确定”
}else if(response==Dialog.Actions.CANCEL){
}
}
});
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
发射(args);
}
}
我是以错误的方式实现的还是conrolfx中的一个bug?尝试警告对话框也会发生同样的情况。我还尝试了其他带有YES、NO和cancel操作的对话框。我正在ubuntu 14.04上使用netbeans 8.0和jdk 8 好的,我是通过互联网搜索得到的,只需消费我的活动,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这怎么能不让你陷入一个无限的关闭请求循环?如果说(response!=Dialog.Actions.OK)t.consume()else System.exit(0),不是更好吗;(当然,我对JavaFX的了解还很有限,很可能是错的)。