简体   繁体   中英

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

I have a code made from 3 files:

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

This code runs properly.

However, I added a GUI using WindowBuilder and tried to run the code by pressing a button in the window. I changed the main(String args[]) name to LeapMain(String st) and then I added an actionListener to the GUI and that's what I got:

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

When I run the code, it will only initialize the listener in LeapdListener and do nothing else.

How can I run the code properly from within the GUI? Thanks

Code addition:

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: This is where I changed the main function to 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;
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM