在javafx中注册鼠标处理程序但未内联处理程序 [英] registering mouse handler but handler not inline, in javafx

查看:57
本文介绍了在javafx中注册鼠标处理程序但未内联处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaFX中有一个应用程序,该应用程序有些大,我想保持代码的可读性.

I have an app in JavaFX that is getting a bit large, and I want to keep the code readable.

我有一个LineChart,我想内置一个缩放功能,该功能在鼠标单击时发生.我知道我需要在图表上注册一个鼠标侦听器.我无法从Oracle示例中弄清楚什么,即写在这里:

I have a LineChart that I want to have zoom functionality built in, that occurs on a mouseclick. I know I need to register a mouse listener to the chart. What I cannot figure out from Oracle examples, ie as written here:

http://docs.oracle.com/javafx/2/events/handlers.htm

是如何不在注册中内联定义我的处理程序.换句话说,我希望处理程序的主体(包含很多代码行)在另一个类中.我可以那样做吗?如果是这样,如何在Javafx主控制器代码中将处理程序注册到图表中?

is how to NOT have my handler defined inline to the registering. In other words, I want the body of the handler (which is many lines of code) to be in another class. Can I do that? And if so, how do I register the handler to my chart in my main Javafx controller code?

推荐答案

将您的处理程序放入实现Mouse EventHandler的新类中,并通过该节点的setOnClicked方法向目标节点注册该类的实例.

Place your handler in a new class which implements the the Mouse EventHandler and register an instance of your class with your target node via the node's setOnClicked method.

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/** 
 * JavaFX sample for registering a click handler defined in a separate class.
 * http://stackoverflow.com/questions/12326180/registering-mouse-handler-but-handler-not-inline-in-javafx
 */ 
public class ClickHandlerSample extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(final Stage stage) throws Exception {
    stage.setTitle("Left click to zoom in, right click to zoom out");
    ImageView imageView = new ImageView("http://upload.wikimedia.org/wikipedia/commons/b/b7/Idylls_of_the_King_3.jpg");
    imageView.setPreserveRatio(true);
    imageView.setFitWidth(150);
    imageView.setOnMouseClicked(new ClickToZoomHandler());

    final StackPane layout = new StackPane();
    layout.getChildren().addAll(imageView);
    layout.setStyle("-fx-background-color: cornsilk;");
    stage.setScene(new Scene(layout, 400, 500));
    stage.show();
  }

  private static class ClickToZoomHandler implements EventHandler<MouseEvent> {
    @Override public void handle(final MouseEvent event) {
      if (event.getSource() instanceof Node) {
        final Node n = (Node) event.getSource();
        switch (event.getButton()) {
          case PRIMARY:
            n.setScaleX(n.getScaleX()*1.1);
            n.setScaleY(n.getScaleY()*1.1);
            break;
          case SECONDARY:
            n.setScaleX(n.getScaleX()/1.1);
            n.setScaleY(n.getScaleY()/1.1);
            break;
        }
      }
    }
  }
}

这篇关于在javafx中注册鼠标处理程序但未内联处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆