在eclipse java IDE中使用WindowBuilder时,代码将无法正常运行

时间:2015-08-10 06:24:48

标签: java eclipse windowbuilder

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

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

此代码正常运行。

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

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

当我运行代码时,它只会在LeapdListener中初始化侦听器而不执行任何其他操作。

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

代码添加:

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();     
    }


}

RS232Protocol:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了问题 - 在Leapd主类中,“serial”也应该是静态的。