Javax Swing在单独的类

时间:2017-01-17 21:01:28

标签: java swing jtextarea

我正在尝试打印到另一个类的TextArea,我得到一个NullPointerException。

以下是我写给textarea的代码

public class basicinterface extends intGui implements SerialPortEventListener {

/**
 * Stream for the storage of incoming data
 */
private InputStream inputStream;
/**
 * Stream for the dispatching of data
 */
private OutputStream outputStream;
/**
 * Timeout of the USB port
 */
private final int PORT_TIMEOUT = 2000;
/**
 * Representation of the serial port used for the communication
 */
private SerialPort serialPort;
/**
 * Buffer that stores the received bytes from the media
 */
protected LinkedBlockingQueue<Byte> receivedBytes;

/**
 * Builds a new manager for the communication via USB port.
 * @exception IOException if an error occurred during the opening of the USB port
 */
public basicinterface() throws IOException {
  receivedBytes = new LinkedBlockingQueue<Byte>(100000);
    String port = "COM8"; //place the right COM port here, OS dependent

    //Check that the USB port exists and is recognized:
    Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
    boolean portFound = false;
    CommPortIdentifier portId = null;
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            intGui.termoutput.setText(portId.getName());
            if (portId.getName().equals(port)) {
                intGui.termoutput.setText("Found port: " + port);
                portFound = true;
                break;
            } 
        } 
    } 

    if (!portFound) 
        throw new IOException("port " + port + " not found.");

    try {
        intGui.termoutput.setText("USB port opening...");
        serialPort = (SerialPort) portId.open("USBCommunicator", PORT_TIMEOUT);
        intGui.termoutput.setText("USB port opened");
        inputStream = serialPort.getInputStream();
        outputStream = serialPort.getOutputStream();
        serialPort.addEventListener(this);
                serialPort.notifyOnDataAvailable(true);
        //#==================================================================#
        // WARNING! - DO NOT SET THE FOLLOWING PROPERTY WITH RXTX LIBRARY, IT
        //            CAUSES A PROGRAM LOCK:
        //  serialPort.notifyOnOutputEmpty(true);
        //#==================================================================#

        //wait for a while to leave the time to javax.comm to
        //correctly configure the port:
        Thread.sleep(1000);

        int baudRate = 115200; //set propertly
        serialPort.setSerialPortParams(baudRate, 
            SerialPort.DATABITS_8, 
            SerialPort.STOPBITS_1, 
            SerialPort.PARITY_NONE);

        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

        intGui.termoutput.setText("setted SerialPortParams");
    } catch (Exception e) {
        System.err.println(e.getMessage());
        throw new IOException(e.getMessage());
    }
}


public void closeUSB(){
    //close the streams for serial port:
    try {
        inputStream.close();
        outputStream.close();
    } catch (IOException e) {
        System.err.printf("Cannot close streams:" + e.getMessage(), e);
    }
}

/**
 * Listener for USB events
 * 
 * @param event new event occurred on the USB port
 */
public void serialEvent(SerialPortEvent event){
    switch (event.getEventType()) {

        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            //nothing to do...
            break;

        case SerialPortEvent.DATA_AVAILABLE:
            byte received = -1;
            do {
                try {
                    received = (byte)inputStream.read();
                } catch (IOException e) {
                    System.err.println("Error reading USB:" + e.getMessage());
                }

                synchronized (receivedBytes) {
                    try {
                        receivedBytes.add(received);
                    } catch (IllegalStateException ew) {
                        System.err.println(ew.getMessage());
                        receivedBytes.poll();
                        receivedBytes.add(received);
                    }
                }
            } while(received != -1);

            break;
    }
}

protected void write(byte[] buffer){
    try {
        outputStream.write(buffer);
        outputStream.flush();
    } catch (IOException e) {
        System.err.println("Cannot write:" + e.getMessage());
    }
}
public static void main(String[] args) {
    Thread thread1 = new Thread() {
        public void run() {
            try {
                new basicinterface();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    Thread thread2 = new Thread() {
        public void run() {
            new intGui();
        }
    };

    // Start the downloads.
    thread1.start();
    thread2.start();

    // Wait for them both to finish
    try {
        thread1.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        thread2.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();    }
}
}

这是我的JTextArea代码

    public class intGui  {
    public static JTextArea termoutput;
    public static JTextField baudrate;
    public static void initialgui() {
        JFrame frame = new JFrame();
        frame.setSize(600,600);
        frame.setVisible(true);

        JPanel panel = new JPanel();
        frame.add(panel);
        panel.setLayout(new GridLayout(2,3));

        baudrate = new JTextField("9600");
        termoutput = new JTextArea("Testing");
        panel.add(baudrate);
    }

    public static void main(String[] args) {

    initialgui();
    }
}

我对这个问题从哪里开始有点迷茫。

0 个答案:

没有答案