如何在JavaFX中使未修饰的窗口可移动/可拖动? [英] How to make an undecorated window movable / draggable in JavaFX?

查看:227
本文介绍了如何在JavaFX中使未修饰的窗口可移动/可拖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个将禁用最小化"和最大化"按钮的应用程序.

I have to create an application in which minimize and maximize button will be disabled.

我使用了"StageStyle.UNDECORATED",该应用程序将不再可移动或拖动,因此我正在寻找其他任何替代方法来制作我的应用程序.

I have used "StageStyle.UNDECORATED" with which the application will not be movable or draggable anymore, so I am searching for any other alternative to make my application.

有人对此有解决方案吗?

Do anyone having solution for this?

推荐答案

要使窗口无装饰但仍可移动/可拖动,您必须在您选择的任何节点上处理适当的MouseEvent.

To achieve the window to be undecorated but still movable/dragable you have to handle the appropriate MouseEvent on any node of your choice.

示例:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class SimpleWindowApplication extends Application {
    private double xOffset = 0;
    private double yOffset = 0;

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

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.initStyle(StageStyle.UNDECORATED);
        BorderPane root = new BorderPane();

        root.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setX(event.getScreenX() - xOffset);
                primaryStage.setY(event.getScreenY() - yOffset);
            }
        });

        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

从Oracle JavaFX下载页面上包含的非常有价值的示例中了解更多信息: JavaFX演示和示例

Learn more from the very valuable examples contained on Oracle's JavaFX download page under: JavaFX Demos and Samples

这篇关于如何在JavaFX中使未修饰的窗口可移动/可拖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆