在 eclipse java IDE 中使用 WindowBuilder 时,代码不会 运行 正确

Code won't run properly when using WindowBuilder in eclipse java IDE

我有一个由 3 个文件组成的代码:

LeapdListener.java
RS232Protocol.java
Leapd.java which contains the main(String args[])

此代码 运行 正确。

但是,我使用 WindowBuilder 添加了一个 GUI,并尝试通过按 window 中的按钮来 运行 代码。 我将 main(String args[]) 名称更改为 LeapMain(String st),然后我向 GUI 添加了一个 actionListener,这就是我得到的:

public void actionPerformed(ActionEvent arg0) {
Leapd.LeapMain(" ");
}

当我运行代码时,它只会初始化LeapdListener中的侦听器而不会做任何其他事情。

如何从 GUI 中正确 运行 代码? 谢谢

代码补充:

图形用户界面:

package com.leaptoarduino;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyGUI {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyGUI window = new MyGUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MyGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnCalibration = new JButton("Calibration");
        btnCalibration.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Leapd.LeapMain(" ");
            }
        });
        btnCalibration.setFont(new Font("Arial", Font.BOLD, 20));
        btnCalibration.setBounds(121, 24, 210, 38);
        frame.getContentPane().add(btnCalibration);
    }
}

Leapd:这是我将主要功能更改为 LeapMain

package com.leaptoarduino;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Gesture;

public class Leapd
{
    static Controller controller;
    static LeapdListener leap;

    //Main
    public static final void LeapMain(String st)
    {
        //Initialize serial communications
        RS232Protocol serial = new RS232Protocol();
        serial.connect("COM22");
        //Initialize the Leapduino listener
        leap = new LeapduinoListener(serial);
        controller = new Controller(leap);
        controller.addListener(leap);
        controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
        //Set up controller for gestures
        controller.config().setFloat("Gesture.KeyTap.MinDownVelocity", 5.0f);
        controller.config().setFloat("Gesture.KeyTap.HistorySeconds", .2f);
        controller.config().setFloat("Gesture.KeyTap.MinDistance", 25.0f);
        controller.config().save();     
    }


}

RS232协议:

package com.leaptoarduino;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class RS232Protocol 
{
// Serial port we're manipulating.
    private SerialPort port;
    // Class: RS232Listener
    public class RS232Listener implements SerialPortEventListener
    {
        public void serialEvent(SerialPortEvent event)
        {
            //Check if data is available
            if (event.isRXCHAR() && event.getEventValue() > 0)
            {
                try
                {
                    int bytesCount = event.getEventValue();
                    System.out.print(port.readString(bytesCount));
                }
                catch (SerialPortException e) { e.printStackTrace(); }
            }
        }
    }
    //Member function: connect
    public void connect(String newAddress)
    {
        try
        {
            //Set up connection
             port = new SerialPort(newAddress);
            //Open the new port and set its parameters
             port.openPort();
             port.setParams(9600, 8, 1, 0);
            //Attach our new event listener
             port.addEventListener(new RS232Listener());
        }
        catch (SerialPortException e) { e.printStackTrace(); }
    }
    //Member function: disconnect
    public void disconnect()
    {
        try { port.closePort(); }
        catch (SerialPortException e) { e.printStackTrace(); }
    }
    //Member function: write
    public void write(String text)
    {
        try { port.writeBytes(text.getBytes()); }
        catch (SerialPortException e) { e.printStackTrace(); }
    }
}

LeapdListener:

package com.leaptoarduino;
import com.leapmotion.leap.*;
import com.leapmotion.leap.Gesture.Type;
public class LeapdListener extends Listener
{

    boolean flag = true;
    //Serial port that we'll be using to communicate with the Arduino
    static RS232Protocol serial;
    //Constructor
    public LeapdListener(RS232Protocol serial)
    {
        this.serial = serial;
    }
    //Member function: onInit
    public void onInit(Controller controller)
    {
        System.out.println("Initialized");
    }
    //Member function: onConncect
    public void onConnect(Controller controller)
    {
       System.out.println("Connected");
    } 
    //Member Function: onDisconnect
    public void onDisconnect(Controller controller)
    {
        System.out.println("Disconnected");
    } 
    //Member Function: onExit
    public void onExit(Controller controller)
    {
       System.out.println("Exited");
    } 
    //Member Function: onFrame
    Frame lastFrameProcessed = Frame.invalid();
    public void onFrame(Controller controller)
    {
        //Get the most recent frame
        Frame frame = controller.frame();
        String tpFinger = " ";
        if (frame.hands().count() > 0)
        {
            GestureList gestures = frame.gestures(lastFrameProcessed);
            for (Gesture gesture: gestures)
            {
                //if (gesture.type() == Type.TYPE_KEY_TAP)
                //{
                    KeyTapGesture keyTap = new KeyTapGesture (gesture);
                    //System.out.println("STRINGTEST" + count);
                    //count++;
                    Pointable poker = keyTap.pointable();
                    if(poker.isFinger()){
                          Finger tappingFinger = new Finger(poker);
                          tpFinger = tappingFinger.type().toString();
                          System.out.println("Tapper: " + tpFinger);
                    }
                //}
            }

            //Give the Arduino some time to process our data
            //try { Thread.sleep(1); }
            //catch (InterruptedException e) { e.printStackTrace(); }
        }
        lastFrameProcessed = frame;
    }
}

我发现了问题 - 在 Leapd main class 中,"serial" 也应该是静态的。