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

📄 chatdisplay.java

📁 一个实现网络会议的软件.包含三个包.其中一个包需JMF的支持.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    		publicTextPane.setText(publicText);
    		break;
	case 1:
		privateText = privateText + mySelf + 
	                 	 " (" + userComboBox.getSelectedItem().toString() + ") " +
	                 	 ": " + msg + "\n";
    		privateTextPane.setText(privateText);
    		v.addElement(userComboBox.getSelectedItem().toString());
    		break;
    	};
    	v.addElement(new Integer(paneType));
    	propertySupport.firePropertyChange("broadcastMessage",null,v);
    }
    
	/**
	  * makes an object to be added to Combo Box.
	  * The string is converted into an object to avoid errors due to
	  * duplicate strings
	  *
	  * @param item The item that has to be converted into object
	  * @return Returns the newly create object
	  */    
    private Object makeObj(final String item)  {
        /**
          * Creates a wrapper object for the given string
          */
        return new Object() { 
            /**
              * This method returns the string contained in the object
              *
              * @return The string contained in the object
              */
            public String toString() { 
                return item; 
            } 
        };
    }
    
    
    /**
      * enables or disables components depending on the state
      * of the chat display.
      */
    private void enableComponents() {
    	switch (paneType) {
	case 1:
	    	if (userComboBox.getItemCount() == 0) {
    			userComboBox.setEnabled(false);
    			sendButton.setEnabled(false);
    			messageText.setEnabled(false);
    		} else {
	    		userComboBox.setEnabled(true);
				sendButton.setEnabled(true);
				messageText.setEnabled(true);
    		}
    		break;
    	case Constants.PUBLIC_CHAT:
    		userComboBox.setEnabled(false);
			sendButton.setEnabled(true);
			messageText.setEnabled(true);
    		break;
    	}
    }
    
    /**
     * sets the current user as the presenter
     */
    public void setPresenter() {
		myCategory = Constants.PRESENTER;
    		int i = 0;
    		for (i = 0; i < usersVector.size(); i++) {
    			String userName = (String) usersVector.elementAt(i);
	   		userComboBox.addItem(makeObj(userName));
    		}
    		enableComponents();
    }

	/**
	 * clears the public chat
	 */
	public void clearPublicChat() {
    		publicText = "";
   		publicTextPane.setText(publicText);
	}

	/**
	 * clears the private chat
	 */	
	public void clearPrivateChat() {
    		privateText = "";
   		privateTextPane.setText(privateText);
	}

	/**
	 * refresh all the messages 
	 */
	public void refresh(Vector msgList) {
		
		if (paneType == 1)  {
	    		JOptionPane.showMessageDialog(this, 
						    "Private Message can't be refreshed!",
						    "Warning", JOptionPane.WARNING_MESSAGE);
		}
		
		if (msgList == null) 
		{ 			
			return;
		}
		clearPublicChat();
		for (int i = 0; i < msgList.size(); i++) {
			Vector v = (Vector) msgList.elementAt(i);

	   		/**
	   	  	 * This is the message sent from the chatDisplay
	   	  	 */
	   		String msg = (String) v.get(0);
	   		String fromName = (String) v.get(1);


	   		/**
	   	  	 * This is the user name towards whom the message has to be directed
	   	  	 */
	   	  	String toName = (String) v.get(2);
	   		/**
	   	  	 * This is the type of the message (public or private)
	   	  	 */
	   		int msgType = ((Integer) v.get(3)).intValue();

	    	if (!(toName.equals("") || toName.equals(mySelf) || fromName.equals(mySelf))) continue;		
		
	    	switch (msgType) {
	    	case Constants.PUBLIC_CHAT:
		    	publicText = publicText + fromName + ": " + msg + "\n";
	    		publicTextPane.setText(publicText);
	    		break;
	    	case Constants.PRIVATE_CHAT:		    	
		    	privateText = privateText + fromName + " To: " + toName + ": " + msg + "\n";
	    		privateTextPane.setText(privateText);
	    		break;
	    	};	
	   }
	}
    
    /**
      * when some action is performed on the registered components
      *
      * @param e The event that caused the method to be called
      */
    public void actionPerformed(ActionEvent e) {
		if ((e.getSource() == sendButton) || (e.getSource() == messageText)) {
    		sendMessage(messageText.getText());
    		messageText.setText("");
    	}
    }
    
    /**
      * when the state of the tabbed pane is changed.
      * Specifically when the user switch between public and private chat
      *
      * @param e The event that caused this method to be called
      */
    public void stateChanged(ChangeEvent e) {    	
    	paneType = textTabbedPane.getSelectedIndex();
    	enableComponents();
    }

	/**
	  * registers a Listener that has to be notified on change of some property
	  *
	  * @param listener The Listener that has to be notified
	  */
    public void addPropertyChangeListener (PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener (listener);
    }

	/**
	  * removes a Listener from the list that are notified on property change
	  *
	  * @param listener The Listener that has to be removed
	  */
    public void removePropertyChangeListener (PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener (listener);
    }

    /**
     * save the chat. Saves Public chat first then the private chat.
     * @param String The filename in which to store the chat.
     */
    public void saveChat(String strfileName)
    {
    		if (strfileName == null)
    		{
    			return;
    		}
    		FileOutputStream fos = null;
    		try
    		{
    			fos = new FileOutputStream(strfileName);
    		}
    		catch(FileNotFoundException fnfe)
    		{
    			showMessage("The file : " + strfileName + " was not found.");
    			fnfe.printStackTrace();
    		}
    	
		try
		{
	    		String  msg =  "_________________________________________________\n";
	    			msg += "            Public Chat Starts Here\n";
	    			msg += "_________________________________________________\n";
	    	byte[] byteMessage = msg.getBytes();
	    	
	    	fos.write(byteMessage);
	    	for (int i = 0; i < publicText.length(); i++)
	    	{
	    		fos.write(publicText.charAt(i));
	    	}
	    	
	      msg =  "\n_________________________________________________\n";
			msg += "            Public Chat Ends Here\n";
			msg += "_________________________________________________\n";
	    	
	    	byteMessage = msg.getBytes();
	    	fos.write(byteMessage);
	    		    	
	     msg = "\n\n_________________________________________________\n";
	    	msg += "            Private Chat Starts Here\n";
	    	msg += "_________________________________________________\n";
	    	
	    	byteMessage = msg.getBytes();
	    	fos.write(byteMessage);
	    	for (int j = 0; j < privateText.length(); j++)
	    	{
	    		fos.write(privateText.charAt(j));
	    	}
	    	
   	   	msg = "\n_________________________________________________\n";
	    	msg += "            Private Chat Ends Here\n";
	    	msg += "_________________________________________________\n";
	    	
	    	byteMessage = msg.getBytes();
	    	fos.write(byteMessage);
	    	fos.close();
	    }
	    catch(IOException ioe)
	    {
	    	showMessage("An error occured while saving the chat");
	    	ioe.printStackTrace();
	    }
	    
	 }
    
    /** show message*/
    private void showMessage(String msg)
	{
		JOptionPane.showMessageDialog(this, msg);
	}
}

⌨️ 快捷键说明

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