gpt4 book ai didi

java - 检索具有 2 个不同类的 JTextArea 上的 GPS 数据

转载 作者:行者123 更新时间:2023-12-01 13:48:58 25 4
gpt4 key购买 nike

我有一个 GPS 接收器。我创建一个类来检索 Eclipse 控制台上的所有 GPS 数据。(这是makia42的代码)

public class COM implements Runnable{
static Thread myThread=null;
static BufferedReader br;
static BufferedWriter wr;
static InputStreamReader isr;
static OutputStreamWriter osw;
static java.io.RandomAccessFile port;

public COM(){ /**Constructeur*/
myThread=new Thread(this);
}

public void start(){
try {
port=new java.io.RandomAccessFile("COM3","rwd");
port.writeBytes("\r\n");
port.writeBytes("c,31,0,0,5\r\n");
port.writeBytes("T,1000,1\r\n");
}
catch (Exception e) {
System.out.println("start "+e.toString());
}
myThread.start();
}


public void run() {
System.out.println("lecture COM...");
for(;;){
String st = null;
try {
st=port.readLine();
} catch (IOException e) {System.out.println(e.getMessage());}
System.out.println(st);
}
}
public static void main(String[] args) {

COM temp= new COM();
temp.start();
}
}

我有另一个类,它是一个包含按钮和 JTextArea 的框架。该类与我的第一类 COM 进行通信。

当我单击该按钮时,COM 将启动并在 Eclipse 控制台中显示数据。但现在,我想在我的 JTextArea 上显示它。

我该怎么做?

最诚挚的问候,

bean 腐

最佳答案

花点时间阅读 this pattern 。使 Thread一个Subject 。在开始注册包含 JTextArea 的类的实例之前作为ObserverThread 为例。在run()使用 notify(String) 而不是在控制台上打印;

public void run() {
System.out.println("lecture COM...");
for(;;){
String st = null;
try {
st=port.readLine();
} catch (IOException e) {System.out.println(e.getMessage());}
System.out.println(st);
}
}

更改为

public void run() {
System.out.println("lecture COM...");
for(;;){
String st = null;
try {
st=port.readLine();
} catch (IOException e) {System.out.println(e.getMessage());}
notifyObservers(st); //Pass the data to the observers.
}
}

编辑:我想你可以重写 Thread到一个简单的类(class)。它会使程序在读取时无响应,这就是为什么你有 Thread 。我想你可以使用 Future<String> 实现更干净的方式

public class GpsReader {
public class GenericGPSException extends Exception {

public GenericGPSException(String message, Throwable cause) {
super(message, cause);
}
}

public static void main(String[] args) {

// Example of usage

GpsReader gpsReader = new GpsReader();

String messageFromDevice;
try {
// Try read it
messageFromDevice = gpsReader.getCoordinate();
} catch (GenericGPSException e) {
// Error, what does it says?
messageFromDevice = e.getMessage();
}

JTextArea mockArea = new JTextArea();
// Show to user anything that comes to it.
mockArea.setText(messageFromDevice);

}

private boolean isReady;

private RandomAccessFile port;

public GpsReader() {
}

public String getCoordinate() throws GenericGPSException {

if (!isReady) {
try {
port = new RandomAccessFile("COM3", "rwd");
port.writeBytes("\r\n");
port.writeBytes("c,31,0,0,5\r\n");
port.writeBytes("T,1000,1\r\n");
isReady = true;
} catch (FileNotFoundException e) {
throw new GenericGPSException(
"Error at starting communication to Device ", e);
} catch (IOException e) {
throw new GenericGPSException(
"Error at starting communication to Device ", e);
}

}

try {
return port.readLine();
} catch (IOException e) {
throw new GenericGPSException("Error at reading the Device ", e);
}
}
}

关于java - 检索具有 2 个不同类的 JTextArea 上的 GPS 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20119040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com