java聊天源代码 java聊天程序设计代码( 二 )


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ClientFrame extends JFrame{
private JTextArea allmsg;
private JTextField welcome,copyright,chatmsg;
private JButton send;
private JScrollPane js;
private boolean isConnected = true;
public DataOutputStream out;
public DataInputStream in;
public Socket s = null;
String nic;/*-- 保存用户昵称 --*/
/**
* 初始化客户端资源
* 1.获取从LoginFrame传递过来的参数
* 2.初始化界面元素
* 3.初始化通信所需要的资源 EG:输入/输出流(DataInputStream/DataOutputStream)
* */
public ClientFrame(String name,Socket socket)
{
this.setSize(310,660);
this.setLocation(290,50);
this.setTitle("聊天室客户端"+name+"");/*-- 指定窗口的标题 --*/
this.s = socket;/*-- 接收从LoginFrame中传递过来的Socket --*/
this.nic = name+" 说: ";
welcome = new JTextField(""+name+" 欢迎您来到聊天室 ",100);
welcome.setBackground(Color.blue);
welcome.setEnabled(false);
copyright = new JTextField("-----all copyright @ TOP-king-----");
copyright.setEnabled(false);
allmsg = new JTextArea();
allmsg.setEditable(false);
allmsg.append("系统消息: 欢迎登录在线聊天室 \n");
js = new JScrollPane(allmsg);//为JTextArea添加滚动条
chatmsg = new JTextField("在此输入聊天信息");
chatmsg.addActionListener(new listen());
send = new JButton("发送");
send.addActionListener(new listen());/*-- 添加事件监听器 --*/
try {
out = new DataOutputStream(s.getOutputStream());
in = new DataInputStream(s.getInputStream());
} catch (IOException e) {JOptionPane.showMessageDialog(null, "系统异常","错误",JOptionPane.OK_CANCEL_OPTION);}
addcomponettocontainer();
/*-- 当用户关闭窗口时进行相关的处理 eg:Socket Data(Input/Output)Stream 的关闭--*/
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
sendmsg("quitlogout");/*-- 向服务器端发送关闭信息 --*/
isConnected = false;
destory();/*-- 销毁窗口资源 --*/
}
});
new Thread(new linread()).start();/*-- 启动读取信息线程 --*/
}
public void addcomponettocontainer()
{
Container c = this.getContentPane();
c.setLayout(null);
welcome.setBounds(75,10,150,20);
js.setBounds(10,50,280,500);
chatmsg.setBounds(10,560,180,30);
send.setBounds(220,560,70,30);
copyright.setBounds(10,600,280,20);
c.add(welcome);
c.add(js);
c.add(chatmsg);
c.add(send);
c.add(copyright);
this.setVisible(true);
this.setResizable(false);
}
class listen implements ActionListener
{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==send||e.getSource()==chatmsg)
{
String msg = chatmsg.getText().trim();
if("".equals(msg))
{
JOptionPane.showMessageDialog(null,"发送信息不能为空!","错误",JOptionPane.OK_OPTION);
}
else
{sendmsg((new Date()).toLocaleString()+"\n"+nic+msg+"\n");chatmsg.setText("");}
}
}
}
/*-- 向服务器端发送信息 --*/
public void sendmsg(String m)
{
if(isConnected)//如果socket的输出流没关闭
{
try {
out.writeUTF(m);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"发送信息失败!(系统异常)","错误",JOptionPane.OK_OPTION);

推荐阅读