⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatclient.java

📁 一个聊天系统,是当时做的一个课程设计,可以用于局域网内的聊天.但你首先需要知道它(用作服务器)的IP.
💻 JAVA
字号:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatClient extends Frame {
	private Label lbName = null;
	private Label lbIP = null;
	private Label lbPort = null;
	
	private TextField tfName = null;
	private TextField tfIP = null;
	private TextField tfPort = null;
	
	private Button btCancel = null;
	private Button btConnect = null;
	
	private String strName = null;
	private String strIP = null;
	private String strPort = null;
	
	public static void main(String[] args){
		new ChatClient().launch();
	}
	
	public void launch(){
		this.setLocation(300,300);
		this.setSize(300,120);
		lbName = new Label("姓名");
		lbIP = new Label("IP地址");
		lbPort = new Label("端口号");
		tfName = new TextField();
		tfIP = new TextField();
		tfPort = new TextField();
		btConnect = new Button("连接服务器");
		btCancel = new Button("取消");
		this.setLayout(new GridLayout(4, 2));
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}			
		});
		this.add(lbName);
		add(tfName);
		add(lbIP);
		add(tfIP);
		add(lbPort);
		add(tfPort);
		add(btConnect);
		add(btCancel);
		btCancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}			
		});
		btConnect.addActionListener(new ConnectListener());
		
		this.setVisible(true);
	}
	
	class ConnectListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			strName = tfName.getText();
			strIP = tfIP.getText();
			strPort = tfPort.getText();
			new Chat(strName, strIP, strPort).launch();
			//System.exit(0);
		}
		
	}
}









class Chat extends Frame {

	private String str = null;
	private String strName = null;
	private String strIP = null;
	private String strPort = null;

	TextArea ta = null;
	TextField tf = null;
	Button bt = null;
	
	Socket s = null;
	DataInputStream dis = null;
	DataOutputStream dos = null;
	
	Chat(String strName, String strIP, String strPort){
		this.strName = strName;
		this.strIP = strIP;
		this.strPort = strPort;
	}
	
	
	public void launch(){
		this.setSize(300, 300);
		this.setLocation(300, 300);
		tf = new TextField();
		ta = new TextArea();
		bt = new Button("回车发送");
		add(ta, BorderLayout.NORTH);
		add(tf, BorderLayout.CENTER);
		add(bt, BorderLayout.SOUTH);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				disConnect();
				System.exit(0);
			}			
		});
		tf.addActionListener(new MyListener());
		bt.addMouseListener(new MyMouseListener());
		this.setVisible(true);
		
		connect();
		new Thread(new Client()).start();
	}
	
	public void connect(){//此函数用于连接数据库,所以这个函数必须在类ChatClient运行之后才能运行
		try {
			s = new Socket(strIP, Integer.valueOf(strPort));//连接IP为127.0.0.1,端口号为6666的服务器.
System.out.println("客户端成功连接上");//用于检验是否连接上服务器
		} catch (UnknownHostException e) {//捕捉各种异常
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	public void disConnect(){
		try {
			if(s != null) s.close();
			if(dos != null) dos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	class MyListener implements ActionListener {//用于中tf文本框中回车时作的反应
		private String str = null;
		public void actionPerformed(ActionEvent e) {
			str = strName + "says: " + tf.getText();
			//ta.setText(ta.getText() + str + '\n');
			tf.setText("");
			
			
			try {//以下是用于联结服务器,把客户端输入的东西发送到服务器
				dos = new DataOutputStream(s.getOutputStream());
				dos.writeUTF(str);
			} catch (IOException e1) {
				e1.printStackTrace();
			}	
		}
		
	}
	
	
	class MyMouseListener extends MouseAdapter {//用于鼠标点击时作的反应
		private String str = null;
		public void mouseClicked(MouseEvent e) {
			str = strName + "  says: " + tf.getText();
			//ta.setText(ta.getText() + str + '\n');
			tf.setText("");
			
			
			try {//以下是用于联结服务器,把客户端输入的东西发送到服务器
				dos = new DataOutputStream(s.getOutputStream());
				dos.writeUTF(str);
			} catch (IOException e1) {
				e1.printStackTrace();
			}	
		}		
	}
	
	
	
	
	class Client implements Runnable {
		public void run() {
			try {
				dis = new DataInputStream(s.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
			String str = null;
			try {
				while (true) {
					str = dis.readUTF();
					ta.setText(ta.getText() + str + '\n');
				}
			} catch (IOException e) {
				try {
					dis.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			} finally {
				try {
					if(dis != null) dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}

		}
	}
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -