demo:带界面的聊天程序
public class ChatFrame extends JFrame implements ActionListener {
JTextField tf;
JTextArea ta;
JScrollPane sp;
JButton send;
JPanel P;
int port;
String s="";
String myID;
Date date;
ServerSocket server;
Socket mySocket;
BufferedReader is;
PrintWriter os;
String line;
public ChatFrame(String ID, String remotelID, String IP, int port, boolean isServer, String remoteID){
super(ID);
myID=ID;
this.port=port;
ta=new JTextArea();
ta.setEditable(false);
sp=new JScrollPane(ta);
this.setSize(330,400);
this.setResizable(false);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception e){
System.out.println("UI error");
}
this.getContentPane().add(sp,"Center");
P=new JPanel();
this.getContentPane().add(P,"South");
send = new JButton("发送");
tf=new JTextField(20);
P.add(tf);
P.add(send);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
send.addActionListener(this);
tf.addActionListener(this);
if (isServer){
try {
server=null;
try {
server=new ServerSocket(port);
}catch (Exception e){
System.out.println("can not listen to:"+e);
}
mySocket=null;
try {
mySocket=server.accept();
}catch (Exception e){
System.out.println("Error."+e);
}
is=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
os=new PrintWriter(mySocket.getOutputStream());
}catch (Exception e){
System.out.println("Error:in server client socket"+e);
}
}//end of if
else{
try {
mySocket=new Socket(IP,port);
os=new PrintWriter(mySocket.getOutputStream());
is=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
}catch (Exception e){
System.out.println("Error:in client socket"+e);
}
while (true){
try {
line=is.readLine();
date=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime=formatter.format(date);
s+=currentTime+""+remoteID+"说:\n"+line+"\n";
ta.setText(s);
}catch (Exception e){
System.out.println("Error:in receive remote information"+e);
}
}
}
}
public ChatFrame(String cat, String dog, String s, int i, boolean b) {
}
@Override
public void actionPerformed(ActionEvent e) {
date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime=formatter.format(date);
s+=currentTime+""+myID+"说:\n"+tf.getText()+"\n";
ta.setText(s);
os.print(tf.getText());
os.flush();
tf.setText("");
tf.requestFocus();
}
}
public class ChatServerFrame {
public static void main(String[] args) {
ChatFrame cserver = new ChatFrame("Cat", "Dog", "127.0.0.1", 2019, true);
}
}
public class ChatClientFrame {
public static void main(String[] args) {
ChatFrame cclient = new ChatFrame("Dog", "Cat", "127.0.0.1", 2019, false);
}
}