繁体   English   中英

如何在 eclipse 中调用同一个项目的类

[英]How can I call the class on the same project in eclipse

我对 Java 编程很陌生。 我面临的问题是调用另一个类在我的主程序中运行。 我在同一个项目中有一个类名“rxtx.java”。 我需要将它调用到另一个类“Login.java”。下面是 rxtx.java 的代码:

`

 //this is the class i need to call to run
 public class rxtx implements SerialPortEventListener {
SerialPort serialPort;
    /** The port we're normally going to use. */
private static final String PORT_NAMES[] = { 
        "COM4", // Windows

};

/**
* A BufferedReader which will be fed by a InputStreamReader 
* converting the bytes into characters 
* making the displayed results codepage independent
*/

private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;
private usedata part12User = new usedata();
public void initialize() {

    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    //First, Find an instance of serial port as set in PORT_NAMES.
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier)`enter code here`  portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }
    }
    if (portId == null) {
        System.out.println("Could not find COM port.");
        return;
    }

    try {
        // open serial port, and use class name for the appName.
        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);

        // set port parameters
        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        // open the streams
        input = new BufferedReader(new    InputStreamReader(serialPort.getInputStream()));
        output = serialPort.getOutputStream();

        // add event listeners
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

/**
 * This should be called when you stop using the port.
 * This will prevent port locking on platforms like Linux.
 */
public synchronized void close() {
    if (serialPort != null) {
        serialPort.removeEventListener();
        serialPort.close();
    }
}

/**
 * Handle an event on the serial port. Read the data and print it.
 */
public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {

            String inputLine=input.readLine();
            String[] parts = inputLine.split(",");
            String part1 = parts[0]; 
            String part2 = parts[1];
            String part3 = parts[2];
            //System.out.print(part1);
            //System.out.print(" , ");
            //System.out.println(part2);
            part12User.usePart1Part2(part1,part2,part3);
            //System.out.println(data);

        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    // Ignore all the other eventTypes, but you should consider the other ones.
}

public static void main(String[] args) throws Exception {
    rxtx main = new rxtx();
    main.initialize();
    Thread t=new Thread() {
        public void run() {
            //the following line will keep this app alive for 1000 seconds,
            //waiting for events to occur and responding to them (printing incoming messages to console).
            try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
        }
    };
    t.start();
    System.out.println("Started");

}

}`

这是我需要“rxtx.java”在不同的类调用“Login.java”中运行的地方。

JButton btnStart = new JButton("Start");
    btnStart.setFont(new Font("Tahoma", Font.BOLD, 13));
    btnStart.setBackground(Color.LIGHT_GRAY);
    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
              // Here is where i wan to run the class rxtx.java
        }

在 Login.java 中导入 rxtx,然后您可以在 Login.java 中创建新的 rxtx 对象

例子:

这是我的项目设置

登录类代码:

package main;

import main.rxtx.Rxtx;

public class Login {

    public static void main(String[] args) {
        new Login(); //create a new main.Login object
    }

    public Login() {
        //create a new Rxtx object
        Rxtx rxtx = new Rxtx();
        rxtx.printA(); //call the printA method
    }
}

Rxtx 类代码:

package main.rxtx;

public class Rxtx {

    public void printA(){
        System.out.println("A");
    }

}

因为 Rxtx.java 在另一个包中,我必须告诉 Login.java Rxtx.class 在哪里,所以我用这个语句导入它:

import main.rxtx.Rxtx;

可能是如果您在项目中使用不同的包,请按照代码片段进行操作,

在 Java 中,您只能导入类Names静态方法/字段

导入类使用

import full.package.name.of.SomeClass;

导入方法/字段使用

import static full.package.name.of.SomeClass.staticMethod;
import static full.package.name.of.SomeClass.staticField;

例如:

import myPackage.rxtx;

class Login {
   rxtx rxtxObj = new rxtx();
   // code
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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