JavaFX:在拖动时松开未修饰的 window
JavaFX: Loosing undecorated window while dragging it
我有一个未装饰的舞台,我在其中添加了一个包含两个 AnchorPains(一个标题栏和一个 mainContent)的 AnchorPane。为了像正常的 window 一样使用这个 window,我实现了 p.e。用于在两侧调整其大小并将其拖动到标题栏上的功能。为了拖动 window,我分析了 MouseEvents onMouseDragged 和 onMousePressed(参见下面的源代码)。源代码基本上可以正常工作。但是我有一个问题,如果我移动鼠标太快,window 将不会移动。一开始鼠标在标题栏上。在此之后,我将鼠标向一侧移动得太快,以至于它在 window 之外,并且不会再被拖动。一旦我回到标题栏,我就可以再次移动它。有人建议如何解决这个问题吗?是否可以将 window 绑定到鼠标 Cursor?
这是我的源代码:
titleContent.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
Stage stage = (Stage) titleContent.getScene().getWindow();
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
if (mouseEvent.getClickCount()==1){
// record a delta distance for the drag and drop operation.
if (stage.getWidth()==bounds.getMaxX() && stage.getHeight()==bounds.getMaxY()){
wasMaximized =true;
}
if(!wasMaximized){
dragDelta.x = stage.getX() - mouseEvent.getScreenX();
dragDelta.y = stage.getY() - mouseEvent.getScreenY();
}
else{
dragDelta.x=-1*minWidth/2;
dragDelta.y=0;
wasMaximized=false;
}
}
}
});
titleContent.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
Scene scene = (Scene) titleContent.getScene();
if (!(mouseEvent.getX() < border || mouseEvent.getX() > scene.getWidth() -border) && !(mouseEvent.getX() < border && mouseEvent.getY() > scene.getHeight() -border)){
Stage stage = (Stage) titleContent.getScene().getWindow();
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
if (stage.getWidth()==bounds.getMaxX() && stage.getHeight()==bounds.getMaxY()){
stage.setWidth(minWidth);
stage.setHeight(minHeight);
}
stage.setX(mouseEvent.getScreenX() + dragDelta.x);
stage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
}
});
我找到了解决办法。对于每个人如何对它感兴趣:问题是 titlecontent.setOnMouseDragged 函数中的 if 语句。如果鼠标在给定元素上被拖拽,该函数就会被调用。这就是为什么没有必要在那里使用 if 语句的原因。正确的函数如下所示:
titleContent.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
Scene scene = (Scene) titleContent.getScene();
if (!(mouseEvent.getX() < border || mouseEvent.getX() > scene.getWidth() -border) && !(mouseEvent.getX() < border && mouseEvent.getY() > scene.getHeight() -border)){
Stage stage = (Stage) titleContent.getScene().getWindow();
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
if (stage.getWidth()==bounds.getMaxX() && stage.getHeight()==bounds.getMaxY()){
stage.setWidth(minWidth);
stage.setHeight(minHeight);
}
stage.setX(mouseEvent.getScreenX() + dragDelta.x);
stage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
}
因此,这是一个完整的解决方案,用于拖动未装饰的舞台并调整其大小。希望对您有所帮助。
我有一个未装饰的舞台,我在其中添加了一个包含两个 AnchorPains(一个标题栏和一个 mainContent)的 AnchorPane。为了像正常的 window 一样使用这个 window,我实现了 p.e。用于在两侧调整其大小并将其拖动到标题栏上的功能。为了拖动 window,我分析了 MouseEvents onMouseDragged 和 onMousePressed(参见下面的源代码)。源代码基本上可以正常工作。但是我有一个问题,如果我移动鼠标太快,window 将不会移动。一开始鼠标在标题栏上。在此之后,我将鼠标向一侧移动得太快,以至于它在 window 之外,并且不会再被拖动。一旦我回到标题栏,我就可以再次移动它。有人建议如何解决这个问题吗?是否可以将 window 绑定到鼠标 Cursor?
这是我的源代码:
titleContent.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
Stage stage = (Stage) titleContent.getScene().getWindow();
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
if (mouseEvent.getClickCount()==1){
// record a delta distance for the drag and drop operation.
if (stage.getWidth()==bounds.getMaxX() && stage.getHeight()==bounds.getMaxY()){
wasMaximized =true;
}
if(!wasMaximized){
dragDelta.x = stage.getX() - mouseEvent.getScreenX();
dragDelta.y = stage.getY() - mouseEvent.getScreenY();
}
else{
dragDelta.x=-1*minWidth/2;
dragDelta.y=0;
wasMaximized=false;
}
}
}
});
titleContent.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
Scene scene = (Scene) titleContent.getScene();
if (!(mouseEvent.getX() < border || mouseEvent.getX() > scene.getWidth() -border) && !(mouseEvent.getX() < border && mouseEvent.getY() > scene.getHeight() -border)){
Stage stage = (Stage) titleContent.getScene().getWindow();
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
if (stage.getWidth()==bounds.getMaxX() && stage.getHeight()==bounds.getMaxY()){
stage.setWidth(minWidth);
stage.setHeight(minHeight);
}
stage.setX(mouseEvent.getScreenX() + dragDelta.x);
stage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
}
});
我找到了解决办法。对于每个人如何对它感兴趣:问题是 titlecontent.setOnMouseDragged 函数中的 if 语句。如果鼠标在给定元素上被拖拽,该函数就会被调用。这就是为什么没有必要在那里使用 if 语句的原因。正确的函数如下所示:
titleContent.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
Scene scene = (Scene) titleContent.getScene();
if (!(mouseEvent.getX() < border || mouseEvent.getX() > scene.getWidth() -border) && !(mouseEvent.getX() < border && mouseEvent.getY() > scene.getHeight() -border)){
Stage stage = (Stage) titleContent.getScene().getWindow();
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
if (stage.getWidth()==bounds.getMaxX() && stage.getHeight()==bounds.getMaxY()){
stage.setWidth(minWidth);
stage.setHeight(minHeight);
}
stage.setX(mouseEvent.getScreenX() + dragDelta.x);
stage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
}
因此,这是一个完整的解决方案,用于拖动未装饰的舞台并调整其大小。希望对您有所帮助。