JavaFX手勢(shì)事件

2018-01-09 19:24 更新

JavaFX教程 - JavaFX手勢(shì)事件


JavaFX可以處理來(lái)自觸摸使能設(shè)備的觸摸和手勢(shì)的事件。

手勢(shì)和觸摸事件

支持觸摸啟用設(shè)備的以下事件

手勢(shì) 描述
Rotate Triggered when using two-finger to rotate objects.

它觸發(fā)以下事件

  • ROTATION_STARTED
  • ROTATE
  • ROTATION_FINISHED
Scroll Sliding movement.

如果鼠標(biāo)滾輪用于滾動(dòng),則僅生成類(lèi)型為SCROLL的事件。它觸發(fā)以下事件:

  • SCROLL_STARTED
  • SCROLL
  • SCROLL_FINISHED
Swipe 在屏幕上掃描運(yùn)動(dòng)。
    它觸發(fā)以下事件:
  • SWIPE_LEFT
  • SWIPE_RIGHT
  • SWIPE_UP
  • SWIPE_DOWN
  • Zoom 由雙指縮放。它會(huì)啟動(dòng)以下事件:
    • ZOOM_STARTED
    • ZOOM
    • ZOOM_FINISHED
    • 手勢(shì)事件示例

      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.input.RotateEvent;
      import javafx.scene.input.ScrollEvent;
      import javafx.scene.input.SwipeEvent;
      import javafx.scene.input.TouchEvent;
      import javafx.scene.input.ZoomEvent;
      import javafx.scene.shape.Rectangle;
      import javafx.stage.Stage;
      /*  w w  w.jav a 2 s . c o  m*/
      public class Main extends Application {
        public static void main(String[] args) {
          Application.launch(args);
        }
      
        @Override
        public void start(Stage primaryStage) {
          Group root = new Group();
          Scene scene = new Scene(root, 300, 250);
      
          Rectangle rect = new Rectangle();
          rect.setWidth(100);
          rect.setHeight(100);
          root.getChildren().add(rect);
          rect.setOnScroll(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent event) {
              if (!event.isInertia()) {
                rect.setTranslateX(rect.getTranslateX() + event.getDeltaX());
                rect.setTranslateY(rect.getTranslateY() + event.getDeltaY());
              }
              System.out.println("Rectangle: Scroll event" + ", inertia: "
                  + event.isInertia() + ", direct: " + event.isDirect());
              event.consume();
            }
          });
      
          rect.setOnScrollStarted(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent event) {
              System.out.println("Rectangle: Scroll started event");
              event.consume();
            }
          });
      
          rect.setOnScrollFinished(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent event) {
              System.out.println("Rectangle: Scroll finished event");
              event.consume();
            }
          });
      
          rect.setOnZoom(new EventHandler<ZoomEvent>() {
            @Override
            public void handle(ZoomEvent event) {
              rect.setScaleX(rect.getScaleX() * event.getZoomFactor());
              rect.setScaleY(rect.getScaleY() * event.getZoomFactor());
              System.out.println("Rectangle: Zoom event" + ", inertia: "
                  + event.isInertia() + ", direct: " + event.isDirect());
      
              event.consume();
            }
          });
      
          rect.setOnZoomStarted(new EventHandler<ZoomEvent>() {
            @Override
            public void handle(ZoomEvent event) {
              System.out.println("Rectangle: Zoom event started");
              event.consume();
            }
          });
      
          rect.setOnZoomFinished(new EventHandler<ZoomEvent>() {
            @Override
            public void handle(ZoomEvent event) {
              System.out.println("Rectangle: Zoom event finished");
              event.consume();
            }
          });
      
          rect.setOnRotate(new EventHandler<RotateEvent>() {
            @Override
            public void handle(RotateEvent event) {
              rect.setRotate(rect.getRotate() + event.getAngle());
              System.out.println("Rectangle: Rotate event" + ", inertia: "
                  + event.isInertia() + ", direct: " + event.isDirect());
              event.consume();
            }
          });
      
          rect.setOnRotationStarted(new EventHandler<RotateEvent>() {
            @Override
            public void handle(RotateEvent event) {
              System.out.println("Rectangle: Rotate event started");
              event.consume();
            }
          });
      
          rect.setOnRotationFinished(new EventHandler<RotateEvent>() {
            @Override
            public void handle(RotateEvent event) {
              System.out.println("Rectangle: Rotate event finished");
              event.consume();
            }
          });
      
          rect.setOnSwipeRight(new EventHandler<SwipeEvent>() {
            @Override
            public void handle(SwipeEvent event) {
              System.out.println("Rectangle: Swipe right event");
              event.consume();
            }
          });
      
          rect.setOnSwipeLeft(new EventHandler<SwipeEvent>() {
            @Override
            public void handle(SwipeEvent event) {
              System.out.println("Rectangle: Swipe left event");
              event.consume();
            }
          });
      
          rect.setOnTouchPressed(new EventHandler<TouchEvent>() {
            @Override
            public void handle(TouchEvent event) {
              System.out.println("Rectangle: Touch pressed event");
              event.consume();
            }
          });
      
          rect.setOnTouchReleased(new EventHandler<TouchEvent>() {
            @Override
            public void handle(TouchEvent event) {
              System.out.println("Rectangle: Touch released event");
              event.consume();
            }
          });
      
          rect.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
              if (event.isSynthesized()) {
                System.out.println("Ellipse: Mouse pressed event from touch"
                    + ", synthesized: " + event.isSynthesized());
              }
              event.consume();
            }
          });
      
          rect.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
              if (event.isSynthesized()) {
                System.out.println("Ellipse: Mouse released event from touch"
                    + ", synthesized: " + event.isSynthesized());
              }
              event.consume();
            }
          });
      
          primaryStage.setScene(scene);
          primaryStage.show();
        }
      }
      

      上面的代碼生成以下結(jié)果。

      null


      以上內(nèi)容是否對(duì)您有幫助:
      在線筆記
      App下載
      App下載

      掃描二維碼

      下載編程獅App

      公眾號(hào)
      微信公眾號(hào)

      編程獅公眾號(hào)