我有一个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上显示它。

我该怎么做 ?

最好的祝福,

豆腐

最佳答案

花一点时间阅读有关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重写为一个简单的类。读取时它将使程序无响应,这就是为什么您有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/

10-13 04:17