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

📄 clientcontrolgui.java

📁 JAVA实现的聊天工具,可以容纳最多10个用户 1.本系统需要JDK1.5 或更高版本的支持。 2.serverDatabase为服务器端的数据文件. 若使用现有数据,可用帐号:1, 密码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.File;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.InetAddress;
import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
public class ClientControlGUI extends WindowAdapter implements Runnable,ActionListener,ListSelectionListener,KeyListener{
	private static boolean connecting;
	private boolean initiate;
	private static boolean updatingList,updatingMessage;
	private String serverAddress;
	private long currentClientID;
	private JFrame frame;
	private static JLabel titleLabel,statusLabel,toFriendLabel,messageLabel,friendLabel;
	private JTextArea toFriendField;
	private static JTextArea messageField;
	private JList friendList;
	private static DefaultListModel listModel;
	
	private Socket serverSocket; //use port 4444
	private ObjectOutputStream oosServer; //stream to communicate with server
    private DataOutputStream dos;
	private ByteArrayOutputStream baos;
	private byte[] byteArray;
	private static boolean isFriendStarted;
	private static Thread clientFriendGUI;
	private static FriendsData messageToFriend;  //to send message to this friend
    
    private static final String DEL="del";
    private static final String ADD="add";
    private static final String SENDINFO="sendinfo";
    private static final String SENDFILE="sendfile";
    private static final String EXIT="exit";
    private static final String ABOUT="about";
    
    private static LinkedList<FriendsData> friendObjectList; //reflect items in friendList,online friend list
	 
	public ClientControlGUI(long currentClientID,String serverAddress,boolean initiate){
		this.currentClientID=currentClientID;
		this.serverAddress=serverAddress;
		this.initiate=initiate;
		connecting=true;
		friendObjectList=new LinkedList<FriendsData>();
	}
	
	public void run(){
		frame=new JFrame("Client Panel");
		frame.setDefaultLookAndFeelDecorated(true);
		frame.setJMenuBar(makeMenuBar());
		frame.setContentPane(makeMainPane());
		frame.addWindowListener(this);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setResizable(false);		
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae){
		String type=ae.getActionCommand();
		if(type.equals(DEL)){  //**
			if(!connecting){
				beep();
				JOptionPane.showMessageDialog(frame,"Connection to server failed!",
						"Task Running",JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(isFriendStarted){
				beep();
				return;
			}
			isFriendStarted=true;
			clientFriendGUI=new Thread(new ClientFriendGUI("delete",serverAddress,currentClientID),"clientFriendGUI");
			clientFriendGUI.start();
		}
		
		else if(type.equals(ADD)){   //**
			if(!connecting){
				beep();
				JOptionPane.showMessageDialog(frame,"Connection to server failed!",
						"Task Running",JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(isFriendStarted){
				beep();
				return;
			}
			isFriendStarted=true;
			clientFriendGUI=new Thread(new ClientFriendGUI("add",serverAddress,currentClientID),"clientFriendGUI");
			clientFriendGUI.start();
		}
		
		else if(type.equals(SENDINFO)){  //**
			sendMessageToFriend();
		}
		
		else if(type.equals(SENDFILE)){  //**
			if(!connecting){
				beep();
				JOptionPane.showMessageDialog(frame,"Connection to server failed!",
						"Error",JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(messageToFriend==null){  //have not select any friend
				beep();
				JOptionPane.showMessageDialog(frame,"You haven't select any friend!",
						"No selected friend",JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			
			JFileChooser fc=new JFileChooser();
			int returnVal=fc.showDialog(frame,"OK");
			int sendFileKey;
			File tempFile;
			ToFriendFile tff;
			ServerClient data;
			fc.setMultiSelectionEnabled(false); //only one file can be selected at once
			fc.setVisible(true);
			if(returnVal==JFileChooser.APPROVE_OPTION){
				tempFile=fc.getSelectedFile();
				tff=new ToFriendFile(tempFile);
				ClientThread.addToFriendFile(tff);  //add to list
				sendFileKey=tff.getID();
				
				data=new ServerClientData("friendWantSendFile",currentClientID,sendFileKey,tempFile.getName());
				
				if(messageToFriend==null){  //have not select any friend,maybe server has quited during file-selection
					beep();
					JOptionPane.showMessageDialog(frame,"You  haven't select any friend!",
							"No selected friend",JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				
				dataToFriend(messageToFriend.getIP(),data); //send send-file request to friend
			}
		}
		
		else if(type.equals(ABOUT)){
			JOptionPane.showMessageDialog(frame,"   This system is designed for study and communicate.\n" +
												"You can modify it in whatever way you want , but no \n" +
												"commercial use is permitted.\n"+
												"   Author: 张文钊  from ZZULI\n" +
												"   E-Mail: zw_z7@hotmail.com",
												"About this system",JOptionPane.INFORMATION_MESSAGE);
		}
		
		else{  //**want to exit
			exit();
		}
	}
	
	public void valueChanged(ListSelectionEvent lse){
		int rows[]=friendList.getSelectedIndices();
		if(rows.length==1){
			messageToFriend=(FriendsData)listModel.get(rows[0]);
			toFriendLabel.setText("To friend, ID:"+messageToFriend.getID());
		}
	}
	
	public void keyTyped(KeyEvent ke){
		//System.out.println("keyEvent");
		//KeyEvent key=(KeyEvent)ke.getSource();
		//int x=ke.getKeyCode();
		//System.out.println("x:"+x+"		"+KeyEvent.VK_ENTER);
		//System.out.println("keyChar:"+ke.getKeyChar());
		if(ke.getKeyChar()=='\n'){
			sendMessageToFriend();
		}
	}
	public void keyPressed(KeyEvent ke){		
	}
	public void keyReleased(KeyEvent ke){		
	}
	
	public void windowClosing(WindowEvent we){ //before quit inform the server
		exit();
	}
	//when receive some online friend info from server,this method is called from ClientThread to updata friend-list
	synchronized static void updateFriendList(LinkedList<FriendsData> tempList){	
		synchronized(friendObjectList){
			if(updatingList){
				try{
					friendObjectList.wait();
				}catch(InterruptedException e){
					
				}
			}
			updatingList=true;
			
			friendObjectList.addAll(tempList);
			
			FriendsData fd;
			for(int i=0;i<tempList.size();i++){
				fd=tempList.get(i);
				listModel.addElement(fd);
			}
			
			friendLabel.setText("Friend Online!");
			for(int i=0;i<3;i++){			
				friendLabel.setForeground(Color.RED);
				beep();
				try{
					Thread.sleep(300);
				}catch(InterruptedException e){
					
				}
				friendLabel.setForeground(Color.BLACK);
			}
			
			updatingList=false;
			friendObjectList.notifyAll();
		}
		
	}
	//call from ClientFriendGUI to del online friend if there is any
	//and del all in the list in FriendsDatabase
	static void delOnlineFriend(LinkedList<Long> tempList){
		//System.out.println("delOnlineFriend called");
		int index;
		long id;
		for(int i=0;i<tempList.size();i++){			
			id=tempList.get(i).longValue();
			Client.getFriendsDatabase().delFriend(id); //del from FriendsDatabase
			index=Collections.binarySearch(friendObjectList,new FriendsData(id));
			if(index>=0) //if this to-be-del friend online
				oneFriendQuit(id);
		}
	}
	//receive quit friend's id from ClientThread to update friend-list
	synchronized static void oneFriendQuit(long friendID){		
		synchronized(friendObjectList){
			if(updatingList){
				try{
					friendObjectList.wait();
				}catch(InterruptedException e){
					
				}
			}
			updatingList=true;
			
			FriendsData fd=new FriendsData(friendID);
			int index=Collections.binarySearch(friendObjectList,fd);
			if(index<0){
				updatingList=false;
				friendObjectList.notifyAll();
				return;
			}
			else{
				friendObjectList.remove(index);
				listModel.remove(index);
				friendLabel.setText("Friend, ID:"+friendID+" quit!");
				friendLabel.setForeground(Color.BLACK);
				//System.out.println("removing friend:"+friendID);  //**test
			}
			if(listModel.size()==0){
				friendLabel.setText("No friend online!");
				toFriendLabel.setText("To friend, ID:");
			}
			
			updatingList=false;
			friendObjectList.notifyAll();
		}		
	}
	
	//called from ClientThread when receive info from friend
	synchronized static void friendInfo(String info,long friendID,String friendName){		
		synchronized(messageField){
			if(updatingMessage){
				try{
					messageField.wait();
				}catch(InterruptedException e){
					
				}
			}
			updatingMessage=true;
			
			//System.out.println("info:"+info); //**test
			messageField.append("\n"+friendName+" ,ID  "+friendID+": "+info);
			
			messageLabel.setText("Info from ID: "+friendID);
			for(int i=0;i<3;i++){
				messageLabel.setForeground(Color.RED);
				beep();
				try{
					Thread.sleep(300);
				}catch(InterruptedException e){
					
				}
				messageLabel.setForeground(Color.BLACK);
			}
			
			updatingMessage=false;
			messageField.notifyAll();
		}		
	}
	//friend want to send file
	synchronized static void sendFileRequest(String fileName,InetAddress ip,int sendFileKey,long friendID,String friendName){
		new Thread(new SaveFileGUI(fileName,ip,sendFileKey,friendID,friendName)).start();
	}

⌨️ 快捷键说明

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