* 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);
}
}
else
{
JOptionPane.showMessageDialog(null,"发送信息失败!(服务器关闭/网络故障)","错误",JOptionPane.OK_OPTION);
}
}
/*-- 读取信息线程 --*/
class linread implements Runnable
{
public void run()
{
read();
}
public void read()
{
while(isConnected)
{
try {
String msg = in.readUTF();
if("SYSTEM_CLOSED".equals(msg))
{
JOptionPane.showMessageDialog(null,"读取消息失败(服务器关闭/网络故障)聊天系统java源代码!","错误",JOptionPane.OK_OPTION);
isConnected = false;
}
else
allmsg.append(msg+"\n");
} catch (IOException e) {
}
}//end while
JOptionPane.showMessageDialog(null,"读取消息失败(服务器关闭/网络故障)聊天系统java源代码!","错误",JOptionPane.OK_OPTION);
推荐阅读
- 抖音直播永哥软件,抖音直播用的软件
- js的页面传参数的方法,js页面之间传递参数
- shell查看mongodb版本号,shell mongodb
- 002762网红直播,网红直播概念股票有哪些
- mysql中怎么建立索引 mysql数据库怎么创建索引
- sqlserver定义游标,sql语句游标
- 视频号流量下滑怎么回事,视频号流量突然下降
- 适合男生恋爱养成的小游戏,适合男生恋爱养成的小游戏有哪些
- vb.net存储用户信息 vbnet recordset