我已经实现了一个EventHandler,它可以获得鼠标的窗口拖动:
public void mouseEventHandler(){
final Delta dragDelta = new Delta();
view.getScene().setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
dragDelta.x = mainStage.getX() - mouseEvent.getScreenX();
dragDelta.y = mainStage.getY() - mouseEvent.getScreenY();
}
});
view.getScene().setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
mainStage.setX(mouseEvent.getScreenX() + dragDelta.x);
mainStage.setY(mouseEvent.getScreenY() + dragDelta.y);
stage.setX(mainStage.getX());
stage.setY(mainStage.getY() + view.getScene().getHeight() + 25);
}
});
}
我有两个阶段:mainStage和一个舞台,它沉迷于mainStage的坐标。我的问题是,只有在我点击mainStage的窗口并拖动它时,舞台才会移动。但是如果我点击窗口的标题栏进行删除,它就不会移动。 如何获得标题栏的MouseEvent?
这是show()方法,因此您可以理解上瘾:
public void show(Stage mainStage, MenuPresenter menuPresenter){
this.model = new Model();
this.view = new GameView(this.model);
this.chatWatchView = new ChatWatchView();
this.score = this.model.getScore();
this.menuPresenter = menuPresenter;
this.mainStage = mainStage;
mainStage.setScene(view.getScene());
mainStage.centerOnScreen();
stage = new Stage();
stage.setScene(chatWatchView.getScene());
stage.initStyle(StageStyle.UNDECORATED);
stage.setX(mainStage.getX());
stage.setY(mainStage.getY() + view.getScene().getHeight() + 25);
stage.show();
mainStage.show();
startTimeline();
activateEventHandler();
mouseEventHandler();
//view.setScore("Score: " + model.getScore());
}
答案 0 :(得分:0)
有类似的问题。考虑将听众添加到舞台的yProperty()
和void updatePosition(double stageX, double stageY) {
//...
}
,我认为它会更好
在我的情况下,我最终得到了类似的东西
stage.xProperty().addListener(((observable, oldValue, newValue) -> {
updatePosition(newValue, stage.getY())
}));
stage.yProperty().addListener(((observable, oldValue, newValue) -> {
updatePosition(stage.getX(), newValue)
}));
然后
{{1}}