有没有办法在 swing 应用程序中使用 JavaFX 触摸事件?目前我正在使用 JFXPanel 来捕获 JavaFX 事件,但是当我尝试获取事件时,我没有收到任何触摸事件,而只收到鼠标事件。这是在 Windows 8.1 戴尔触摸屏上进行测试的。
Updated:下面的代码是我用来获取事件的框架。此 JFXPanel 在 Swing 应用程序中用作玻璃面板。这会为 glasspane 创建一个 JFXPanel,它能够捕获所有事件。
public class MouseEventRouter extends JFXPanel {
...
public ZeusMouseEventRouter(JMenuBar menuBar, Container contentPane) {
...
this._contentPane = contentPane;
this._contentPane.add(_JFXpanel);
this._contentPane.setVisible(true);
init();
}
private void init() {
pane = new VBox();
pane.setAlignment(Pos.CENTER);
Platform.runLater(this::createScene);
}
private void createScene() {
Scene scene = new Scene(pane);
...
scene.setOnTouchPressed(new EventHandler<javafx.scene.input.TouchEvent>() {
@Override public void handle(javafx.scene.input.TouchEvent event) {
System.out.println("tap down detected");
}
});
...
setScene(scene);
}
}
FX 邮件列表上的这个问题 http://mail.openjdk.java.net/pipermail/openjfx-dev/2014-May/014008.html建议使用您所采用的方法是不可能的,您需要创建一个 JavaFX 阶段并使用 SwingNode(FX 中的 Swing)而不是 JFXPanel(Swing 中的 FX)嵌入您的 Swing 应用程序。
我没有任何支持触摸的硬件来测试这一点,但我希望以下内容能够工作......
public class TouchApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
JPanel swingContent = new JPanel();
swingContent.add(new JButton("Hello world"));
swingContent.add(new JScrollBar());
BorderPane content = new BorderPane();
SwingNode swingNode = new SwingNode();
swingNode.setContent(swingContent);
content.setCenter(swingNode);
Scene scene = new Scene(content);
scene.setOnTouchPressed((e) -> {
System.out.println(e);
});
primaryStage.setScene(scene);
primaryStage.show();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)