<tfoot id='W9dIV'></tfoot>
  • <legend id='W9dIV'><style id='W9dIV'><dir id='W9dIV'><q id='W9dIV'></q></dir></style></legend>
    • <bdo id='W9dIV'></bdo><ul id='W9dIV'></ul>

    1. <small id='W9dIV'></small><noframes id='W9dIV'>

      <i id='W9dIV'><tr id='W9dIV'><dt id='W9dIV'><q id='W9dIV'><span id='W9dIV'><b id='W9dIV'><form id='W9dIV'><ins id='W9dIV'></ins><ul id='W9dIV'></ul><sub id='W9dIV'></sub></form><legend id='W9dIV'></legend><bdo id='W9dIV'><pre id='W9dIV'><center id='W9dIV'></center></pre></bdo></b><th id='W9dIV'></th></span></q></dt></tr></i><div id='W9dIV'><tfoot id='W9dIV'></tfoot><dl id='W9dIV'><fieldset id='W9dIV'></fieldset></dl></div>

        使用 JavaFX 在任何地方处理鼠标事件

        时间:2023-09-30
        <tfoot id='Z7OGq'></tfoot>

          <small id='Z7OGq'></small><noframes id='Z7OGq'>

        • <i id='Z7OGq'><tr id='Z7OGq'><dt id='Z7OGq'><q id='Z7OGq'><span id='Z7OGq'><b id='Z7OGq'><form id='Z7OGq'><ins id='Z7OGq'></ins><ul id='Z7OGq'></ul><sub id='Z7OGq'></sub></form><legend id='Z7OGq'></legend><bdo id='Z7OGq'><pre id='Z7OGq'><center id='Z7OGq'></center></pre></bdo></b><th id='Z7OGq'></th></span></q></dt></tr></i><div id='Z7OGq'><tfoot id='Z7OGq'></tfoot><dl id='Z7OGq'><fieldset id='Z7OGq'></fieldset></dl></div>

            <bdo id='Z7OGq'></bdo><ul id='Z7OGq'></ul>
          • <legend id='Z7OGq'><style id='Z7OGq'><dir id='Z7OGq'><q id='Z7OGq'></q></dir></style></legend>
                <tbody id='Z7OGq'></tbody>

                  本文介绍了使用 JavaFX 在任何地方处理鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 JavaFX 应用程序,我想为场景中任意位置的鼠标单击添加一个事件处理程序.以下方法工作正常,但不完全按照我想要的方式.下面是一个示例来说明问题:

                  I have a JavaFX application, and I would like to add an event handler for a mouse click anywhere within the scene. The following approach works ok, but not exactly in the way I want to. Here is a sample to illustrate the problem:

                  public void start(Stage primaryStage) {
                      root = new AnchorPane();
                      scene = new Scene(root,500,200);
                      scene.setOnMousePressed(new EventHandler<MouseEvent>() {
                          @Override
                          public void handle(MouseEvent event) {
                              System.out.println("mouse click detected! "+event.getSource());
                          }
                      });
                  
                      Button button = new Button("click here");
                      root.getChildren().add(button);
                  
                      primaryStage.setScene(scene);
                      primaryStage.show();
                  }
                  

                  如果我点击空白区域的任意位置,EventHandler 会调用 handle() 方法,但如果我点击 buttonEventHandlercode>handle() 方法没有被调用.我的应用程序中有许多按钮和其他交互元素,因此我需要一种方法来捕获对这些元素的点击,而不必为每个元素手动添加新的处理程序.

                  If I click anywhere in empty space, the EventHandler invokes the handle() method, but if i click the button, the handle() method is not invoked. There are many buttons and other interactive elements in my application, so I need an approach to catch clicks on those elements as well without having to manually add a new handler for every single element.

                  推荐答案

                  您可以使用 addEventFilter().这将在任何子控件使用事件之前调用.下面是事件过滤器的代码.

                  You can add an event filter to the scene with addEventFilter(). This will be called before the event is consumed by any child controls. Here's what the code for the event filter looks like.

                  scene.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
                      @Override
                      public void handle(MouseEvent mouseEvent) {
                          System.out.println("mouse click detected! " + mouseEvent.getSource());
                      }
                  });
                  

                  这篇关于使用 JavaFX 在任何地方处理鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在类的构造函数中找出实例化对象 下一篇:getSource() 和 getActionCommand()

                  相关文章

                  最新文章

                    <bdo id='QE0Q7'></bdo><ul id='QE0Q7'></ul>
                  1. <legend id='QE0Q7'><style id='QE0Q7'><dir id='QE0Q7'><q id='QE0Q7'></q></dir></style></legend>

                      <tfoot id='QE0Q7'></tfoot>
                    1. <small id='QE0Q7'></small><noframes id='QE0Q7'>

                      <i id='QE0Q7'><tr id='QE0Q7'><dt id='QE0Q7'><q id='QE0Q7'><span id='QE0Q7'><b id='QE0Q7'><form id='QE0Q7'><ins id='QE0Q7'></ins><ul id='QE0Q7'></ul><sub id='QE0Q7'></sub></form><legend id='QE0Q7'></legend><bdo id='QE0Q7'><pre id='QE0Q7'><center id='QE0Q7'></center></pre></bdo></b><th id='QE0Q7'></th></span></q></dt></tr></i><div id='QE0Q7'><tfoot id='QE0Q7'></tfoot><dl id='QE0Q7'><fieldset id='QE0Q7'></fieldset></dl></div>