我尝试使用此方法打印显示带有标签“THAT'S IT”的弹出窗口,我不想使用Popup类
public void showStage(Stage Owner){
HBox hBox = new HBox();
hBox.getChildren().add(new Label("THAT'S IT"));
Scene sc = new Scene(hBox);
Stage popup = new Stage();
popup.setScene(sc);
popup.setWidth(400);
popup.setHeight(100);
popup.initOwner(owner);
popup.initModality(Modality.WINDOW_MODAL);
popup.show();
}
然后我从start方法
调用showStage()
方法
public void start(Stage primaryStage) {
Label lb = new Label();
btn.setText("Say 'Hello World'");
btn.setOnAction(e->{
lb.setText("hello everyone");showStage(primaryStage);
});
但是代码的输出:
答案 0 :(得分:0)
为什么不使用对话框? 您可以在主控制器类中使用它,而无需创建其他阶段,例如:
Dialog dialogQtPrescription = new Dialog();
dialogQtPrescription.setTitle("yourTitle");
dialogQtPrescription.setHeaderText("yourHeadertext");
dialogQtPrescription.initModality(Modality.WINDOW_MODAL);
dialogQtPrescription.initOwner(mainStage);
dialogQtPrescription.initStyle(StageStyle.UTILITY);
GridPane gridDialogPrescription = new GridPane();
gridDialogPrescription.setHgap(10);
gridDialogPrescription.setVgap(10);
gridDialogPrescription.add(new Label(bundle.getString("quantityPrescription.title")), 0, 0);
TextField txtQtPrescr = new TextField();
ButtonType buttonTypeNo = new ButtonType("no");
ButtonType buttonTypeYes = new ButtonType("yes");
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
dialogQtPrescription.getDialogPane().getButtonTypes().addAll(buttonTypeNo,buttonTypeYes, buttonTypeCancel);
txtQtPrescr.setPrefWidth(100);
gridDialogPrescription.add(txtQtPrescr, 1, 0);
dialogQtPrescription.getDialogPane().setContent(gridDialogPrescription);
Optional<ButtonType> result = dialogQtPrescription.showAndWait();
这只是项目中的一堆代码,但我希望它能让你理解我的想法。
这里有更好的解释:http://code.makery.ch/blog/javafx-dialogs-official/
答案 1 :(得分:0)
您可以使用它来在JavaFX的任何屏幕上制作弹出窗口
public void popup() {
final Stage dialog = new Stage();
dialog.setTitle("Confirmation");
Button yes = new Button("Yes");
Button no = new Button("No");
Label displayLabel = new Label("What do you want to do ?");
displayLabel.setFont(Font.font(null, FontWeight.BOLD, 14));
dialog.initModality(Modality.NONE);
dialog.initOwner((Stage) tableview.getScene().getWindow());
HBox dialogHbox = new HBox(20);
dialogHbox.setAlignment(Pos.CENTER);
VBox dialogVbox1 = new VBox(20);
dialogVbox1.setAlignment(Pos.CENTER_LEFT);
VBox dialogVbox2 = new VBox(20);
dialogVbox2.setAlignment(Pos.CENTER_RIGHT);
dialogHbox.getChildren().add(displayLabel);
dialogVbox1.getChildren().add(yes);
dialogVbox2.getChildren().add(no);
yes.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
// inside here you can use the minimize or close the previous stage//
dialog.close();
}
});
no.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
dialog.close();
}
});
dialogHbox.getChildren().addAll(dialogVbox1, dialogVbox2);
Scene dialogScene = new Scene(dialogHbox, 500, 40);
dialogScene.getStylesheets().add("//style sheet of your choice");
dialog.setScene(dialogScene);
dialog.show();
}