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

📄 winpopclint.java

📁 一个用java编写的聊天程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

class clintThread extends Thread {
	private Socket socket;
	private WinpopClint winClint;
	private boolean sendFlag = false;
	private String strSend;
	private BufferedReader in;
	private PrintWriter out;
	private InetAddress addr;
	private static int PORT = 1983;
	private String myName;
	private boolean bQuit = false;
	
	public clintThread(WinpopClint wc) {
		try {
			addr = InetAddress.getLocalHost();
//		System.out.println(a.getHostName());
    		myName = ":";
			addr = InetAddress.getByName("192.168.204.54");
		} catch(UnknownHostException e) {
		}
		this.winClint = wc;
		winClint.display.setText("欢迎来托普局域网聊天系统!\n");
		try {
			socket = new Socket(addr, PORT);
		} catch(IOException e) {
		}
		try {
			in = new BufferedReader(
				new BufferedReader(
					new InputStreamReader(
						socket.getInputStream())));
						
			out = new PrintWriter(
				new BufferedWriter(
					new OutputStreamWriter(
						socket.getOutputStream())), true);
		
		start();
		} catch(IOException e) {
			try {
				socket.close();
			} catch(IOException ex) {
			}
		}
	}
	
	public void sendFlag() {
		
		String strTemp = this.winClint.textField.getText();
		strTemp = strTemp.trim();
//		System.out.println(strTemp);
		if (strTemp != null && !strTemp.equals("")) {
			sendFlag = true;
			strSend = "send+" + myName + strTemp;
		}
//		this.winClint.display.append(myName + this.winClint.textField.getText() + "\n");

//以下两行=append(....)  要的效果为滚动条离到最下方
/* 		
		String strTemp = myName + this.winClint.textField.getText();
		this.winClint.display.setText(this.winClint.display.getText() + strTemp + "\n");
*/		
//		String strOld = this.winClint.display.getText();
//		this.winClint.display.setText(myName + this.winClint.textField.getText() + "\n");
//		this.winClint.display.append(strOld);
		
	}
	
	public void quit() {
		bQuit = true;
	}
	
	public void run() {
		try {
			while (true) {
				if (!sendFlag) {
					synchronized(this) {
						out.println("receive+");
						String strReceive = in.readLine();
//						System.out.println(strReceive);
						while (!strReceive.equals("+noMessage")) {//如果接收到+noMessage就不进入while
							String strOld = this.winClint.display.getText();
							this.winClint.display.setText(strReceive + "\n");
							this.winClint.display.append(strOld);
//							winClint.display.append(strReceive + "\n");
//							this.winClint.display.setText(this.winClint.display.getText() + strReceive + "\n");
							strReceive = in.readLine();
						}
					}
				} else {
					synchronized(this) {
						sendFlag = false;
						out.println(strSend);
					}
				}
				
				if (bQuit) {
					out.println("quit+");
//					winClint.display.append(in.readLine());
					String strOld = this.winClint.display.getText();
					this.winClint.display.setText(in.readLine() + "\n");
					this.winClint.display.append(strOld);
				}
				
				Thread.currentThread().sleep(500);
			}
		} catch(IOException e) {
		} catch(InterruptedException e2) {
		} finally {
			this.winClint.isConn = false;
			this.winClint.quitButton.setText("连接");
			String strOld = this.winClint.display.getText();
			this.winClint.display.setText("您与服务器断开连接,请点\"连接\"重新登录!\n");
			this.winClint.display.append(strOld);
		}
		
	}
}

public class WinpopClint extends Applet {
	private clintThread ct = null;
	boolean isConn = true;
	JTextArea display = new JTextArea(10, 60);
	JScrollPane displayPane = new JScrollPane();
	JPanel textPanel = new JPanel();
	JLabel textLabel = new JLabel();
	JTextField textField = new JTextField(20);
	JPanel sendPanel = new JPanel();
	JPanel buttonPanel = new JPanel();
	JButton sendButton = new JButton("发送");
	JButton resetButton = new JButton("清屏");
	JButton quitButton = new JButton("退出");
	
		
	public void init() {
		display.setEditable(false);
		this.setLayout(new BorderLayout());
//		displayPane.setAutoscrolls(true);
		displayPane.getViewport().add(display, null);
		this.add(displayPane, BorderLayout.NORTH);
		textLabel.setText("发言");
		textPanel.add(textLabel, null);
		textPanel.add(textField, null);
		this.add(textPanel, BorderLayout.CENTER);
		sendPanel.setLayout(new BorderLayout());
		buttonPanel.setLayout(new FlowLayout());
		sendButton.addActionListener(new SendL());
		buttonPanel.add(sendButton, null);
		resetButton.addActionListener(new ResetL());
		buttonPanel.add(resetButton, null);
		quitButton.addActionListener(new QuitL());
		buttonPanel.add(quitButton, null);
		sendPanel.add(buttonPanel, BorderLayout.CENTER);
		this.add(sendPanel, BorderLayout.SOUTH);
		ct = new clintThread(WinpopClint.this);
	}
	
	class SendL implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (ct != null) {
				ct.sendFlag();
				textField.setText("");
			}
		}
	}
	
	class ResetL implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			display.setText("");
		}
	}
	
	class QuitL implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (isConn) {
				ct.quit();
				isConn = false;
				quitButton.setText("连接");
			} else {
				isConn = true;
				quitButton.setText("退出");
				ct = new clintThread(WinpopClint.this);
			}
		}
	}
	
	static class WL extends WindowAdapter {
	    public void windowClosing(WindowEvent e) {
	   		System.exit(0);
    	}
  	}

	
	public static void main(String[] args) {
		WinpopClint applet = new WinpopClint();
		JFrame frame = new JFrame("WinpopClint");
		frame.addWindowListener(new WL());
    	frame.getContentPane().add(
 		applet, BorderLayout.CENTER);
	    frame.setSize(300,300);
	    applet.init();
	    applet.start();
 	   	frame.setVisible(true);
	}
}

⌨️ 快捷键说明

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