我们正在尝试 JavaFX 的多点触控功能,但似乎效果不佳。
在细节上,我们试图在 javafx.scene.layout.Pane 中拦截事件:
- onSwipeDown
- 向左滑动
- 向右滑动
- onSwipeUp
这是我们最简单的代码:
主.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
示例.xml
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.control.Button?>
<Pane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml"
onSwipeDown="#handleSwipeDown"
onSwipeUp="#handleSwipeUp"
onSwipeLeft="#handleSwipeLeft"
onSwipeRight="#handleSwipeRight">
<Button onAction="#btnClick" text="Click me" />
</Pane>
和 Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.scene.input.SwipeEvent;
public class Controller {
public void handleSwipeDown(SwipeEvent swipeEvent) {
System.out.println("EVENT: swipeDown");
}
public void handleSwipeUp(SwipeEvent swipeEvent) {
System.out.println("EVENT: swipeUp");
}
public void handleSwipeLeft(SwipeEvent swipeEvent) {
System.out.println("EVENT: SwipeLeft");
}
public void handleSwipeRight(SwipeEvent swipeEvent) {
System.out.println("EVENT: SwipeRight");
}
public void btnClick(ActionEvent actionEvent) {
System.out.println("EVENT: Click");
}
}
但是处理函数的代码永远不会被执行。 我们尝试了不同版本的 Java(8 和 11),结果是一样的。 我们设备上的触摸屏工作正常,我们使用了触摸设备和标准鼠标。都不起作用,而标准的 SWING/AWT 组件似乎可以很好地与两者一起工作。
谁知道可能是什么问题?或者有什么建议?
提前致谢。
*************************** 更新 2019 07 16 - 尝试使用 JVM 属性 ******** *******************
我们还使用 JVM 选项尝试了上面的示例:
-Dcom.sun.javafx.isEmbedded=true
-Dcom.sun.javafx.touch=true
没有影响...
*************** 更新 2019 09 23 - 尝试使用不同的操作系统和不同的技术 *************
我们做了更多的测试,在细节上我们发现:
- JavaFX Multitouch 事件在 Windows 中运行良好,但在 Linux 中运行不佳
- 多点触控事件在采用不同技术的 Linux 上运行良好。我们尝试使用 Qt Widgets 基本示例:Finger Paint Example
现在我们知道问题不在于设备或操作系统。我们无法使 JavaFX 多点触控事件在 Linux 上运行,有人有任何建议吗?
最佳答案
我在 Windows 7 中使用 MultiTouch 设备和鼠标尝试了该示例。 仅使用鼠标会生成 Click 事件。 使用触摸生成所有事件。
所以我猜你的触摸设备正在作为模拟鼠标工作,并且没有正确设置为充当真正的触摸设备。
使用 Java 8:
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
关于java - 如何在 Linux 上将 JavaFX 多点触控事件与 JFX Pane 组件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57040753/