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

📄 chatclient.java

📁 简易聊天室 测试过可以在局域网内进行聊天
💻 JAVA
字号:
/*
 * 简易聊天室
 * 作者:王安宇
 */

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

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class ChatClient extends Frame { 
	
	
	Socket s = null; 
	DataOutputStream dos = null; 
	DataInputStream dis = null; 
	

	//IP PORT 用户姓名
	int portNumber;
	String ipAddress;
	String clientName;
	
	
	//是否连接
	public boolean connected = false; 
	
	
	//图形界面元素
	JTextField ip = new JTextField();
	JTextField name = new JTextField();
	JTextField port = new JTextField();
	TextField input = new TextField(); 
	TextArea output = new TextArea(); 
	JButton connect = new JButton("Connect");
	JButton disconnect = new JButton("Disconnect");
	
	
	//用户线程
	Thread tRecv = new Thread(new RecvThread()); 

	
	/*
	 * 聊天室main函数
	 * 载入聊天室界面,运行聊天室
	 */
	public static void main(String[] args) { 
		
		new ChatClient().launchFrame();
		
	} 

	
	/*
	 * 载入聊天室界面
	 */
	public void launchFrame() { 
		
		
		Panel p1 = new Panel();
		p1.setSize(100, 300);
		Panel p2 = new Panel();
		p2.setSize(100, 300);
		setLocation(400, 300); 
		this.setSize(300, 300); 
		
		p1.setLayout(new GridLayout(4,2,5,5));
		p1.add(new JLabel("   IP:"));
		p1.add(ip);
		p1.add(new JLabel("  Port:"));
		p1.add(port);
		p1.add(new JLabel("   Name:"));
		p1.add(name);
		p1.add(connect);
		p1.add(disconnect);
		
		p2.setLayout(new BorderLayout());
		p2.add(output, BorderLayout.CENTER); 
		p2.add(input, BorderLayout.SOUTH); 
		
		this.setLayout(new GridLayout(1,1,5,5));
		this.add(p1);
		this.add(p2);
		
		
		/*
		 * void java.awt.Window.pack()
		 * Causes this Window to be sized to fit the preferred 
		 *	size and layouts of its subcomponents. If the window 
		 *	and/or its owner are not yet displayable, both are made
		 *	displayable before calculating the preferred size. The
		 *	Window will be validated after the preferredSize is calculated. 
		 */
		pack(); 
		
		
		/*
		 * void java.awt.Window.addWindowListener(WindowListener l)
		 * Adds the specified window listener to receive window 
		 * events from this window. If l is null, no exception 
		 * is thrown and no action is performed. 
		 */
		this.addWindowListener(new WindowAdapter() 
			{ 
			@Override 
				public void windowClosing(WindowEvent arg0) { 
					disconnect(); 
					System.exit(0); 
				} 
			}
		); 
		
		
		//为输入框添加监听器
		input.addActionListener((ActionListener) new InputListener()); 
		
		
		//connect按钮 建立连接
		connect.addActionListener(new ActionListener(){
			   
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == connect ){
					connect();
				}
			}
		});
		
		
		//disconnect按钮  断开连接
		disconnect.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == disconnect)
					disconnect();
			}
			
		});
		
		
		//设置框架可见
		setVisible(true); 
		
		//System.out.println(connected);//调程序用的

	} 

	
	/*
	 * 建立连接
	 */
	public void connect() { 
		
		try { 
			
			//接收输入的ip,port号,还有用户名字
			ipAddress = ip.getText().trim();
//			System.out.println(ipAddress);//调程序用的
			portNumber = Integer.parseInt(port.getText().trim());
//			System.out.println(portNumber);
			clientName = name.getText().trim();
//			System.out.println(clientName);
			
			
			s = new Socket(ipAddress, portNumber); 
//			s = new Socket("127.0.0.1", 8888); //调程序用的
			dos = new DataOutputStream(s.getOutputStream()); 
			dis = new DataInputStream(s.getInputStream()); 
			System.out.println("connected!"); 
			connected = true; 
			
//			System.out.println(connected);//调程序用的
			
			//启动用户线程
			tRecv.start(); 
			
		} catch (UnknownHostException e) { 
			
			e.printStackTrace(); 
			
		} catch (IOException e) { 
			
			e.printStackTrace(); 
			
		} 
	
	} 

	
	/*
	 * 断开连接
	 */
	public void disconnect() { 
		try { 
		dos.close(); 
		dis.close(); 
		s.close(); 
		} catch (IOException e) { 
		e.printStackTrace(); 
		} 

	}

	
	/*
	 * 接收输入
	 */
	public class InputListener implements ActionListener { 

		public void actionPerformed(ActionEvent e) { 
			
			String str = input.getText().trim(); 
			input.setText(""); 
	
			try { 
				//System.out.println(s); //调程序用的
				dos.writeUTF(clientName+" : "+str); 
				dos.flush();  
			} catch (IOException e1) { 
				e1.printStackTrace(); 
			} 
	
		} 
	
	} 

	
	/*
	 * 输出到聊天室对话框中
	 * 当断开连接时给出离开聊天室提示
	 */
	public class RecvThread implements Runnable { 
	   
		public void run() { 
		//System.out.println("helloworld");//调程序用的
		try { 
			//System.out.println(connected);//调程序用的

			while(connected) { 
				String str = dis.readUTF(); 
				System.out.println(str); 
				output.setText(output.getText() + str + '\n'); 
			} 
		} catch (SocketException e) { 
			System.out.println(clientName + "走啦!"); 
		} catch (EOFException e) { 
			System.out.println(clientName + "走啦!"); 
		} catch (IOException e) { 
			e.printStackTrace(); 
		} 
	
	}
	
	} 
} 

⌨️ 快捷键说明

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