查找解决方案:Javafx:在场景上绑定多个鼠标事件

时间:2016-02-11 08:05:33

标签: javafx

我正在尝试将两个单独的事件绑定到javafx中的场景。

    scene.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<Event>() {
            @Override
            public void handle(Event event) {
           ......Drag Content Task.......
}
});

那么如何才能让这两个事件得到任何建议。

scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getClickCount() == 2){
                    System.out.println("Call Dbl Click Eve");
                  Zooming....Task.....
}
});

我写了一些代码,它对我有用! 使用当前时间计算两次点击事件的时间,如果它小于预期的dblclick,则考虑双击..

scene.addEventHandler(MouseEvent.ANY, e -> {   
if (e.getEventType().equals(MouseEvent.DRAG_DETECTED)) {
            System.out.println("Drag Event call ");
            drag_Flag=true;
            }
            if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED) && drag_Flag) {
            System.out.println("Rellease Event call");
            drag_Flag=false;
            System.out.println("Drag Event Flag : "+drag_Flag);
            time1=0;
            time2=0;
        } else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED) && !drag_Flag) {
                long diff = 0;
            if(time1==0)
             time1=System.currentTimeMillis();
            else
            time2=System.currentTimeMillis();
            if(time1!=0 && time2!=0)
            diff=time2-time1;
                if((diff/1000)<=215 && diff>0)
            {
                isdblClicked=true;
            }
            else
            {
                isdblClicked=false;
            }

            System.out.println("IsDblClicked()"+isdblClicked);

            if(isdblClicked==true)
            {
            ZoomInOutPojo z = null;
                do_Zoom();
                time1=time2=0;
                isdblClicked=false;

            }
            else
            {
                statement1;
            }

            }
                drag_Flag=false;
        }

    });

3 个答案:

答案 0 :(得分:1)

  

解决方案适用于我的案例。

 static boolean drag_Flag=false
scene.addEventHandler(MouseEvent.ANY, e -> {
        if (e.getEventType().equals(MouseEvent.DRAG_DETECTED)) {
            System.out.println("Drag Event call ");
            drag_Flag=true;
}
 if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED) && drag_Flag) {
            System.out.println("Rellease Event call");


            drag_Flag=false;
            System.out.println("Drag Event Flag : "+drag_Flag);
            time1=0;
            time2=0;

        } else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED) && !drag_Flag) {
                long diff = 0;
            if(time1==0)
             time1=System.currentTimeMillis();
            else
            time2=System.currentTimeMillis();
            if(time1!=0 && time2!=0)
            diff=time2-time1;
            if((diff/1000)<=215 && diff>0)
            {
                isdblClicked=true;
            }
            else
            {
                isdblClicked=false;
            }

            System.out.println("IsDblClicked()"+isdblClicked);

            if(isdblClicked==true)
            {
            ZoomInOutPojo z = null;
                do_Zoom();
                time1=time2=0;
                isdblClicked=false;
            }
            else
            {
                statement1;
            }

            }

            drag_Flag=false;
        }

    });

答案 1 :(得分:0)

你发布它的方式应该已经有效,另一种方法是:

scene.addEventHandler(MouseEvent.ANY, e -> {
        if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED)) {
            // do something
        } else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED)) {
            // do something else
        }
    });

答案 2 :(得分:0)

您也可以使用以下代码。试试吧。

 EventHandler<MouseEvent> clickEvent = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
             if(event.getClickCount() == 2){
                    System.out.println("Call Dbl Click Eve");
             }
        }
    };

    EventHandler<MouseEvent> releaseEvent = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            System.out.println("Rele");
        }
    };

    scene.addEventHandler(MouseEvent.MOUSE_CLICKED, clickEvent);
    scene.addEventHandler(MouseEvent.MOUSE_RELEASED, releaseEvent);

我希望它会对你有所帮助。