在我的JavaFX应用程序中,我正在使用来自JavaFX的对话框。但是在单击Cancel按钮时,它会关闭应用程序。
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中是一个bug?尝试过的警告对话框也同样发生在那里。我也尝试了其他对话与是,否和取消行动。我在ubuntu14.04上使用netbeans 8.0和jdk 8。
发布于 2014-07-24 10:31:23
好的,我通过互联网搜索得到了这个,只需要消耗我的事件,初选阶段就不会退出。
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
https://stackoverflow.com/questions/24890105
复制相似问题