2016-10-25 22 views
-1

我試着用這個方法來打印顯示具有標籤「就是它」,我不想使用彈出類如何顯示彈出,而我在初級階段

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(); 
    } 
一個彈出

,然後我打電話從一開始的方法

 public void start(Stage primaryStage) { 

    Label lb = new Label(); 

    btn.setText("Say 'Hello World'"); 
    btn.setOnAction(e->{ 
     lb.setText("hello everyone");showStage(primaryStage); 
    }); 

showStage()方法,但該代碼的輸出:

enter image description here

+1

('階段Owner' - >'舞臺owner' ),我無法重現此問題。由於答案中的信息不足以重現,或者它是一個簡單的錯字,因此投票表示接近主題。 – fabian

回答

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/

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(); 
    }