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

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

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

        <tfoot id='ks5Jt'></tfoot>

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

        时间:2023-06-28
        <i id='uhPOh'><tr id='uhPOh'><dt id='uhPOh'><q id='uhPOh'><span id='uhPOh'><b id='uhPOh'><form id='uhPOh'><ins id='uhPOh'></ins><ul id='uhPOh'></ul><sub id='uhPOh'></sub></form><legend id='uhPOh'></legend><bdo id='uhPOh'><pre id='uhPOh'><center id='uhPOh'></center></pre></bdo></b><th id='uhPOh'></th></span></q></dt></tr></i><div id='uhPOh'><tfoot id='uhPOh'></tfoot><dl id='uhPOh'><fieldset id='uhPOh'></fieldset></dl></div>

        <tfoot id='uhPOh'></tfoot>
          <bdo id='uhPOh'></bdo><ul id='uhPOh'></ul>
          • <legend id='uhPOh'><style id='uhPOh'><dir id='uhPOh'><q id='uhPOh'></q></dir></style></legend>
          • <small id='uhPOh'></small><noframes id='uhPOh'>

              <tbody id='uhPOh'></tbody>

                1. 本文介绍了使用 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 在任何地方处理鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:日期格式不起作用? 下一篇:getSource() 和 getActionCommand()

                  相关文章

                    <tfoot id='833R0'></tfoot>

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

                      <bdo id='833R0'></bdo><ul id='833R0'></ul>
                    1. <small id='833R0'></small><noframes id='833R0'>