课题:一个简单的聊天软件
一、课设要求
设计一个图形用户界面程序,先设置好本地地址(包括IP地址和端口号),打开程序窗口后,输入目标地址(包括IP地址和端口号)和要发送的信息之后,点击发送按钮可以向目标地址发送信息;并且程序可以接收发送到本地地址的信息并显示在程序窗口中。
ChatWindow.java 主类,负责创建聊天软件登录界面以及聊天窗体界面
SendThread.java 实现数据的发送的,当按键按下时,会调用sendMessage方法
ReceiveThread.java 维护一个阻塞线程,来对端口进行监听,接受收到的信息,每当收到信息时,都会显示
(1)ChatWindow类成员变量
聊天窗体组件
sendButton 发送按钮
cancelButton 取消按钮
receiveButton 开始接收数据按钮
receiveText 聊天记录
sendText 消息内容
localPort 发送方端口号
remoteAddress 接收方IP
remotePort 接收方端口号
sendThread 发送方
ReceiveThread 接收方
登陆界面组件
Jbl1 QQ图片
Jp2_jbl1 IP地址
Jp2_jbl2 端口号
Jp2_jbl3 空格(增加间距)
Jp2_jb1 登录按钮
chatWindow 聊天主类对象
remoteIP 发送方IP地址
port 发送方端口
message 发送消息
server 服务对象
window 聊天窗体对象
(1)ChatWindow类方法
denglu() 负责创建登陆界面
GUIini() 负责创建聊天界面
Actionini() 负责监听
Threadini() 负责线程控制
senMessage() 负责记录发送方IP,端口号,信息
notRun() 负责将数据写入到流中
run() 负责和发送方建立联系
ReceiveThread() 负责确认发送方是否匹配
四、 课设成果
2.聊天界面
发送与接收方聊天
五丶附录
代码如下:
ChatWindow
package liaotian;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatWindow extends JFrame implements ActionListener{
private JButton sendButton;
private JButton cancelButton;
private JButton receiveButton;
private JTextArea receiveText;
private JTextField sendText;
private JTextField localPort;
private JTextField remoteAddress;
private JTextField remotePort;
private SendThread sendThread;
private ReceiveThread receiveThread;
private JLabel jbl1;
private JPanel jp2;
private JLabel jp2_jbl1,jp2_jbl2,jp2_jbl3;
private JButton jp2_jb1;
public void denglu() {
jbl1=new JLabel(new ImageIcon("image/图片1.gif"));
this.setSize(350, 150);
this.add(jbl1, "North");
jp2=new JPanel(new GridLayout(2,2));
jp2_jbl1=new JLabel("IP地址",JLabel.CENTER);
jp2_jbl2=new JLabel("端口号(1-65535)",JLabel.CENTER);
jp2_jbl3=new JLabel(" ");
jp2_jb1=new JButton("登录");
jp2_jb1.addActionListener(this);
jp2.add(jp2_jbl1);
try {
jp2.add(new JTextField(InetAddress.getLocalHost().getHostAddress()));
} catch (UnknownHostException e) {
jp2.add(new JTextField("当前机器的IP地址是:" + "Unknown"));
}
jp2.add(jp2_jbl3);
jp2.add(jp2_jbl2);
jp2.add(localPort = new JTextField(""+0));
jp2.add(jp2_jb1);
this.add(jp2,"Center");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0 ) {
if(arg0.getSource()==jp2_jb1)
{
jbl1.setVisible(false);
jp2.setVisible(false);
GUIini();
ActionIni();
ThreadIni();
}
}
private void GUIini(){
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
setSize(400, 500);
JPanel panel1 = new JPanel(new GridLayout(4, 2));
panel1.setSize(400, 75);
panel1.add(new JLabel("发送方IP:"));
try {
panel1.add(new JLabel(InetAddress.getLocalHost().getHostAddress()));
} catch (UnknownHostException e) {
panel1.add(new JLabel("发送方IP:" + "Unknown"));
}
panel1.add(new JLabel("发送方端口:"));
panel1.add(new JLabel(localPort.getText()));
panel1.add(new JLabel("接收方IP:"));
remoteAddress = new JTextField();
remoteAddress.setColumns(0);
panel1.add(remoteAddress);
panel1.add(new JLabel("接收方端口:"));
remotePort = new JTextField();
remotePort.setColumns(0);
panel1.add(remotePort);
c.add(panel1,BorderLayout.NORTH);
JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
panel2.setSize(400,200);
panel2.add(new JLabel("聊天记录:"));
receiveText = new JTextArea(15,30);
receiveText.setEditable(false);
receiveText.setAutoscrolls(true);
JScrollPane jsp = new JScrollPane(receiveText);
panel2.add(jsp);
panel2.add(new JLabel("信息内容:"));
sendText = new JTextField(30);
sendText.setAutoscrolls(true);
panel2.add(sendText);
c.add(panel2,BorderLayout.CENTER);
JPanel panel4 = new JPanel(new GridLayout(1, 0));
panel4.setSize(400,20);
receiveButton = new JButton("开始接受数据");
sendButton = new JButton("发送");
cancelButton = new JButton("清除");
panel4.add(receiveButton);
panel4.add(cancelButton);
panel4.add(sendButton);
c.add(panel4,BorderLayout.SOUTH);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void ActionIni() {
this.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
sendThread.senMessage(remoteAddress.getText(), Integer.parseInt(remotePort.getText()), sendText.getText());
receiveText.setText(receiveText.getText() + "\n" + "发送:" + sendText.getText());
}
}
public void keyPressed(KeyEvent e) {
}
});
cancelButton.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
sendText.setText("");
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
});
sendButton.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
sendThread.senMessage(remoteAddress.getText(), Integer.parseInt(remotePort.getText()), sendText.getText());
receiveText.setText(receiveText.getText() + "发送:" + sendText.getText()+ "\n");
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
});
}
private void ThreadIni() {
sendThread = new SendThread(0, this);
receiveThread = new ReceiveThread(this, Integer.parseInt(localPort.getText()));
}
public ChatWindow() {
denglu();
}
public void printError(String err){
System.out.println("Error occur:" + err);
}
public void setReceive(String receive){
receiveText.setText(receiveText.getText() + "收到:" + receive+ "\n" );
}
public void setLocalPort(int localPortText){
localPort.setText(""+localPortText);
}
public static void main(String[] args) {
new ChatWindow();
}
}
ReceiveThread
package liaotian;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class ReceiveThread extends Thread {
private ServerSocket server;
private ChatWindow window;
public ReceiveThread(ChatWindow window,int Port) {
this.window = window;
try {
server = new ServerSocket(Port);
start();
} catch (IOException e) {
window.printError("连接出错");
}
}
public void run() {
while(true){
Socket socket;
try {
socket = server.accept();
InputStreamReader reader = new InputStreamReader(socket.getInputStream());
int c;
StringBuilder sb = new StringBuilder();
while((c = reader.read()) != -1){
sb.append((char)c);
}
window.setReceive(sb.toString());
} catch (IOException e) {
System.out.println("线程将接受到的数据写入对话框出错");
}
}
}
}
SendThread
package liaotian;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class SendThread {
ChatWindow chatWindow;
private String remoteIP = "";
private int port = 0;
private String message = "";
public SendThread(int port,ChatWindow window) {
chatWindow = window;
}
public void notRun() {
InetSocketAddress isa = new InetSocketAddress(remoteIP, port);
try {
Socket socket = new Socket();
socket.connect(isa);
OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
writer.write(message);
writer.flush();
writer.close();
System.out.println("将数据写入到流中");
} catch (IOException e) {
e.printStackTrace();
}finally{
message = "";
}
}
public void senMessage(String host,int port,String message){
remoteIP = host;
this.port = port;
this.message = message;
notRun();
}
}