有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使弹出窗口居中?

所以我使用jfoenix JFXPopup组件作为我的弹出窗口。最终得到弹出窗口显示后。我意识到弹出窗口是相对于您传递的所有者节点显示的。我的问题是,有没有办法显示与场景相关的弹出窗口,以便将其居中

不幸的是,您不能将弹出窗口置于所有者节点的中心,您只能将其上下或左右移动

Popup relative to the search textfield in the middle of the application

//Current Code
@FXML
    protected void handleLoginButton(ActionEvent event) {
        System.out.println("Popup BUtton clicked");
        if (!loginPopup.isShowing()) {
            System.out.println("Popup is open!");
            //loginPopup.show(searchBtn, JFXPopup.PopupVPosition.BOTTOM, JFXPopup.PopupHPosition.LEFT);
            loginPopup.show(searchBtn);
        }
    }

共 (1) 个答案

  1. # 1 楼答案

    调用show时使用场景的根窗格,并计算中心位置

    popupRoot.setPrefWidth(200);
    popupRoot.setPrefHeight(300);
    button.setOnAction(event -> {
        JFXPopup loginPopup = new JFXPopup(popupRoot);
        Bounds rootBounds = scene.getRoot().getLayoutBounds();
        loginPopup.show(scene.getRoot(), JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT,
                (rootBounds.getWidth() - popupRoot.getPrefWidth()) / 2,
                (rootBounds.getHeight() - popupRoot.getPrefHeight()) / 2);
    });
    

    请参见here以从控制器访问场景