图形用户界面聊天室 客户端和服务器
服务器代码:
package netprogramme;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class ChatServer extends JFrame implements ActionListener{
ServerSocket server;
Socket theClient;
boolean done;
JButton send;
JTextArea chatContent;
JTextField sentence;
DataInputStream in=null;
DataOutputStream out=null;
public ChatServer(){
buildGUI("聊天室--服务器端");
try {
server=new ServerSocket(2011);
} catch (IOException e) {
System.out.println(e);
}
while(true){
try {
theClient=server.accept();
out=new DataOutputStream(theClient.getOutputStream());
in=new DataInputStream(theClient.getInputStream());
done=true;
String line=null;
while(done){
while((line=in.readUTF())!=null){
chatContent.append("对方:"+line+"\n");
}
in.close();
1
out.close();
theClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void buildGUI(String title) {
this.setTitle(title);
this.setSize(400,300);
Container container=this.getContentPane();
container.setLayout(new BorderLayout());
JScrollPane centerPanel=new JScrollPane();
chatContent=new JTextArea();
centerPanel.setViewportView(chatContent);
container.add(centerPanel,BorderLayout.CENTER);
chatContent.setEditable(false);
JPanel bottomPanel=new JPanel();
sentence=new JTextField(20);
send=new JButton("发送");
bottomPanel.add(new JLabel("聊天信息"));
bottomPanel.add(sentence);
bottomPanel.add(send);
container.add(bottomPanel,BorderLayout.SOUTH);
send.addActionListener(this);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
out.writeUTF("bye");
}catch(IOException e1){
}finally{
System.exit(0);
System.out.println("服务器端窗口关闭……");
}
}
});
}
public void actionPerformed(ActionEvent e){
String str=sentence.getText();
if(str!=null&&!str.equals("")){
2
chatContent.append("本人"+str+"\n");
try{
out.writeUTF(sentence.getText());
}catch(Exception e1){
chatContent.append("客户不在……\n");
}
}
else{
chatContent.append("聊天信息不能为空\n");
}
sentence.setText("");
}
public static void main(String[] args) {
new ChatServer();
}
}
客户端代码:
package netprogramme;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChatClient extends JFrame implements ActionListener{
Socket client;
boolean done;
JButton send;
JTextArea chatContent;
JTextField sentence;
DataInputStream in=null;
DataOutputStream out=null;
public ChatClient(){
buildGUI("聊天室--客户端");
try {
client=new Socket("localhost",2011);
in=new DataInputStream(client.getInputStream());
out=new DataOutputStream(client.getOutputStream());
done=false;
String line=null;
while(true){
3
while((line=in.readUTF())!=null){
chatContent.append("对方:"+line+"\n");
if(line.equals("bye")){
String msg="服务器发来结束通信命令!\n";
msg+="系统将在您确认此对话框8秒后关闭,\n请及时保存聊天内
容!";
JOptionPane.showMessageDialog(this, msg);
try {
Thread.sleep(8000);
done=true;
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
in.close();
out.close();
System.exit(0);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void buildGUI(String title) {
this.setTitle(title);
this.setSize(400,300);
Container container=this.getContentPane();
container.setLayout(new BorderLayout());
JScrollPane centerPanel=new JScrollPane();
chatContent=new JTextArea();
centerPanel.setViewportView(chatContent);
container.add(centerPanel,BorderLayout.CENTER);
chatContent.setEditable(false);
JPanel bottomPanel=new JPanel();
sentence=new JTextField(20);
send=new JButton("发送");
bottomPanel.add(new JLabel("聊天信息"));
bottomPanel.add(sentence);
bottomPanel.add(send);
container.add(bottomPanel,BorderLayout.SOUTH);
4
send.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ChatClient();
}
public void actionPerformed(ActionEvent e) {
String str=sentence.getText();
if(str!=null&&!str.equals("")){
chatContent.append("本人"+str+"\n");
try{
out.writeUTF(sentence.getText());
}catch(Exception e1){
chatContent.append("服务器没有启动……\n");
}
}
else{
}
}
chatContent.append("聊天信息不能为空\n");
}
sentence.setText("");
5