__无标题栏的窗口拖动___javafx

时间:2021-05-26 15:16:26

遇到困难::添加mouseevent监听,我用的mouse_DragedDetected配合MouseEvent.Pressed,,闪的不行,后来借鉴swing的pressed,move,release那个,但是这个fx在拖动时就认为不在pressed状态下,这个时候这个按下点和弹起就靠近了。,,,

解决方法:在mouseDrag中直接操作,,,具体:原始鼠标位置与窗口位置的差距,拖动后的位置为当前鼠标位置减去前者。

 1.声明:
 private double lastx_distance,lasty_distance;
 2.确认初位置:
 stage.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
             private int i=0;
             @Override
             public void handle(MouseEvent event) {
                 // TODO Auto-generated method stub
                 System.out.println("点了舞台::"+i+++"左上角:["+stage.getX()+","+stage.getY()+"],鼠标位置_在窗口中:["
                         +event.getX()+","+event.getY()+"],在屏幕中点:["+event.getScreenX()+","+event.getScreenY()+"]");

                 lastx_distance = event.getScreenX()-stage.getX();
                 lasty_distance = event.getScreenY()-stage.getY();
             }});
 3.拖动实现:
 stage.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
             int i=0;
             ///-----------------------------------方法-----2---------------
             //last-point已经有初值了
             @Override
             public void handle(MouseEvent event) {
                 // TODO Auto-generated method stub
                 System.out.println("不稳定,拖动次数::"+i++);
                 move_stage = true;
                 stage.setX(event.getScreenX()-lastx_distance);
                 stage.setY(event.getScreenY()-lasty_distance);
             }
         });