Swing&JavaFX:如何将JavaFX MouseEvent转换为Swing MouseEvent?

huangapple go评论64阅读模式
英文:

Swing & JavaFX: how to convert JavaFX MouseEvent to Swing MouseEvent?

问题

`SwingUtilities`包中有一个很好的函数,`convertMouseEvent`,用于将组件上的鼠标事件转换为另一个组件上的鼠标事件

```java
MouseEvent convertedEvent = SwingUtilities.convertMouseEvent(originalComponent, event, otherComponent);

与此同时,JavaFX中的MouseEvent有一个名为copyFor的方法,用于为另一个组件创建MouseEvent的副本:

MouseEvent convertedEvent = e.copyFor(e.getSource(), otherComponent);

我想做类似的事情,将JavaFx的MouseEvent e转换为Swing鼠标事件。

我找不到现成的函数,所以我尝试编写自己的函数。但有两个字段我不能轻易转换:

  • 用于标识事件的Id字段。我猜我需要手动从e.getEventType().getEventType().getName()进行转换。
  • 在JavaFX中,没有指示事件发生时间的"when"属性。在这里,我似乎可以使用当前系统时间。
  • 鼠标事件的修饰符。我不确定如何从JavaFX事件的属性构建这些修饰符。

是否有办法从e的属性中获取修饰符?


<details>
<summary>英文:</summary>

The package `SwingUtilities` has a nice function, `convertMouseEvent`, to convert a mouse event on a component to a mouse event on another component:

```java
MouseEvent convertedEvent = SwingUtilities.convertMouseEvent(originalComponent, event, otherComponent);

Meanwhile, the MouseEvent in JavaFX has a method copyFor to create a copy of the MouseEvent for another component:

MouseEvent convertedEvent = e.copyFor(e.getSource(), otherComponent);

I would like to do something similar to convert a JavaFx MouseEvent e to a Swing mouse event.

I couldn't find a baked in function, so I tried to write my own. There are two fields I can't readily convert though:

  • Id field that identifies the event. I assume I'll have to manually convert from e.getEventType().getEventType().getName()
  • In JavaFX there is no "when" property indicating when the event occurred. It seems I could use the current system time here
  • The modifier of the mouse event. I'm not sure how to build those from the properties of the JavaFX event

Is there a way to get the modifier from the properties of e?

答案1

得分: 0

SwingEvents 中的函数可能很有用。 可以通过以下导入进行访问:

import com.sun.javafx.embed.swing.SwingEvents;

从 JavaFX 转换到 Swing:

SwingEvents.fxMouseButtonToMouseButton(fxEvent):将 JavaFX 事件转换为 Swing 鼠标按钮。

SwingEvents.fxMouseEventTypeToMouseID(fxEvent):将 JavaFX 事件转换为 Swing 鼠标 ID。

SwingEvents.fxMouseModsToMouseMods(fxEvent):将 JavaFX 事件转换为 Swing 修饰键。

从 Swing 转换到 JavaFX:

SwingEvents.mouseIDToEmbedMouseType(swingEvent.getID()):将 Swing 鼠标事件 ID 转换为 JavaFX MouseType。

SwingEvents.mouseButtonToEmbedMouseButton(swingEvent.getButton, swingEvent.getModifiersEx()):将 Swing 鼠标按钮转换为 JavaFX 鼠标按钮。目前存在错误(JDK-8242419)。

英文:

The functions in SwingEvents can be useful. They can be accessed with the import

import com.sun.javafx.embed.swing.SwingEvents;

To convert from JavaFX to Swing:

SwingEvents.fxMouseButtonToMouseButton(fxEvent): converts a JavaFX event to a Swing mouse button.

SwingEvents.fxMouseEventTypeToMouseID(fxEvent): converts a JavaFX event to a Swing mouse ID.

SwingEvents.fxMouseModsToMouseMods(fxEvent): converts a JavaFX event to Swing mods.

To convert from Swing to JavaFX:

SwingEvents.mouseIDToEmbedMouseType(swingEvent.getID()): converts a Swing mouse event ID to a JavaFX MouseType.

SwingEvents.mouseButtonToEmbedMouseButton(swingEvent.getButton, swingEvent.getModifiersEx()): converts a Swing mouse button to a JavaFX mouse button. Currently bugged (JDK-8242419).

huangapple
  • 本文由 发表于 2020年4月8日 13:44:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61094025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定