Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

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
  • onReleased
  • 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.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I created a sample of an animated clock in an undecorated window which you can drag around.

Relevant code from the sample is:

// 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...