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

📄 clientcontrolgui.java

📁 JAVA实现的聊天工具,可以容纳最多10个用户 1.本系统需要JDK1.5 或更高版本的支持。 2.serverDatabase为服务器端的数据文件. 若使用现有数据,可用帐号:1, 密码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	
	static void friendGUIExit(){
		isFriendStarted=false;
		clientFriendGUI.stop();
	}
	
	static void serverExit(){
		connecting=false;
		beep();
		titleLabel.setForeground(Color.red);
		statusLabel.setText("Server is down!");
		statusLabel.setForeground(Color.RED);
		messageToFriend=null;
		friendObjectList.removeAll(friendObjectList);
		listModel.removeAllElements();
		ClientThread.clearFileList(); //clear file list
	}
	
	private Container makeMainPane(){
		Container original=new JPanel();
		original.setLayout(new BoxLayout(original,BoxLayout.Y_AXIS));
		JPanel statusPane=new JPanel(new GridLayout(3,1));
		titleLabel=new JLabel("Status:");	
		if(initiate){
			titleLabel.setForeground(Color.GREEN);
			statusLabel=new JLabel("Listening for messages.");
			statusLabel.setForeground(Color.GREEN);
		}
		else{ //port is pre-occupied
			titleLabel.setForeground(Color.RED);
			statusLabel=new JLabel("Cannot listen on port 4445 for message!");
			statusLabel.setForeground(Color.RED);
		}
		titleLabel.setHorizontalAlignment(SwingUtilities.CENTER);
		statusLabel.setHorizontalAlignment(SwingUtilities.CENTER);
		statusPane.add(titleLabel);
		statusPane.add(statusLabel);
		JLabel label=new JLabel("Current client's ID:"+currentClientID);
		label.setHorizontalAlignment(SwingUtilities.CENTER);
		statusPane.add(label);
		
		JPanel buttonPane=new JPanel(new GridLayout(2,5));
		JButton button=new JButton("Send File");
		button.setActionCommand(SENDFILE);
		button.addActionListener(this);
		button.setToolTipText("To send a file to a friend.");
		buttonPane.add(button);
		buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
		buttonPane.add(button);
		buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
		button=new JButton("Add");
		button.setActionCommand(ADD);
		button.addActionListener(this);
		button.setToolTipText("To add some friends.");
		buttonPane.add(button);
		buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
		button=new JButton("Send Info");
		button.setActionCommand(SENDINFO);
		button.addActionListener(this);
		button.setToolTipText("Send message to a friends.");
		buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
		buttonPane.add(button);
		buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
		button=new JButton("Delete");
		button.setActionCommand(DEL);
		button.addActionListener(this);
		button.setToolTipText("To delete some friends.");
		buttonPane.add(button);
		buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
		
		//panel to monitor and send message with online friends list
		JSplitPane monitorPane;
		//monitorPane.setLayout(new BoxLayout(monitorPane,BoxLayout.X_AXIS));  =new JPanel()
		//pane to monitor and send message
		JSplitPane messagePane;
		JPanel superMessagePane=new JPanel();
		superMessagePane.setLayout(new BoxLayout(superMessagePane,BoxLayout.Y_AXIS));
		JPanel pane1=new JPanel();
		pane1.setLayout(new BoxLayout(pane1,BoxLayout.Y_AXIS));
		toFriendLabel=new JLabel("To friend, ID:");
		toFriendField=new JTextArea(3,10);  //write message to be sent to friend
		toFriendField.addKeyListener(this);
		JScrollPane scrollPane1=new JScrollPane(toFriendField,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pane1.add(toFriendLabel);
		pane1.add(scrollPane1);		
		JPanel pane2=new JPanel();
		pane2.setLayout(new BoxLayout(pane2,BoxLayout.Y_AXIS));
		messageLabel=new JLabel("Messages from friends:");
		messageField=new JTextArea(5,10); //to display message from friend
		messageField.setEditable(false);
		JScrollPane scrollPane2=new JScrollPane(messageField,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pane2.add(messageLabel);
		pane2.add(scrollPane2);
		messagePane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,pane1,pane2);
		messagePane.setOneTouchExpandable(true);
		JLabel tempLabel=new JLabel("Message Pane");
		tempLabel.setHorizontalAlignment(SwingUtilities.CENTER);
		superMessagePane.add(tempLabel);
		superMessagePane.add(messagePane);
		//messagePane.add(toFriendLabel);
		//messagePane.add(scrollPane1);
		//messagePane.add(messageLabel);
		//messagePane.add(scrollPane2);
		//pane to list online friends
		JPanel friendPane=new JPanel();
		friendPane.setLayout(new BoxLayout(friendPane,BoxLayout.Y_AXIS));
		//if(Client.getFriendsDatabase().haveFriends())  //
		friendLabel=new JLabel("No online friend");
		listModel=new DefaultListModel();
		friendList=new JList(listModel);
		friendList.addListSelectionListener(this);
		JScrollPane scrollPane3=new JScrollPane(friendList,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		//scrollPane3.setPreferredSize(new Dimension(100,80));
		//System.out.println(scrollPane3.getPreferredSize());
		friendPane.add(friendLabel);
		friendPane.add(scrollPane3);
		//add to monitor pane		
		monitorPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,messagePane,friendPane);
		monitorPane.setDividerLocation(250);  //set split-pane location
		monitorPane.setOneTouchExpandable(true);
		JPanel superMonitorPane=new JPanel();
		superMonitorPane.add(monitorPane);
		
		//monitorPane.setOneTouchExpandable(true);		
		//monitorPane.add(superMessagePane);		
		//monitorPane.add(Box.createHorizontalStrut(5));
		//monitorPane.add(friendPane);
		
		//monitorPane.setPreferredSize(new Dimension(monitorPane.getPreferredSize().width,messagePane.getPreferredSize().height));
		
		//buttonPane.setPreferredSize(new Dimension(monitorPane.getPreferredSize().width,buttonPane.getPreferredSize().height));
		original.add(statusPane);
		original.add(Box.createVerticalStrut(5));
		original.add(buttonPane);
		original.add(Box.createVerticalStrut(5));
		original.add(superMonitorPane);
		//original.setPreferredSize(new Dimension(200,300));
		return original;
	}
	
	private JMenuBar makeMenuBar(){
		JMenuBar menuBar=new JMenuBar();
		JMenu menu=new JMenu("File");
		menu.setMnemonic(KeyEvent.VK_F);
		JMenuItem menuItem=new JMenuItem("Exit");
		menuItem.setMnemonic(KeyEvent.VK_E);
		menuItem.setActionCommand(EXIT);
		menuItem.addActionListener(this);
		menu.add(menuItem);
		menuItem=new JMenuItem("About this system");
		menuItem.setActionCommand(ABOUT);
		menuItem.addActionListener(this);
		menu.addSeparator();
		menu.add(menuItem);
		menuBar.add(menu);
		
		menu=new JMenu("Edit");
		menu.setMnemonic(KeyEvent.VK_E);
		menuItem=new JMenuItem("Add Friend");
		menuItem.setMnemonic(KeyEvent.VK_A);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.ALT_MASK));
		menuItem.setActionCommand(ADD);
		menuItem.addActionListener(this);
		menu.add(menuItem);
		menu.addSeparator();
		menuItem=new JMenuItem("Del Friend");
		menuItem.setMnemonic(KeyEvent.VK_D);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,ActionEvent.ALT_MASK));
		menuItem.setActionCommand(DEL);
		menuItem.addActionListener(this);
		menu.add(menuItem);
		menuBar.add(menu);		
		
		return menuBar;
	}
	
	private void dataToFriend(String tempIP,ServerClient data){
		if(!connecting){
			beep();
			JOptionPane.showMessageDialog(frame,"Connection to server failed!",
					"Error",JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		byte[] tempByteArray;
		try{
			Socket toFriendSocket=new Socket(tempIP,4445);
			ByteArrayOutputStream tempBaos=new ByteArrayOutputStream(6000);
			ObjectOutputStream tempOos=new ObjectOutputStream(tempBaos);
			tempOos.flush();
			tempOos.writeObject(data);
			tempOos.flush();
			tempByteArray=tempBaos.toByteArray();
			DataOutputStream tempDos=new DataOutputStream(toFriendSocket.getOutputStream());
			tempDos.write(tempByteArray,0,tempByteArray.length);
			
			tempBaos.close();
			tempOos.close();
			tempDos.close();
			toFriendSocket.close();
		}catch(ConnectException e){
			JOptionPane.showMessageDialog(frame,"Sorry,friend(ID: "+messageToFriend.getID()+") has quited.",
					"Connection failed",JOptionPane.ERROR_MESSAGE);
			//System.out.println("Sorry,friend(ID: "+messageToFriend.getID()+") has quited.");
		}
		catch(UnknownHostException e){
			JOptionPane.showMessageDialog(frame,"IP address isn't valid!",
					"Connection failed",JOptionPane.ERROR_MESSAGE);
			//System.out.println(e+" ClientControlThread "+e.getMessage());
		}
		catch(IOException e){
			System.out.println(e+" ClientControlThread "+e.getMessage());
		}
	}
	
	private void sendMessageToFriend(){		
		if(messageToFriend==null){
			beep();
			JOptionPane.showMessageDialog(frame,"You  haven't select any friend!",
					"No selected friend",JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		
		String tempIP=messageToFriend.getIP();
		String info=toFriendField.getText();
		info=String.valueOf(currentClientID)+","+info;
		ServerClient data=new ServerClientData("friendWantChat",info);
		
		dataToFriend(tempIP,data);
		
		toFriendField.setText("");
	}
	
	private boolean initiateSocketStreams(String serverAddress){ 
		try{serverSocket=new Socket(serverAddress, 4444);  //construct connection to server		
			//oosServer=new ObjectOutputStream(/*new BufferedOutputStream(*/
					//serverSocket.getOutputStream());
			//oisServer=new ObjectInputStream(/*new BufferedInputStream(*/
					//serverSocket.getInputStream());
			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.WARNING_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.WARNING_MESSAGE);
			//System.out.println(e+" ClientControlThread "+e.getMessage());
			return false;
		}catch(IOException e){
			JOptionPane.showMessageDialog(frame,"IOException cause connection failed!",
					"Connection Failed",JOptionPane.WARNING_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());
		}
	}
	
	private void exit(){
		int choice=JOptionPane.showConfirmDialog(frame,
				"Do you really want to quit?",
				"Exit Confirm",
				JOptionPane.YES_NO_OPTION);
		if(choice==1)   //do not want to del
			return;
		
		if(!connecting || !initiateSocketStreams(serverAddress)){
			Client.exit();
			return;			
		}
		//System.out.println("test");
		ServerClient quit=new ServerClientData("clientQuit",currentClientID);
		try{
			oosServer.flush();
			oosServer.writeObject(quit);
			oosServer.flush();
			byteArray=new byte[6000];
			byteArray=baos.toByteArray();
			dos.write(byteArray,0,byteArray.length);
		}catch(IOException e){
			JOptionPane.showMessageDialog(frame,"IOException:"+e.getMessage(),
					"ClientControlThread",JOptionPane.ERROR_MESSAGE);
			//System.out.println(e+" ClientControlThread "+e.getMessage());
		}
		closeSocketStreams();
		Client.exit();
	}
	
	private static void beep(){
		Toolkit.getDefaultToolkit().beep();
	}
}

⌨️ 快捷键说明

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