java.net.* 界面聊天程序.请大家帮帮助
这个是客户端的程序:
package as;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.Socket;
import java.net.
UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CharClientText extends JFrame implements ActionListener{
/**
* @param args
*/
JLabel jl = new JLabel( "输入您要说的话 ");
JPanel jp = new JPanel();
JTextField jtf = new JTextField(15);
JTextArea jta = new JTextArea(10,10);
Socket sk=null;
BufferedReader br;
BufferedWriter bof;
PrintWriter pw;
public CharClientText()
{
super( "Client 端 ");
Container con = getContentPane();
jp.add(jl);
jp.add(jtf);
con.add(jp,BorderLayout.SOUTH);
con.add(jta,BorderLayout.CENTER);
jtf.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setSize(300,250);
show();
try
{
Socket sk = new Socket( "localhost ",9000);
jta.append( "已连接到服务器 "+sk.getInetAddress().getHostName()+ "\n ");
pw = new PrintWriter(sk.getOutputStream(),true);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent arg0)
{
try {
String str = jtf.getText();
jtf.setText( " ");
pw.write(str);
jta.append( "Client: "+str+ "\n ");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
new CharClientText();