Swing的Leap Motion问题?

时间:2013-09-12 12:45:50

标签: java swing leap-motion

我开始玩Leap Motion控制器并在Swing中制作一个小GUI。这意味着它只是一个带有标签的框架,它应该显示Leap Motion看到的文本。不幸的是,我的程序在两秒后就崩溃了。我没有例外或类似的东西。这是我的代码:

public class GUI extends JFrame{
private static final long serialVersionUID = 6411499808530678723L;

public JLabel label;

public GUI(){
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.label = new JLabel("Waiting for Gestures");
    this.add(label);

    setVisible(true);
}

public class LeapListener extends Listener{
    JLabel label;

    LeapListener(JLabel label){
        this.label = label;
    }

    @Override
    public void onInit(Controller controller){

    }

    @Override
    public void onExit(Controller controller){

    }

    @Override
    public void onConnect(Controller controller){
        System.out.println("bin da");
        controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
        controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
        controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
        controller.enableGesture(Gesture.Type.TYPE_SWIPE);
    }

    @Override
    public void onDisconnect(Controller controller){

    }

    @Override
    public void onFrame(Controller controller){
        Frame frame = controller.frame();

        GestureList glist = frame.gestures();

        for (int i = 0; i < glist.count(); i++){
            Gesture g = glist.get(i);

            switch (g.type()){
            case TYPE_CIRCLE:
                CircleGesture circle = new CircleGesture(g);
                this.label.setText("Mach mal nen Kreis!");
            case TYPE_KEY_TAP:
                KeyTapGesture key = new KeyTapGesture(g);
                this.label.setText("Schreiben?");
            case TYPE_SCREEN_TAP:
                ScreenTapGesture screen = new ScreenTapGesture(g);
                this.label.setText("Klicken?");
            case TYPE_SWIPE:
                SwipeGesture swipe = new SwipeGesture(g);
                this.label.setText("Da wurde gewischt!");
            default:
                System.out.println("nothing!");
            break;
            }
        }
    }
}

public static void main(String[] args) {
    GUI gui = new GUI();
    LeapListener listener;
    listener = gui.new LeapListener(gui.label);
    Controller controller = new Controller();
    controller.addListener(listener);

}

}

我不知道我做错了什么。 Windows-Error-Message表示Java Platform SE二进制文件已关闭(javaw.exe)。 Errormodule:Leap.dll 这是我犯过的错误,还是我整个设置的错误?

1 个答案:

答案 0 :(得分:0)

好的,我自己找到了一个解决方案,但不幸的是我无法解释它 我必须在类声明中创建Controller对象,而不是在main方法中。然后我必须在GUI的构造函数中将侦听器添加到控制器中,如下所示:

public class GUI extends JFrame{
private static final long serialVersionUID = 6411499808530678723L;

public JLabel label;
public Controller c = new Controller();
public LeapListener listener;

public GUI(){
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.label = new JLabel("Waiting for Gestures");
    this.add(label);
    listener = new LeapListener(this.label);
    c.addListener(listener);
    setVisible(true);
}
    ...
}

所以,我很高兴我得到了它的工作,但也许有人对这个问题很感兴趣,并且可能找到我遇到错误的原因。