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

📄 talkclient.java

📁 类似于QQ的聊天程序
💻 JAVA
字号:
package client;

import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.NoRouteToHostException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class TalkClient extends JFrame implements Runnable {

	private static final long serialVersionUID = 1L;
	private TextArea showWord = new TextArea(
			"");
	private JTextField inputWord = new JTextField();
	private Socket client = null;
	private String name = "";
	private String isName = "no";

	public static void main(String[] args) {
		new Thread(new TalkClient()).start();
	}
//客户端初始化,包括:调用用户登录方法,界面显示,给控件增加监听器。
	public TalkClient() {
		userLogOn();

		new DefaultClientFrame(this);

		inputWord.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				talk();
			}

		});
	}
public TextArea getShowWord() {
		return showWord;
	}
	public void setShowWord(TextArea showWord) {
		this.showWord = showWord;
	}
	public JTextField getInputWord() {
		return inputWord;
	}
	public void setInputWord(JTextField inputWord) {
		this.inputWord = inputWord;
	}
	//用户登录方法
	public void userLogOn() {
		while (isName.equals("no")) {

			name = "";
			while (name.equals("")) {
				name = JOptionPane.showInputDialog("输入用户名:");
				if (name == null)
					System.exit(-1);
			}

				try {
					client = new Socket("172.27.249.183", 8888);
					OutputStream output = client.getOutputStream();
					DataOutputStream dataOutput = new DataOutputStream(output);
					dataOutput.writeUTF(name);

					InputStream input = client.getInputStream();
					DataInputStream dataInput = new DataInputStream(input);
					isName = dataInput.readUTF();
				} catch(NoRouteToHostException e1){
					JOptionPane.showMessageDialog(this, "没有连接网络");
					System.exit(-1);
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
			
			if (isName.equals("no")) {
				try {
					JOptionPane.showMessageDialog(this, "用户名已被人占用。");
					client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
//用户在聊天室发言
	public void talk() {
		if (inputWord.getText().trim().equals("")) {
			showWord.append('\n' + "系统:不能发送空消息。");
			inputWord.setText("");
		} else {
			try {
				OutputStream output = client.getOutputStream();
				DataOutputStream dataOutput = new DataOutputStream(output);
				dataOutput.writeUTF(name + ": " + inputWord.getText().trim());
				inputWord.setText("");
			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
//用线程来随时接受其他用户的发言
	public void run() {
		InputStream input = null;
		DataInputStream dataInput = null;
		while (true) {
			String str = null;
			try {
				input = client.getInputStream();
				dataInput = new DataInputStream(input);
				str = dataInput.readUTF();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "你被管理员踢出聊天室");
				System.exit(-1);
			}
			showWord.append('\n' + str);
		}

	}

}

⌨️ 快捷键说明

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