我开发了一个Swing应用程序,我现在需要实现一个自定义的多点触摸手势。
经过一些研究,最简单的方法似乎是使用JavaFX,因为它可以嵌入到Swing中并单独为触摸屏上的每个手指提供事件。然后,我尝试根据以下示例实现触摸处理:
http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm
http://docs.oracle.com/javafx/2/events/touch_events.htm
我决定在JPanel上面创建一个透明的JFXPanel来捕捉所有鼠标和触摸事件。它适用于JavaFX鼠标事件,但我没有收到任何触摸事件。
我在不同的可触摸Windows 7和Window 8设备上测试了我的应用程序。我目前正在使用Java JDK 1.8.0_45。
所以我的问题是 为什么我没有收到任何触摸事件?
我将我的问题减少到以下两个类:
GUI类:
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestWindow extends JFrame {
private static final long serialVersionUID = 1L;
private static final JFXPanel jfxPanel = new JFXPanel();
private static Scene jfxScene;
private static TestController controller;
private boolean enableJFXPanel;
/**
* Create the Frame
* @param tc The Application's controller
*/
public TestWindow(TestController tc) {
this.setBounds(200,200,400,400);
this.setResizable(false);
this.setTitle("JavaFX in Swing Test");
getContentPane().setLayout(null);
controller = tc;
// Create the swing panel
JPanel jPanel = new JPanel();
jPanel.setBounds(0, 0, this.getWidth(), this.getHeight());
jPanel.addMouseListener(controller);
// Create the button
JButton button = new JButton("show / hide JavaFX panel");
button.setBounds((jPanel.getWidth()/2)-100, (jPanel.getHeight()/2)-25, 200, 50);
button.addActionListener(controller);
jPanel.add(button);
// Create the JavaFX panel
jfxPanel.setBounds(0, 0, 0, 0);
enableJFXPanel = false;
jPanel.add(jfxPanel);
getContentPane().add(jPanel);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
this.setVisible(true);
}
/**
* Initialize and Show JavaFX
*/
private static void initAndShowGUI() {
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX();
}
});
}
/**
* Initialize the JavaFX {@code Scene} and set touch controller.
*/
private static void initFX() {
Group jfx_root = new Group();
jfxScene = new Scene(jfx_root);
jfxScene.setFill(Color.AQUA);
// Register touch event listener (DOESN'T WORK !!!)
jfxScene.setOnTouchMoved(controller);
jfxScene.setOnTouchPressed(controller);
jfxScene.setOnTouchReleased(controller);
jfxScene.setOnTouchStationary(controller);
// Test catching JavaFX mouse events (WORKS PERFECTLY !!!)
jfxScene.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
System.out.println("MOUSE CLICKED ON JAVA FX PANEL");
}
});
jfxPanel.setScene(jfxScene);
}
/**
* Show / hide the JavaFX panel
*/
public void switchJFXPanel() {
if (enableJFXPanel) {
jfxPanel.setBounds(0, 0, 0, 0);
enableJFXPanel = false;
} else {
jfxPanel.setBounds(0, 0, this.getWidth(), this.getHeight());
enableJFXPanel = true;
}
}
}
控制器类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javafx.scene.input.TouchEvent;
public class TestController implements MouseListener, ActionListener, javafx.event.EventHandler<TouchEvent> {
private TestWindow testWindow;
public TestController() {
testWindow = new TestWindow(this);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("MOUSE CLICKED ON JPANEL");
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void actionPerformed(ActionEvent e) {
testWindow.switchJFXPanel();
}
@Override
public void handle(TouchEvent event) {
// OCCURING JAVA FX TOUCH EVENT SHALL BE WRITTEN TO CONSOLE
System.out.println("JAVA FX TOUCH EVENT: " + event.toString());
}
public static void main(String arg[]) {
TestController test = new TestController();
}
}
感谢您的帮助。