2017-01-27 4 views
0

Webを介して私は解決策を見出しませんでした。下の画像は、私が達成したいものを説明しますJavaFXノードの中央Java警告[StackPaneまたはボタンなど]

enter image description here


一つの可能​​な解決策は、それが修正を必要とする複数の画面のために、一つの画面で[コンピュータのために作成されました。それは、[1画面分]動作しますが、私は....これはそれを行うための正しい方法であるかわからない

質問: は、JavaFXのがでビルドしていない可能性があることを正しい方向ですか? Nodesのような機能とそれはStageのためだけにありますか?

コード:

/** 
    * Makes a question to the user. 
    * 
    * @param text 
    *   the text 
    * @param node 
    *   The node owner of the Alert 
    * @return true, if successful 
    */ 
    public static boolean doQuestion(String text, Node node) { 
    questionAnswer = false; 

    // Show Alert 
    Alert alert = new Alert(AlertType.CONFIRMATION); 
    alert.initStyle(StageStyle.UTILITY); 
    alert.setHeaderText(""); 
    alert.setContentText(text); 

    // Make sure that JavaFX doesn't cut the text with ... 
    alert.getDialogPane().getChildren().stream().filter(item -> node instanceof Label) 
     .forEach(item -> ((Label) node).setMinHeight(Region.USE_PREF_SIZE)); 

    // I noticed that height property is notified after width property 
    // that's why i choose to add the listener here 
    alert.heightProperty().addListener(l -> { 

     // Width and Height of the Alert 
     int alertWidth = (int) alert.getWidth(); 
     int alertHeight = (int) alert.getHeight(); 

     // Here it prints 0!! 
     System.out.println("Alert Width: " + alertWidth + " , Alert Height: " + alertHeight); 

     // Find the bounds of the node 
     Bounds bounds = node.localToScreen(node.getBoundsInLocal()); 
     int x = (int) (bounds.getMinX() + bounds.getWidth()/2 - alertWidth/2); 
     int y = (int) (bounds.getMinY() + bounds.getHeight()/2 - alertHeight/2); 

     // Check if Alert goes out of the Screen on X Axis 
     if (x + alertWidth > InfoTool.getVisualScreenWidth()) 
     x = (int) (getVisualScreenWidth() - alertWidth); 
     else if (x < 0) 
     x = 0; 

     // Check if Alert goes out of the Screen on Y AXIS 
     if (y + alertHeight > InfoTool.getVisualScreenHeight()) 
     y = (int) (getVisualScreenHeight() - alertHeight); 
     else if (y < 0) 
     y = 0; 

     // Set the X and Y of the Alert 
     alert.setX(x); 
     alert.setY(y); 
    }); 
    return questionAnswer; 
} 

上から欠落している二つの方法:

/** 
    * Gets the visual screen width. 
    * 
    * @return The screen <b>Width</b> based on the <b>visual bounds</b> of the 
    *   Screen.These bounds account for objects in the native windowing 
    *   system such as task bars and menu bars. These bounds are 
    *   contained by Screen.bounds. 
    */ 
    public static double getVisualScreenWidth() { 
    return Screen.getPrimary().getVisualBounds().getWidth(); 
    } 

    /** 
    * Gets the visual screen height. 
    * 
    * @return The screen <b>Height</b> based on the <b>visual bounds</b> of the 
    *   Screen.These bounds account for objects in the native windowing 
    *   system such as task bars and menu bars. These bounds are 
    *   contained by Screen.bounds. 
    */ 
    public static double getVisualScreenHeight() { 
    return Screen.getPrimary().getVisualBounds().getHeight(); 
    } 

答えて

1

は、現在の画面でJavaFX CenteredAlert

この実装センターのアラートのこの実装を見てみましょう、別のセンタリングロジックを実装する必要がある場合は、メソッドを編集することができますcenterAlertInStage()

+1

ステージだけでなく、一般的なノードでも動作させるようにしてください。あなたの試しに+1。 – GOXR3PLUS

+1

ありがとうございました@ GOXR3PLUS、今はノード与えられた動作します。ダイアログを表示する際に点滅するのを修正しました。 – cdr89

+1

私はそれを試してみる:) – GOXR3PLUS

関連する問題