猿问

防止对话框离开屏幕

我有一个很好的primaryStage。我有一个以primaryStage为中心打开的对话框(我通过将对话框的所有者设为primaryStage来实现这个功能


dialog.setInitOwner(primaryStage).  

但是,如果 primaryStage 已经靠近屏幕边缘,则对话框将离开屏幕。如果所有者靠近屏幕边缘,如何使对话框(所有者为 primaryStage)不离开屏幕?


我试过这个:


    double x = dialog.getX();

    double y = dialog.getY();

    double w = dialog.getWidth();

    double h = dialog.getHeight();

    if (x < Main.bounds.getMinX())

    {

        dialog.setX(Main.bounds.getMinX());

    }

    if (x + w > Main.bounds.getMaxX())

    {

        dialog.setX(Main.bounds.getMaxX() - w);

    }

    if (y < Main.bounds.getMinY())

    {

        dialog.setY(Main.bounds.getMinY());

    }

    if (y + h > Main.bounds.getMaxY())

    {

        dialog.setY(Main.bounds.getMaxY() - h);

    }


    dialog.showAndWait();

Main.bounds 由以下人员创建:


private static Bounds computeAllScreenBounds()

{

    double minX = Double.POSITIVE_INFINITY;

    double minY = Double.POSITIVE_INFINITY;

    double maxX = Double.NEGATIVE_INFINITY;

    double maxY = Double.NEGATIVE_INFINITY;

    for (Screen screen : Screen.getScreens())

    {

        Rectangle2D screenBounds = screen.getVisualBounds();

        if (screenBounds.getMinX() < minX)

        {

            minX = screenBounds.getMinX();

        }

        if (screenBounds.getMinY() < minY)

        {

            minY = screenBounds.getMinY();

        }

        if (screenBounds.getMaxX() > maxX)

        {

            maxX = screenBounds.getMaxX();

        }

        if (screenBounds.getMaxY() > maxY)

        {

            maxY = screenBounds.getMaxY();

        }

    }

    return new BoundingBox(minX, minY, maxX - minX, maxY - minY);

尝试这个,不会改变任何行为。我觉得这是因为 dialog.getX() 函数返回一个 NaN ... }


眼眸繁星
浏览 227回答 2
2回答

收到一只叮咚

使用 showAndWait 时警报上的 onShown 处理程序的(对我来说出乎意料的)问题是,在调用处理程序时,警报尚未显示且尚未确定大小/位置 - 这有点疯狂,可能被视为错误.一种解决方法是侦听警报的显示属性并在该侦听器中进行任何大小/位置调整。类似的东西(显然只是一个 poc)alert.showingProperty().addListener((src, ov, nv) -> {&nbsp; &nbsp; double x = alert.getX();&nbsp; &nbsp; double y = alert.getY();&nbsp; &nbsp; double w = alert.getWidth();&nbsp; &nbsp; double h = alert.getHeight();&nbsp; &nbsp; // as example just adjust if location top/left is off&nbsp;&nbsp; &nbsp; // production must cope with bottom/right off as well, obviously&nbsp;&nbsp; &nbsp; if (x < 0) {&nbsp; &nbsp; &nbsp; &nbsp; alert.setWidth(w + x);&nbsp; &nbsp; &nbsp; &nbsp; alert.setY(0);&nbsp; &nbsp; }&nbsp; &nbsp; if (y <0) {&nbsp; &nbsp; &nbsp; &nbsp; alert.setHeight(h + y);&nbsp; &nbsp; &nbsp; &nbsp; alert.setY(0);&nbsp; &nbsp; }});alert.showAndWait();仅供参考:我真的认为这是一个错误,所以提交了一个问题让我们看看会发生什么

撒科打诨

当您将对话框定位在舞台中央时.. 如果舞台也(至少部分)在您的屏幕之外,则该对话框只能在屏幕之外。请看下面的代码示例..@Overridepublic void start(Stage stage) {&nbsp; &nbsp; Pane pane = new Pane();&nbsp; &nbsp; Scene scene = new Scene(pane, 800, 500);&nbsp; &nbsp; Button button = new Button("Alert");&nbsp; &nbsp; button.setOnAction(new EventHandler<ActionEvent>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void handle(ActionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Alert alert = new Alert(Alert.AlertType.INFORMATION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert.setHeaderText("This is an alert!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert.initOwner(stage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert.setOnShown(new EventHandler<DialogEvent>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void handle(DialogEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(alert.getOwner().getX());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(alert.getOwner().getY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Values from screen&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int screenMaxX = (int) Screen.getPrimary().getVisualBounds().getMaxX();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int screenMaxY = (int) Screen.getPrimary().getVisualBounds().getMaxY();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Values from stage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = (int) stage.getWidth();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = (int) stage.getHeight();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int stageMaxX = (int) stage.getX();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int stageMaxY = (int) stage.getY();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Maximal values your stage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int paneMaxX = screenMaxX - width;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int paneMaxY = screenMaxY - height;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Check if the position of your stage is not out of screen&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (stageMaxX > paneMaxX || stageMaxY > paneMaxY) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Set stage where ever you want&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert.showAndWait();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; pane.getChildren().add(button);&nbsp; &nbsp; stage.setScene(scene);&nbsp; &nbsp; stage.show();}
随时随地看视频慕课网APP

相关分类

Java
我要回答