需要你稍稍了解设计模式的知道,太乱了,杂交了。

解决方案 »

  1.   

    我想你的目的是通过点击按钮来启动线程吧,于是就改了一下你的代码!import java.awt.*;
    import java.lang.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.*;public class Demo extends Applet implements Runnable,ActionListener{
    TextArea t1;
    TextField tf1;
    Button b1;
    Thread tr1;
    Thread tr2;
    Panel p1;
    public Demo() {
    t1=new TextArea();
    p1=new Panel();
    tf1=new TextField();
    b1=new Button("发言");
    p1.setLayout(new FlowLayout());
    p1.add(tf1);
    p1.add(b1);
    setLayout(new BorderLayout());
    add(t1,BorderLayout.CENTER);
    add(p1,BorderLayout.NORTH); 
    b1.addActionListener(this);
    setVisible(true);
    }
    public void init() { 

     tr1=new Thread(this,"线程1");
     tr2=new Thread(this,"线程2");
    //  tr1.start();
    //  tr2.start();
    }
    //run方法
    public void run() {
    printMsg("你好");
    } public synchronized void printMsg(String message) {
    t1.append(message+"\n");
    }

    public void actionPerformed(ActionEvent e){
    tr1.start();
    tr2.start();
    JOptionPane.showMessageDialog(null,"线程已启动","消息",JOptionPane.ERROR_MESSAGE);

    }
    }