小编tre*_*498的帖子

JavaFX 窗口拖动捕捉并且不流畅

我正在开发一个以透明 AnchorPane(无标题栏和圆角)开头的应用程序。我希望能够拖动和移动窗口。我已经让它工作了,但是当我单击它时,窗口会向上捕捉到您从中心拖动的位置,而不是您单击的位置。

CSS:

.root {
    -fx-background-radius: 20; 
    -fx-border-radius: 20;
    -fx-background-color: transparent;
}
Run Code Online (Sandbox Code Playgroud)

主.java:

public void start(Stage primaryStage) throws Exception {
    primaryStage.initStyle(StageStyle.TRANSPARENT);
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(Main.class.getResource("../Scenes/Login.fxml"));

    //Creates the layout for the new scene
    AnchorPane layout = (AnchorPane) loader.load();             
    Scene scene = new Scene(layout);
    scene.setFill(Color.TRANSPARENT);
    scene.getStylesheets().add(getClass().getResource("../StyleSheets/application.css").toExternalForm());
    LoginController.allowDrag(layout, primaryStage);

    primaryStage.setScene(scene);   
    primaryStage.setResizable(false);
    primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)

控制器:

private static final Rectangle2D SCREEN_BOUNDS = Screen.getPrimary().getVisualBounds();

public static void allowDrag(AnchorPane root, Stage primaryStage) {
    root.setOnMousePressed((MouseEvent mouseEvent1) -> {
            xOffset = mouseEvent1.getSceneX();
            yOffset = mouseEvent1.getScreenY();
    });

    root.setOnMouseDragged((MouseEvent …
Run Code Online (Sandbox Code Playgroud)

javafx

2
推荐指数
1
解决办法
298
查看次数

标签 统计

javafx ×1