Store javafx window inside screen borders

I am programming a javafx application and I want my window to stay on the screen, as there is little use for it. So, for example, the window should not be dragged so that half of it is not displayed.

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();
    }
}
+4
source share
2 answers

: , , , , (, ) (, "" Mac OS X 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);
    }
}
+5

,

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

2 .

, , , , , , . - ...

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

}

, - , .

0

All Articles