[英]Code won't run properly when using WindowBuilder in eclipse java IDE
I have a code made from 3 files: 我有一个由3个文件组成的代码:
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. 但是,我使用WindowBuilder添加了GUI,并尝试通过按窗口中的按钮来运行代码。 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:
我将main(String args [])名称更改为LeapMain(String st),然后在GUI中添加了一个actionListener,这就是我得到的:
public void actionPerformed(ActionEvent arg0) {
Leapd.LeapMain(" ");
}
When I run the code, it will only initialize the listener in LeapdListener and do nothing else. 当我运行代码时,它将仅在LeapdListener中初始化侦听器,并且不执行其他任何操作。
How can I run the code properly from within the GUI? 如何在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 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: 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: 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主类中,“序列”也应该是静态的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.