二个界面,能够实现互相通信就行了,谢谢了,急用import java.awt.*;
import java.awt.event.*;
class A
{
String str="";
String stt="";
public synchronized void get(TextArea td,String stb)
{
this.str=str;
this.stt=stb;
td.append("接收成功"+this.stt+"\n");
try
{
this.notify();
this.wait();
}
catch(Exception e){}
}
public synchronized void play(TextArea ta)
{
ta.append("接收成功:"+this.str+"\n");
try
{
this.notify();
this.wait();
}
catch(Exception e){}
}}class T1 extends Thread implements ActionListener
{
Frame f;
Button b1;
TextArea ta;
TextField tf;
Panel p;
boolean fat=false;
A a;
T1(A k)
{
this.a=k;
f=new Frame("聊天程序");
b1=new Button("发送");
ta=new TextArea();
tf=new TextField(20);
p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER));
p.add(tf);
p.add(b1);
f.add(ta,"Center");
f.add(p,"South");
f.setSize(400,400);
f.setResizable(false);
f.setVisible(true);
b1.addActionListener(this);
}
public synchronized void run()
{
while(true)
{
try
{
if(fat==true)
{
a.get(ta,tf.getText());
fat=false;
}
}
catch(Exception e){}
}
} /**
 * Method actionPerformed
 *
 *
 * @param e
 *
 */
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
fat=true;
}
}}
class T2 extends Thread implements ActionListener
{
Frame f;
Button b2;
TextArea tw;
TextField tf;
Panel p;
boolean fal=false;
A a;
T2(A k)
{
this.a=k;
f=new Frame("接收");
b2=new Button("发送");
tw=new TextArea();
tf=new TextField(20);
p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER));
p.add(tf);
p.add(b2);
f.add(tw,"Center");
f.add(p,"South");
f.setSize(400,400);
f.setResizable(false);
f.setVisible(true);
b2.addActionListener(this);
}
public synchronized void run()
{
while(true)
{
try
{
if(fal=true)
{
a.get(tw,tf.getText());

fal=false;
}

}
catch(Exception e){}
}
}

public void actionPerformed(ActionEvent ee)
{
if(ee.getSource()==b2)
{
fal=true;
}
}
}
public class Chat
{
public static void main(String[] args)
{
A kk=new A();
T1 my=new T1(kk);
T2 my1=new T2(kk);
my.start();
my1.start();

}
}

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    class A
    {
    public static String str="";
    public static String stt="";
    public synchronized void get(TextArea td,String stb)
    {
    this.str=str;
    this.stt=stb;
    td.append("接收成功\n"+A.stt+"\n");
    try
    {
    this.notify();
    this.wait();
    }
    catch(Exception e){}
    }
    public synchronized void play(TextArea ta)
    {
    ta.append("接收成功:\n"+A.str+"\n");
    try
    {
    this.notify();
    this.wait();
    }
    catch(Exception e){}
    }}class T1 extends Thread implements ActionListener
    {
    Frame f;
    Button b1;
    TextArea ta;
    TextField tf;
    Panel p;
    public static boolean fat=false;
    A a;

    T1(A k)
    {
    this.a=k;
    f=new Frame("聊天程序");
    b1=new Button("发送");
    ta=new TextArea();
    tf=new TextField(20);
    p=new Panel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER));
    p.add(tf);
    p.add(b1);
    f.add(ta,"Center");
    f.add(p,"South");
    f.setSize(400,400);
    f.setResizable(false);
    f.setVisible(true);
    b1.addActionListener(this);
    }
    public synchronized void run()
    {
    while(true)
    {
    try
    {
    if(fat)
    {
    a.get(ta,tf.getText());
    fat=false;
    }
    }
    catch(Exception e){}
    }
    } /**
     * Method actionPerformed
     *
     *
     * @param e
     *
     */
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b1)
    {
    T2.fal=true;
    }
    }}
    class T2 extends Thread implements ActionListener
    {
    Frame f;
    Button b2;
    TextArea tw;
    TextField tf;
    Panel p;
    public static boolean fal=false;
    A a;
    T2(A k)
    {
    this.a=k;
    f=new Frame("接收");
    b2=new Button("发送");
    tw=new TextArea();
    tf=new TextField(20);
    p=new Panel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER));
    p.add(tf);
    p.add(b2);
    f.add(tw,"Center");
    f.add(p,"South");
    f.setSize(400,400);
    f.setResizable(false);
    f.setVisible(true);
    b2.addActionListener(this);
    }
    public synchronized void run()
    {
    while(true)
    {
    try
    {
    if(fal)
    {
    a.get(tw,tf.getText());

    fal=false;
    }

    }
    catch(Exception e){}
    }
    }

    public void actionPerformed(ActionEvent ee)
    {
    if(ee.getSource()==b2)
    {
    T1.fat=true;
    }
    }
    }
    public class Chat
    {
    public static void main(String[] args)
    {
    A kk=new A();
    T1 my=new T1(kk);
    T2 my1=new T2(kk);
    my.start();
    my1.start();

    }
    }
      

  2.   

    呵呵,这样就完全可以了.import java.awt.*;
    import java.awt.event.*;
    class A
    {
    //public static String str="";
    public static String stt="";
    public synchronized void get(TextArea td,String stb)
    {
    //this.str=str;
    A.stt=stb;
    td.append("接收成功\n"+A.stt+"\n");
    try
    {
    this.notify();
    //this.wait();
    }
    catch(Exception e){}
    }
    public synchronized void play(TextArea ta)
    {
    ta.append("接收成功:\n"+A.stt+"\n");
    try
    {
    this.notify();
    //this.wait();
    }
    catch(Exception e){}
    }}class T1 extends Thread implements ActionListener
    {
    Frame f;
    Button b1;
    TextArea ta;
    public static TextField tf;
    Panel p;
    public static boolean fat=false;
    A a;T1(A k)
    {
    this.a=k;
    f=new Frame("聊天程序");
    b1=new Button("发送");
    ta=new TextArea();
    tf=new TextField(20);
    p=new Panel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER));
    p.add(tf);
    p.add(b1);
    f.add(ta,"Center");
    f.add(p,"South");
    f.setSize(400,400);
    f.setResizable(false);
    f.setVisible(true);
    b1.addActionListener(this);
    }
    public synchronized void run()
    {
    while(true)
    {
    try
    {
    if(fat)
    {
    a.get(ta,T2.tf.getText());
    fat=false;
    }
    }
    catch(Exception e){}
    }
    }/**
     * Method actionPerformed
     *
     *
     * @param e
     *
     */
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b1)
    {
    T2.fal=true;
    }
    }}
    class T2 extends Thread implements ActionListener
    {
    Frame f;
    Button b2;
    TextArea tw;
    public static TextField tf;
    Panel p;
    public static boolean fal=false;
    A a;
    T2(A k)
    {
    this.a=k;
    f=new Frame("接收");
    b2=new Button("发送");
    tw=new TextArea();
    tf=new TextField(20);
    p=new Panel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER));
    p.add(tf);
    p.add(b2);
    f.add(tw,"Center");
    f.add(p,"South");
    f.setSize(400,400);
    f.setResizable(false);
    f.setVisible(true);
    b2.addActionListener(this);
    }
    public synchronized void run()
    {
    while(true)
    {
    try
    {
    if(fal)
    {
    a.get(tw,T1.tf.getText());fal=false;
    }}
    catch(Exception e){}
    }
    }public void actionPerformed(ActionEvent ee)
    {
    if(ee.getSource()==b2)
    {
    T1.fat=true;
    }
    }
    }
    public class Chat
    {
    public static void main(String[] args)
    {
    A kk=new A();
    T1 my=new T1(kk);
    T2 my1=new T2(kk);
    my.start();
    my1.start();}
    }