在JavaFX 2中移动未装饰的舞台 [英] Moving an undecorated stage in javafx 2
问题描述
我一直在尝试通过使用以下鼠标侦听器在屏幕上移动未装饰的舞台:
I've been trying to move an undecorated stage around the screen, by using the following mouse listeners:
- onPressed
- 已发布
- onDragged
这些事件来自矩形.我的想法是通过单击矩形并拖动所有窗口来移动未装饰的窗口.
These events are from a rectangle. My idea is to move the undecorated window clicking on the rectangle and dragging all the window.
@FXML
protected void onRectanglePressed(MouseEvent event) {
X = primaryStage.getX() - event.getScreenX();
Y = primaryStage.getY() - event.getScreenY();
}
@FXML
protected void onRectangleReleased(MouseEvent event) {
primaryStage.setX(event.getScreenX());
primaryStage.setY(event.getScreenY());
}
@FXML
protected void onRectangleDragged(MouseEvent event) {
primaryStage.setX(event.getScreenX() + X);
primaryStage.setY(event.getScreenY() + Y);
}
这些事件的全部作用是当我按下矩形并开始拖动窗口时,它会移动一点.但是,当我释放按钮时,窗口将移动到矩形所在的位置.
All that I've got with these events is when I press the rectangle and start to drag the window, it moves a little bit. But, when I release the button, the window is moved to where the rectangle is.
谢谢.
推荐答案
我创建了一个示例动画时钟放在未装饰的窗口中,您可以拖动它.
I created a sample of an animated clock in an undecorated window which you can drag around.
示例中的相关代码为:
// allow the clock background to be used to drag the clock around.
final Delta dragDelta = new Delta();
layout.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta distance for the drag and drop operation.
dragDelta.x = stage.getX() - mouseEvent.getScreenX();
dragDelta.y = stage.getY() - mouseEvent.getScreenY();
}
});
layout.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
stage.setX(mouseEvent.getScreenX() + dragDelta.x);
stage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
});
...
// records relative x and y co-ordinates.
class Delta { double x, y; }
代码看起来与您的代码非常相似,所以不太确定为什么您的代码对您不起作用.
Code looks pretty similar to yours, so not quite sure why your code is not working for you.
这篇关于在JavaFX 2中移动未装饰的舞台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!