package chap10;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class MyClient extends JFrame implements ActionListener {
JPanel jpLeft=new JPanel();
JPanel jpRight=new JPanel();
JPanel jpLup=new JPanel();
JPanel jpLdown=new JPanel();
JPanel jpLdownUp=new JPanel();
JPanel jpLdownDown=new JPanel();
JTextArea allMsg=new JTextArea();
JTextArea inputMsg=new JTextArea();
JList member=new JList();
JButton send=new JButton("发送");
JButton close=new JButton("关闭");
private static int width=600;
private static int height=600;
//用于关联JList,方便增加、删除操作
private DefaultListModel listModel=new DefaultListModel();
//客户端设置
private String server="127.0.0.1";
private int port=8000;
private DataInputStream din;
private DataOutputStream dout;
public static void main(String args[]){
MyClient mc=new MyClient("群聊");
mc.setSize(width, height);
mc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mc.setVisible(true);
}
MyClient(String caption){
super(caption);
this.add(jpRight,BorderLayout.EAST);
this.add(jpLeft,BorderLayout.CENTER);
jpLeft.setLayout(new GridLayout(2,1));
// 消息显示区
jpLeft.add(jpLup);
jpLup.setLayout(new BorderLayout());
TitledBorder tb1=new TitledBorder("消息显示区");
jpLup.setBorder(tb1);
JScrollPane spAllMsg=new JScrollPane();
spAllMsg.setViewportView(allMsg);
jpLup.add(spAllMsg);
allMsg.setFont(new Font("宋体",Font.BOLD,20));
allMsg.setForeground(Color.BLACK);
allMsg.setBackground(Color.GREEN);
allMsg.setEditable(false);
//输入区
jpLdown.setLayout(new BorderLayout());;
jpLeft.add(jpLdown);
jpLdown.add(jpLdownUp,BorderLayout.NORTH);
JScrollPane spInputMsg=new JScrollPane();
spInputMsg.setViewportView(inputMsg);
jpLdown.add(spInputMsg,BorderLayout.CENTER);
inputMsg.setBackground(Color.WHITE);
inputMsg.setFont(new Font("宋体",Font.BOLD,20));
inputMsg.setForeground(Color.BLACK);
inputMsg.setBackground(Color.white);
jpLdown.add(jpLdownDown,BorderLayout.SOUTH);
jpLdownDown.setLayout(new FlowLayout(FlowLayout.RIGHT));
jpLdownDown.add(send);
jpLdownDown.add(close);
//群成员显示
jpRight.setLayout(new BorderLayout());
JScrollPane spMember=new JScrollPane();
spMember.setViewportView(member);
jpRight.add(spMember,BorderLayout.CENTER);
member.setModel(listModel);
member.setBackground(Color.WHITE);
TitledBorder tb=new TitledBorder("群组成员");
jpRight.setBorder(tb);
//按钮动作
send.addActionListener(this);
close.addActionListener(this);
//申请与服务器端的连接
try {
Socket client=new Socket(server,port);
InputStream in=client.getInputStream();
OutputStream out=client.getOutputStream();
din=new DataInputStream(in);
dout=new DataOutputStream(out);
// 创建接收消息的线程
RsvThread rsv= new RsvThread();
new Thread(rsv).start();
}
catch (IOException ioe){
ioe.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae){
if (ae.getSource()==close){
try{
//退出前,通知服务器
dout.writeUTF("bye");
System.exit(0);
}
catch (Exception ec){
ec.printStackTrace();
}
}
else if (ae.getSource()==send){
try{
//向服务器端发送消息
String toServer=inputMsg.getText();
dout.writeUTF(toServer);
inputMsg.setText("");
}
catch( IOException ex){
ex.printStackTrace();
}
}
}
class RsvThread implements Runnable{
public void run(){
try{
while (true){
String ss=din.readUTF();
//**表示传过来的是成员信息,显示在右侧列表中
if (ss.substring(0,2).equals("**")){
listModel.addElement(ss.substring(2,ss.length()));
}
//##表示清除成员列表
else if (ss.substring(0,2).equals("##")){
listModel.clear();
}
//其他为正常聊天信息
else
allMsg.append(ss+"\n");
}
}
catch(IOException ex){
ex.printStackTrace();
}
finally {
try {
if(din != null) din.close();
if(dout != null) dout.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
- 1
- 2
前往页