【问题标题】:Keep javafx window inside screen bounds将 javafx 窗口保持在屏幕边界内
【发布时间】:2014-10-05 14:42:47
【问题描述】:

我正在编写一个 javafx 应用程序,我希望我的窗口不会超出屏幕边界,因为这并没有多大用处。因此,例如,窗口不应该被拖动,使其一半离开屏幕。

public class ui extends Application{
    public static void main (String[] args){
        launch(args);
    }
    public void start(Stage mainStage){
        mainStage.initStyle(StageStyle.DECORATED);
        Rectangle2D mainScreen = Screen.getPrimary().getVisualBounds();
        mainStage.setWidth(mainScreen.getWidth());
        mainStage.setHeight(mainScreen.getHeight());

        BorderPane mainPane = new BorderPane(background);

        Scene mainScene = new Scene(mainPane, Color.BLACK);
        mainStage.setScene(mainScene);

        mainStage.show();
    }
}

【问题讨论】:

  • 如果你能把你的一些代码扔给我们,我们就能为你做更多的事情。现在,我只能猜你有什么......
  • @DreadHeadedDeveloper 我只需要一个通用的解决方案,所以我不确定它是否有帮助,但我会添加一些代码。

标签: java javafx window screen bounds


【解决方案1】:

不这样做的原因有很多:您需要注意不要禁用用户可能期望的功能,例如在扩展桌面上拖动(即在多个物理显示器之间拖动)或在虚拟桌面之间移动窗口(例如 Mac OS X 中的“Spaces”,或 Linux 中的等效系统)。

您可以在这里做的最好的事情可能是用侦听器观察窗口的位置,如果它超出所需的范围,则将其推回最佳位置。这可能作为用户体验有点不尽人意,但实现了你想要的功能:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class ConstrainedWindow extends Application {

    @Override
    public void start(Stage primaryStage) {

        Bounds allScreenBounds = computeAllScreenBounds();
        ChangeListener<Number> boundsListener = (obs, oldValue, newValue) -> {
            double x = primaryStage.getX();
            double y = primaryStage.getY();
            double w = primaryStage.getWidth();
            double h = primaryStage.getHeight();
            if (x < allScreenBounds.getMinX()) {
                primaryStage.setX(allScreenBounds.getMinX());
            }
            if (x + w > allScreenBounds.getMaxX()) {
                primaryStage.setX(allScreenBounds.getMaxX() - w);
            }
            if (y < allScreenBounds.getMinY()) {
                primaryStage.setY(allScreenBounds.getMinY());
            }
            if (y + h > allScreenBounds.getMaxY()) {
                primaryStage.setY(allScreenBounds.getMaxY() - h);
            }
        };
        primaryStage.xProperty().addListener(boundsListener);
        primaryStage.yProperty().addListener(boundsListener);
        primaryStage.widthProperty().addListener(boundsListener);
        primaryStage.heightProperty().addListener(boundsListener);
        primaryStage.show();
    }

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

    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • 只是因为它不允许我发表少于 15 个字符的评论,根本没有任何意义:)
  • 像魅力一样工作!谢谢。
【解决方案2】:

这里有一些可能会有所帮助的想法

public final int width = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
public final int height = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

这两行代码可以让你得到大多数计算机屏幕尺寸的长度和宽度。

接下来,我会设置一个方法来不断获取窗口的位置,或者至少在它移动时改变值,然后根据你对屏幕大小和窗口大小的了解来设置限制。一种方法是......

if(yWinPos > (height - wHeight))//wHeight is actual height of the window, not the y pos of window
{

//code that prevents it from moving past the bottom part of screen

}

让我知道这是否有帮助或者它不能回答你的问题,我会编辑我的答案作为回应。

【讨论】:

  • 除非真的必须,否则不要将 AWT 代码与 JavaFX 代码混合使用。
  • @James_D 会有那么糟糕吗?我们只是用它来获取屏幕的尺寸,然后我们就完成了,只需将它存储到一个 int 中,然后继续
  • 在这种情况下可能没问题,但通常您无法真正保证是否存在 AWT 工具包。有时你必须做出这样的假设才能获得你想要的功能(例如与任务栏交互),但如果只使用一个工具包就可以完成同样的事情,那么这种方式总是更好。
猜你喜欢
  • 2016-02-08
  • 1970-01-01
  • 2021-01-03
  • 2012-01-05
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多