我是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
}
答案 0 :(得分:1)
在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在另一个包中,我必须告诉Rxtx.class所在的Login.java,所以我用这个语句导入它:
import main.rxtx.Rxtx;
答案 1 :(得分:0)
如果您在项目中使用不同的包,可能会按照代码段
进行操作在Java中,您只能导入类名称或静态方法/字段。
导入课程使用
select count(distinct value1) from t;
导入方法/字段使用
import full.package.name.of.SomeClass;
例如:
import static full.package.name.of.SomeClass.staticMethod;
import static full.package.name.of.SomeClass.staticField;