📄 chatfra.java
字号:
package chatSystem;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import javax.swing.*;
/**
* 这个是一个用来处理用户与一个朋友单聊的类,由一个界面类和一个线程类组成,
* 是用户请求一对一单聊的线程
* @author 55
*
*/
public class ChatFra extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1461600851709524462L;
JTextArea tshow,twrite;
JButton send,close;
JPanel tp,bp;
String str,received,me,friendName;
Dimension dim;
int port;
boolean request=true,stop=false;
String ip=null;
ClientThread t=null;
Socket socket=null;
Date d=null;
/**
* 生成聊天对话框,初始化一些变量
* @param color
* @param port
* @param ip
* @param me
* @param friendName
*/
public ChatFra(Color color,int port ,String ip ,final String me,String friendName)
{
this.me=me;
this.port=port;
this.ip=ip;
this.friendName=friendName;
setTitle("与"+friendName+"聊天中");
setSize(530,300);
tshow=new JTextArea(9,45);
tshow.setFont(new Font("SansSerif",Font.BOLD,12));
tshow.setEditable(false);
JScrollPane scrollpane=new JScrollPane(tshow);
twrite=new JTextArea(3,45);
twrite.setFont(new java.awt.Font("SansSerif", Font.BOLD, 12));
twrite.setEnabled(true);
twrite.setLineWrap(true);
JScrollPane wjsp=new JScrollPane(twrite);
tp=new JPanel();
tp.setBackground(color);
tp.add(scrollpane,BorderLayout.NORTH);
tp.add(wjsp,BorderLayout.SOUTH);
add(tp,BorderLayout.CENTER);
send=new JButton("发送");
send.addActionListener(this);
close=new JButton("关闭");
close.addActionListener(this);
bp=new JPanel();
bp.setBackground(color);
bp.add(send);
bp.add(close);
add(bp,BorderLayout.SOUTH);
Toolkit kit=Toolkit.getDefaultToolkit();
dim=kit.getScreenSize();
setLocation((dim.width-500)/2,(dim.height-300)/2 );
setVisible(true);
d=new Date();
try
{
socket=new Socket(ip,port);
}
catch (UnknownHostException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
t=new ClientThread(socket);//生成接收消息线程
t.start();
twrite.addKeyListener(new KeyAdapter()//当用户按Ctrl+Enter时发送消息
{
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode()==KeyEvent.VK_ENTER&&ke.isControlDown())
{
str=(d.getYear()+1900)+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+(d.getMinutes()+1)
+":"+(d.getSeconds()+1)+"\n"+me+"说: "+twrite.getText();
try
{
t.sendMessage(str);
tshow.append(str+"\n");
twrite.setText("");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
t.quiet();
new WarmDialog("你中毒了","哈哈..").setVisible(true);
}
});
}
/**
* 处理按钮事件,发送消息,关闭对话框
*/
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==send)
{
str=(d.getYear()+1900)+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+(d.getMinutes()+1)
+":"+(d.getSeconds()+1)+"\n"+me+"说: "+twrite.getText();
try
{
t.sendMessage(str);
tshow.append(me+"说: ");
tshow.append(str+"/n");
twrite.setText("");
}
catch (Exception e)
{
e.printStackTrace();
}
}
else if(ae.getSource()==close)
{
t.quiet();
setVisible(false);
}
}
/**
* 这个类是用来接收对方发来的消息的
* @author 黄祖光
*
*/
public class ClientThread extends Thread
{
Socket socket=null;
BufferedReader br=null;
PrintWriter pw=null;
String str="";
boolean quit=false;
ChatFr cf=null;
ClientThread(Socket s)
{
socket=s;
try
{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 监听对方发来的消息,收到就显示在对话框上
*/
public void run()
{
while(!quit)
{
try
{
System.out.println("收消息前.....");
str=br.readLine();
System.out.println("收到信息了...."+str);
while(!"".equals(str))
{
tshow.append(str);
tshow.append("\n");
str="";
}
}
catch (IOException e)
{
//e.printStackTrace();
}
}
try
{
br.close();
pw.close();
socket.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
/**
* 用来发送用户的消息
* @param s
*/
public void sendMessage(String s)
{
pw.println(s);
pw.flush();
System.out.println("发送信息了....");
}
/**
* 通过赋值给变量来控制线程的运行
*
*/
public void quiet()
{
quit=true;
}
}
/* public static void main(String[] args)
{
ChatFra cf=new ChatFra(Color.cyan,1234,"127.0.0.1","客户端");
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -