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

📄 qqmain.java

📁 模仿qq的聊天系统,java做的,能基本实现聊天同信功能
💻 JAVA
字号:
package com.hs ;

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

public class QQmain extends JFrame implements ActionListener ,Runnable, WindowListener {
	String username ;
	
	JTextField tt = new JTextField() ;
	JTextArea cc = new JTextArea() ;
	JScrollPane sp = new JScrollPane(cc) ;
	
	JComboBox c_user = new JComboBox() ;
	
	MyNet mn = new MyNet() ;
	
	private Socket s = null ;
	FileSend fs = new FileSend() ;
	
	QQmain(String title , Socket s) {
		
		this.s = s ;
		
		//设置窗体属性
		this.setTitle(title) ;
		this.setSize(400 , 600) ;
		this.setResizable(false) ;
		
		int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
		int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() ;
		this.setLocation((width - 400) / 2 ,(height - 600) / 2) ;
		
		username = title ;
		
		//new 组件
		JButton b_send = new JButton("发送") ;
		b_send.setForeground(Color.red) ;
		JButton b_clear = new JButton("清空聊天记录") ;
		JButton b_file = new JButton("发送文件") ;
		
		//设置让发送目录可填
		c_user.setEditable(true) ;		
		c_user.addItem("ALL") ;
		
		//注册按钮事件监听
		b_send.addActionListener(this) ;
		b_clear.addActionListener(this) ;
		b_file.addActionListener(this) ;
		
		//布置小面板
		JPanel p_xiao = new JPanel() ;
		p_xiao.setLayout(new GridLayout(1 , 4)) ;
		
		p_xiao.add(c_user) ;
		p_xiao.add(b_send) ;
		p_xiao.add(b_clear) ;
		p_xiao.add(b_file) ;
		
		//布置大面板
		JPanel p_da = new JPanel() ;
		p_da.setLayout(new GridLayout(2 , 1)) ;
		
		p_da.add(tt) ;
		p_da.add(p_xiao) ;
		
		//布置窗体
		this.setLayout(new BorderLayout()) ;
		
		this.add(p_da , BorderLayout.NORTH) ;
		this.add(sp , BorderLayout.CENTER) ;
		
		this.addWindowListener(this) ;	    
		
		//读取聊天记录
		try {
			File f = new File("D:/聊天记录/" + username + ".hs") ;
			FileReader fr = new FileReader(f) ;
			BufferedReader br = new BufferedReader(fr) ;
			
			while(br.ready()) {
				cc.append(br.readLine()+"\n") ;
			}
	    }
	    catch (Exception ex) {
	    	ex.printStackTrace() ;
	    }
	    
	    //启动线程
		new Thread(this).start() ;  
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("发送")) {
			try {
									
				if(!tt.getText().equals("")) {
					//向服务器发送聊天信息
					if(c_user.getSelectedItem().toString().equals("ALL")) {
						mn.send( s , "ALL==hs==" +username + " 对所有人说: " + tt.getText()) ;
	
						
						//tt---->>cc						
					//	cc.append(username + "对所有人说:" + tt.getText() + "\n") ;	
						
					}else {
						mn.send( s , c_user.getSelectedItem() + "==hs=="+username+"悄悄的对你说" + tt.getText()) ;
						cc.append("你悄悄的对"+ c_user.getSelectedItem() +"说"+tt.getText() + "\n") ;
					}
				}
								
				//清空输入的信息
				tt.setText("") ;
		    }
		    catch (Exception ex) {
		    	ex.printStackTrace() ;
		    }	
		}
		
		if(e.getActionCommand().equals("清空聊天记录")) {
			cc.setText("") ;
		}
		
		if(e.getActionCommand().equals("发送文件")) {
		
			
			if(c_user.getSelectedItem().toString().equals("ALL")) {
				mn.send(s ,"File==hs==all") ;
				
				
			} else {
				mn.send(s ,"File==hs=="+c_user.getSelectedItem()) ;
				
			}
			
			//选择文件
			JFileChooser fc = new JFileChooser() ;
			fc.showOpenDialog(this) ;
			File fileName = fc.getSelectedFile() ;
			fs.sendFile(s , fileName) ;		
			
			
		}
	}
	
	//接收服务端发送回来的消息	
	public void run() {
		try {
			while(true) {
				System.out.println ("哈哈哈  "+s) ;
				String mess = mn.receive(s) ;
				
				//裁分信息
				String op = mess.split("==hs==")[0] ;
				String ms = mess.split("==hs==")[1] ;
				if(op.equals("exit")) {
					c_user.removeItem((Object)ms) ;	
					cc.append("你的好友"+ms+"离开了"+"\n") ;
				}
				if(op.equals("say")) {
					cc.append(ms+"\n") ;	
				}
				if(op.equals("username")) {
					System.out.println ("你的好友"+ms+"上线了") ;
					c_user.addItem(ms) ;
					cc.append("你的好友"+ms+"上线了"+"\n") ;
				}	
				if(op.equals("aFile")) {
					
					//选择文件
					JFileChooser fc = new JFileChooser() ;
					fc.showSaveDialog(new JFrame()) ;
					File fileName = fc.getSelectedFile() ;
					
					fs.fileReceive(s ,fileName) ;
				}
			}
	    }
	    catch (Exception ex) {
	    	ex.printStackTrace() ;
	    }
	}

	public void windowOpened(WindowEvent e) {

	}

	public void windowClosing(WindowEvent e) {
		//保存聊天记录
		try {
			//发送下线信息
			mn.send(s ,"exit==hs==" + username) ;
			
			System.out.println ("正在保存聊天记录.........") ;
			File f = new File("D:/聊天记录/" + username + ".hs") ;
			
			FileWriter fw = new FileWriter(f , true) ;
			PrintWriter pw = new PrintWriter(fw) ;
			
			pw.println(cc.getText()) ;
			
			System.out.println ("保存聊天记录完毕") ;
			pw.close() ;
	    }
	    catch (Exception ex) {
	    	ex.printStackTrace() ;
	    }
	}

	public void windowClosed(WindowEvent e) {

	}

	public void windowIconified(WindowEvent e) {

	}

	public void windowDeiconified(WindowEvent e) {

	}

	public void windowActivated(WindowEvent e) {

	}

	public void windowDeactivated(WindowEvent e) {

	}
}

⌨️ 快捷键说明

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