如果我从另一个类启动线程,如何在run方法中显示javaapplet?

如果我从另一个类启动线程,如何在run方法中显示javaapplet?,java,multithreading,applet,Java,Multithreading,Applet,当我运行以下代码时,小程序查看器出现,串行连接工作,但当我在另一个类中创建此线程并启动它时,小程序查看器不会出现,但串行连接工作 package parkingStatus; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import java.applet.Applet; import java.awt.Canvas; import java.awt.Co

当我运行以下代码时,小程序查看器出现,串行连接工作,但当我在另一个类中创建此线程并启动它时,小程序查看器不会出现,但串行连接工作

package parkingStatus;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;

import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;


public class lifeDisplay extends Applet implements Runnable {

    SerialPort serialPort; 
    private static final String PORT_NAMES[] = {"COM20"};
    private BufferedReader input;
    private OutputStream output = null;
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 9600;
    String ParkingStatus = "";
    Image backGround;

    public lifeDisplay (){}

    public void run() {
        repaint();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while (true){
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        //String ParkingStatus = "";
        repaint();
        ParkingStatus = initialize("2", "2");//to serial // in the uc 0 = enter, 1 = exit, 2 = life display
        System.out.println(ParkingStatus);
        System.out.println ("========================");


        for (int i = 0; i < 5000; i++){}
        }
    }

    public String initialize(String num1, String num2) {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();


        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) 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 null; 
        }
        String Status = "";
        try {

            serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);  
            serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();

            Thread.sleep(1000);
            output.write(num1.getBytes()); 
            Thread.sleep(1000);


            System.out.println("Life display of the parking lots");
            String convert = "";
            int lots;


            while (true){
                lots = input.read() - '0';
                if (lots == 9)
                    break;
                convert = Integer.toString(lots);
                Status += convert;  
            }



            serialPort.removeEventListener();
            serialPort.close();
            //serialPort.addEventListener(this);
            //serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        return Status;
    }

    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }

    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                String inputLine=input.readLine();
                System.out.println(inputLine);
            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }

    }

    ///////////////////////applet/////////////////////////////
    Thread t;
    public void init() {
        setSize(480, 600);
        System.out.println("in init");
        t = new Thread(this);
        t.start();

    }

    /*public void start() {
        System.out.println("in start");
        Thread t;
        t = new Thread(this);
        t.start();
    }*/

    public void paint(Graphics g) {
        System.out.println("in paint");
        super.paint(g);
        backGround = getImage(getCodeBase(), "park.png");
        g.drawImage( backGround, 0, 0, (int)getBounds().getWidth(), (int)getBounds().getHeight(), this);
        String first = "1111100000";

        if (first.charAt(4) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,56,63,50);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("5",370 , 90);

        if (first.charAt(3) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,108,63,50);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("4",370 , 140);

        if (first.charAt(2) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,160,63,47);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("3",370 , 190);

        if (first.charAt(1) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,377,63,50);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("2",370 , 410);

        if (first.charAt(0) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(345,429,63,50);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("1",370 , 460);

        if (first.charAt(8) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,440,63,47);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("9",95 , 471);


        if (first.charAt(9) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,489,63,42);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("10",88 , 520);

        if (first.charAt(7) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,244,63,49);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("8",95 , 276);

        if (first.charAt(6) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,196,63,45);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("7",95 , 225);

        if (first.charAt(5) == '0')
            g.setColor(Color.green); //GREEN
        else 
            g.setColor(Color.red); //red
        g.fillRect(72,144,63,49);
        g.setColor(Color.black);
        g.setFont(new Font("Times", Font.BOLD, 20));
        g.drawString("6",95 , 177);
    }


}
我正在尝试使用多线程

有什么问题

1)为什么要编写小程序?如果是由于老师的特殊要求,请参考。2) 为什么是AWT而不是Swing?关于放弃使用AWT组件的许多好理由,请参见我的答案。。。。3) 不要阻止EDT(事件调度线程)-发生这种情况时,GUI将“冻结”。不要调用
Thread.sleep(n)
为重复任务执行Swing
计时器,或为长时间运行的任务执行
SwingWorker
。有关更多详细信息,请参阅。
lifeDisplay display = new lifeDisplay ();
Thread parkingLots = new Thread (display);
parkingLots.start();