JavaFX 控件 Dialog

Dialog

Dialog

Dialog<ButtonType> dialog = new Dialog<>();
dialog.setTitle("Title");
dialog.setHeaderText("HeaderText");
dialog.setContentText("ContentText");
ImageView imageView = new ImageView("image\\portrait.jpg");
imageView.setFitWidth(50);
imageView.setPreserveRatio(true);
dialog.setGraphic(imageView);

DialogPane dialogPane = dialog.getDialogPane();
dialogPane.setPrefSize(500, 300);

ObservableList<ButtonType> buttonTypes = dialogPane.getButtonTypes();
buttonTypes.addAll(ButtonType.OK, ButtonType.APPLY, ButtonType.CANCEL);
Button btnOk = (Button) dialogPane.lookupButton(ButtonType.OK);
Button btnApply = (Button) dialogPane.lookupButton(ButtonType.APPLY);
Button btnCancel = (Button) dialogPane.lookupButton(ButtonType.CANCEL);

btnOk.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("OK");
    }
});
btnApply.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("Apply");
    }
});


btnCancel.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("Cancel");
    }
});

dialog.setOnCloseRequest(new EventHandler<DialogEvent>() {
    @Override
    public void handle(DialogEvent event) {
        System.out.println("OnCloseRequest");
    }
});

/*
dialog.showAndWait()
        .filter(response -> response == ButtonType.OK)
        .ifPresent(response -> System.out.println("doAction"));
        */

dialog.showAndWait().filter(new Predicate<ButtonType>() {
    @Override
    public boolean test(ButtonType buttonType) {
        return buttonType == ButtonType.OK;
    }
}).ifPresent(new Consumer<ButtonType>() {
    @Override
    public void accept(ButtonType buttonType) {
        System.out.println("doAction");
    }
});

System.out.println("阻塞的代碼");

dialog.show();
System.out.println("不會阻塞的代碼");

//如果 Dialog<> 泛型不是ButtonType 需要設置 ResultConverter
//dialog.setResultConverter(new Callback<ButtonType, ButtonType>() {
//    @Override
//    public String call(ButtonType param) {
//        return "";
//    }
//});

設置左上角小圖標

//設置左上角小圖標
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("image/stash.png"));

//使用與它正在運行的應用程序相同的圖標

dialog.initOwner(primaryStage);

dialog.initStyle(StageStyle.UTILITY);

Alert

Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Title");
alert.setHeaderText("HeaderText");
alert.setContentText("ContentText");
Button btnOk = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("Ok");
    }
});
alert.show();

ChoiceDialog

ObservableList<String> itemStrings = FXCollections.observableArrayList();
for (int i = 0; i < 6; i++) {
    itemStrings.add("item " + i);
}
ChoiceDialog<String> choiceDialog = new ChoiceDialog<>();
choiceDialog.setSelectedItem("item 3");
choiceDialog.getItems().addAll(itemStrings);

Button btnOk = (Button) choiceDialog.getDialogPane().lookupButton(ButtonType.OK);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("Ok");
    }
});
choiceDialog.selectedItemProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
    }
});

TextInputDialog

TextInputDialog textInputDialog = new TextInputDialog();
textInputDialog.setTitle("Title");
textInputDialog.setHeaderText("HeaderText");
textInputDialog.setContentText("ContentText");
Button btnOk = (Button) textInputDialog.getDialogPane().lookupButton(ButtonType.OK);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        //輸入框中的文本
        String text = textInputDialog.getEditor().getText();
        System.out.println(text);
    }
});
//設置左上角小圖標
Stage stage = (Stage) textInputDialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("image/stash.png"));
textInputDialog.show();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章