JavaFX节点上的中心Java警报[例如StackPane或Button]

时间:2017-01-27 05:25:31

标签: java javafx alert

通过网络浏览我没有找到任何解决方案。下图说明了我想要实现的目标:

enter image description here

为具有一个屏幕的计算机创建了一个可能的解决方案,对于需要修改的多个屏幕。虽然它适用于[一个屏幕],但我不确定这是否是正确的方法....

问题: 这是正确的方向吗?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 个答案:

答案 0 :(得分:1)

看一下JavaFX CenteredAlert

的实现

此实现将警报置于当前屏幕中,如果需要实现不同的居中逻辑,可以编辑方法centerAlertInStage()