8

をシミュレートする必要がありますMouseEvent.MOUSE_CLICKED。前述のタイプのイベントをディスパッチするために、特定のノードのfireEventメソッドを使用したいと思います。しかし、私はそれを生成するのに苦労しています。javafx.scene.input.MouseEventには有効なコンストラクターがないようですが、古いオブジェクトはこの方法でインスタンス化できます。それでも、変換のための実用的な解決策は見つかりませんでした。どうすればこれを回避できますか?java.awt.event.MouseEvent

ありがとう。

4

4 に答える 4

8

You can generate a MouseEvent using the deprecated MouseEvent.impl_mouseEvent API. I did this previously in this forum thread for JavaFX 2.0. Note that the API is deprecated for a reason - it is private API used in the implementation of JavaFX and the API is not guaranteed to maintain the same signature or even exist in future versions (which can be evidenced because the original code I posted in the forum thread no longer compiles.

The correct solution to generating such an event is to have a public API so support this. There has already been a request filed to supply this functionality RT-9383 "Add proper constructors & factory methods to event classes, remove impl". This jira is scheduled to be completed next year for JavaFX 3.0.

In the meantime, usage of the Robot class as Sergey suggests is probably your best method.


Update: Java 8 added public constructors for javafx.event.MouseEvent and the (as indicated in Jay Thakkar's answer), you can fire such an event using Event.fireEvent (you can also fire events on Windows).

于 2012-07-19T18:31:32.313 に答える
3

または、単純な「ハック」を使用して、ボタンをプログラムでクリックすることもできます。「Util」クラスでこのメソッドを作成します。

public static void click(javafx.scene.control.Control control) {
    java.awt.Point originalLocation = java.awt.MouseInfo.getPointerInfo().getLocation();
    javafx.geometry.Point2D buttonLocation = control.localToScreen(control.getLayoutBounds().getMinX(), control.getLayoutBounds().getMinY());
    try {
        java.awt.Robot robot = new java.awt.Robot();
        robot.mouseMove((int)buttonLocation.getX(), (int)buttonLocation.getY());
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.mouseMove((int) originalLocation.getX(), (int)originalLocation.getY());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

次に、ボタンを「クリック」するには、ボタンをパラメーターとして渡してメソッド click を呼び出すだけです。

于 2014-12-17T17:42:43.967 に答える
1

ハンドラーを設定すると、パブリック プロパティが設定されます。そのプロパティからイベントを取得し、handle() を呼び出すことができます。

button1.setOnMouseClicked()....
the corresponding property is
button1.onMouseClickedProperty().get().handle(me);//where me is some MouseEvent object
于 2016-09-08T06:23:31.430 に答える