[英]Retrieve GPS DATA on a JTextArea with 2 different class
I have aa GPS receptor. 我有一个GPS接收器。 I create a class to retrieve all the GPS data on my Eclipse Console.
我创建一个类来检索我的Eclipse控制台上的所有GPS数据。 (This is the code of makia42)
(这是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();
}
}
I have another class which is a frame containing a button and a JTextArea. 我还有另一个类,它是一个包含按钮和JTextArea的框架。 This class is in communication with my first class COM.
此类与我的头等舱COM通信。
When i click the button, COM is starting and show me the data in my Eclipse Console. 当我单击按钮时,COM正在启动,并在Eclipse控制台中向我显示数据。 But now, I'd like to show it on my JTextArea.
但是现在,我想在我的JTextArea上显示它。
How can I do it ? 我该怎么做 ?
Best regards, 最好的祝福,
Tofuw 豆腐
Take a moment to read about this pattern . 花点时间阅读一下这种模式 。 Make the
Thread
a Subject
. 使
Thread
成为Subject
。 Before starting register the instance of the class that contains the JTextArea
as the Observer
with the instance of the Thread
. 在开始注册之前,将包含
JTextArea
的类的实例注册为Thread
的实例作为Observer
。 At the run()
instead of printing on the console, use the notify(String)
; 在
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);
}
}
Change to 改成
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.
}
}
EDIT: I suppose you can rewrite the Thread
to a simple class. 编辑:我想您可以将
Thread
重写为一个简单的类。 It will render the program unresponsive while it reads, that's why you have a Thread
. 它将使程序在读取时无响应,这就是为什么您拥有
Thread
的原因。 I suppose you can implement a cleaner way using Future<String>
我想您可以使用
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.