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

📄 clientfriendgui.java

📁 JAVA实现的聊天工具,可以容纳最多10个用户 1.本系统需要JDK1.5 或更高版本的支持。 2.serverDatabase为服务器端的数据文件. 若使用现有数据,可用帐号:1, 密码
💻 JAVA
字号:
//called from ClientControlGUI to add/del friend by sending request to server
import java.io.*;
import java.net.*;
import java.util.LinkedList;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClientFriendGUI extends WindowAdapter implements Runnable,ActionListener{
	//private static final String ADD="add";
	//private static final String DEL="del";
	private static final String ANOTHER="another";
	private static final String FINISH="finish";
	private static final String EXIT="exit";
	private String task,serverAddress; //add or delete
	private JFrame frame;
	private JLabel statusLabel;
	private JTextField idField;
	private boolean connecting;
	private long currentClientID;
	
	private Socket serverSocket; //use port 4444
	private ObjectOutputStream oosServer; //stream to communicate with server
    private DataOutputStream dos;
	private ByteArrayOutputStream baos;
	private byte[] byteArray;
	private List<Long> tempIDList; //id list to add or del
	
	public ClientFriendGUI(String task,String serverAddress,long id){
		this.task=task;
		this.serverAddress=serverAddress;
		tempIDList=new LinkedList<Long>();
		currentClientID=id;
	}
	
	public void run(){
		if(task.equals("add"))
			frame=new JFrame("Add friends");
		else
			frame=new JFrame("Delete friends");
		frame.setDefaultLookAndFeelDecorated(true);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setContentPane(makeMainPane());
		frame.addWindowListener(this);
		frame.setResizable(false);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae){
		String type=ae.getActionCommand();
		if(type.equals(ANOTHER)){
			long friendID;
			try{
				friendID=Long.parseLong(idField.getText());
			}catch(NumberFormatException e){
				JOptionPane.showMessageDialog(frame,"Input not valid!",
						"Warning",JOptionPane.WARNING_MESSAGE);
				idField.setText("");
				return;
			}
			if(friendID<=0){
				JOptionPane.showMessageDialog(frame,"ID cannot less than one!",
						"Warning",JOptionPane.WARNING_MESSAGE);
				idField.setText("");
				return;
			}
			if(friendID==currentClientID){
				JOptionPane.showMessageDialog(frame,friendID+" is your own ID!",
						"Warning",JOptionPane.WARNING_MESSAGE);
				idField.setText("");
				return;
			}
			tempIDList.add(new Long(friendID));
			idField.setText("");
			idField.requestFocusInWindow();
		}
		else if(type.equals(FINISH)){
			if(connecting){
				return;
			}
			
			long friendID=0;
			boolean valide=true;
			try{
				friendID=Long.parseLong(idField.getText());
			}catch(NumberFormatException e){
				JOptionPane.showMessageDialog(frame,"Input not valid!",
						"Warning",JOptionPane.WARNING_MESSAGE);
				valide=false;
			}
			if(friendID<0){
				JOptionPane.showMessageDialog(frame,"ID cannot less than one!",
						"Warning",JOptionPane.WARNING_MESSAGE);
				valide=false;
			}
			if(friendID==currentClientID){
				JOptionPane.showMessageDialog(frame,friendID+" is your own ID!",
						"Warning",JOptionPane.WARNING_MESSAGE);
				valide=false;
			}
			if(valide && friendID!=0){
				//System.out.println("finish:"+friendID); //**test
				tempIDList.add(new Long(friendID));
			}
			
			if(tempIDList.size()==0){
				JOptionPane.showMessageDialog(frame,"You have not input any valid ID!",
						"Warning",JOptionPane.WARNING_MESSAGE);
				return;
			}
			connecting=true;
			statusLabel.setText("Sending Request");
			statusLabel.setForeground(Color.BLACK);
			if(!initiateSocketStreams(serverAddress)){
				connecting=false;
				statusLabel.setText("Server has failed!");
				statusLabel.setForeground(Color.RED);
				return;
			}
			String taskType;
			if(task.equals("add"))
				taskType="addFriends"; //send to server
			else
				taskType="delFriends";
			ServerClient friend=new ServerClientData(taskType,currentClientID,tempIDList);
			try{
				oosServer.flush();
				oosServer.writeObject(friend); //send list to server
				oosServer.flush();
				byteArray=baos.toByteArray();
				dos.write(byteArray,0,byteArray.length);
			}catch(IOException e){
				System.out.println(e+" ClientControlThread "+e.getMessage());
			}
			closeSocketStreams();
			connecting=false;
			JOptionPane.showMessageDialog(frame,"Request has been sent to server!",
					"Successful",JOptionPane.INFORMATION_MESSAGE);
			
			//ClientControlGUI.delOnlineFriend((LinkedList<Long>)tempIDList);
			frame.dispose();
			ClientControlGUI.friendGUIExit();
		}
		else{ //exit
			frame.dispose();
			ClientControlGUI.friendGUIExit();
		}
	}
	
	public void windowClosing(WindowEvent we){
		frame.dispose();
		ClientControlGUI.friendGUIExit();
	}
	
	private Container makeMainPane(){
		Container original=new JPanel();
		original.setLayout(new BoxLayout(original,BoxLayout.Y_AXIS));
		JPanel inputPane=new JPanel(new GridLayout(1,2));
		statusLabel=new JLabel("Input a friend's ID to "+task+":");
		statusLabel.setHorizontalAlignment(SwingUtilities.CENTER);
		idField=new JTextField();
		inputPane.add(statusLabel);
		inputPane.add(idField);
		
		JPanel buttonPane=new JPanel(new GridLayout(1,3));
		JButton button=new JButton("Another");
		button.setToolTipText("To input another friend's ID.");
		button.setActionCommand(ANOTHER);
		button.addActionListener(this);
		buttonPane.add(button);
		button=new JButton("Finish");
		button.setToolTipText("Send request to server");
		button.setActionCommand(FINISH);
		button.addActionListener(this);
		buttonPane.add(button);
		button=new JButton("Exit");
		button.setActionCommand(EXIT);
		button.addActionListener(this);
		buttonPane.add(button);
		
		idField.requestFocusInWindow();
		original.add(inputPane);
		original.add(Box.createVerticalStrut(10));
		original.add(buttonPane);
		return original;
	}
	
	private boolean initiateSocketStreams(String serverAddress){ 
		try{
			serverSocket=new Socket(serverAddress, 4444);  //construct connection to server			
			baos=new ByteArrayOutputStream(6000);
			oosServer=new ObjectOutputStream(baos);
			dos=new DataOutputStream(serverSocket.getOutputStream());	
		}catch(ConnectException e){
			JOptionPane.showMessageDialog(frame,"Server hasn't been set up!",
					"Connection Failed",JOptionPane.INFORMATION_MESSAGE);
			//System.out.println("Server hasn't been set up!");
			return false;
		}
		catch(UnknownHostException e){
			JOptionPane.showMessageDialog(frame,"Cannot connect to server:"+serverAddress,
					"Connection Failed",JOptionPane.INFORMATION_MESSAGE);
			//System.out.println(e+" ClientControlThread "+e.getMessage());
			return false;
		}catch(IOException e){
			JOptionPane.showMessageDialog(frame,"IOException cause connection failed!",
					"Connection Failed",JOptionPane.INFORMATION_MESSAGE);
			//System.out.println(e+" ClientControlThread "+e.getMessage());
			return false;
		}
		return true;		
	}
	
	private void closeSocketStreams(){
		try{
			serverSocket.close();
			baos.close();
			oosServer.close();
			dos.close();
		}catch(IOException e){
			System.out.println(e+" ClientControlThread "+e.getMessage());
		}
	}
}

⌨️ 快捷键说明

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