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

📄 instantmessagewindow.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on Nov 1, 2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.GTADS.client;

import java.util.*;

import java.awt.*;
import java.awt.event.*;

import org.GTADS.messenger.*;
import org.GTADS.protocol.*;
import org.GTADS.usermanager.TranslateManager;

import java.io.*;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.ScrollPaneConstants;

import org.GTADS.helper.*;
import org.GTADS.client.swing.*;

/**
 * @author sday
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class InstantMessageWindow extends GTADSFrame implements ActionListener, KeyListener{
	public static Hashtable instances = new Hashtable();
	private TranslateManager locale = TranslateManager.getInstance();
	private GTADSTextArea IMChatTextBox;
	private GTADSTextField IMInputChatBox;
	private GTADSScrollPane IMScroll;
	private GTADSButton IMSendButton = new GTADSButton("Send");
	private GTADSButton IMCloseButton = new GTADSButton("Close");
	public String IMUser;
	private String currentUser = DSChatClient.clientUser.getUsername();
	
	public static InstantMessageWindow getInstance(String instanceKey){
		synchronized(InstantMessageWindow.class){
			if (!instances.containsKey(instanceKey)){
				instances.put(instanceKey,new InstantMessageWindow(instanceKey));
				return InstantMessageWindow.getInstance(instanceKey);
			}
			else if (!(instances.get(instanceKey) instanceof InstantMessageWindow)){
				instances.remove(instanceKey);
				instances.put(instanceKey,new InstantMessageWindow(instanceKey));
				return InstantMessageWindow.getInstance(instanceKey);
			}
			else {
				return (InstantMessageWindow)instances.get(instanceKey);
			}
		}
	}
	
	public static void clearInstance(String instanceKey){
		synchronized(InstantMessageWindow.class){
			if (instances.containsKey(instanceKey)){
				if (instances.get(instanceKey) instanceof InstantMessageWindow){
					((InstantMessageWindow)instances.get(instanceKey)).dispose();
					((InstantMessageWindow)instances.get(instanceKey)).hide();
				}				
				instances.remove(instanceKey);
			}
		}
	}
	
	public static void clearAllInstances(){
		while (!instances.isEmpty()){
			instances.clear();
		}	
	}
	
	public InstantMessageWindow (String IMUser){
		this.IMUser = IMUser;
		GTADSPanel MainPanel = new GTADSPanel();
		GTADSPanel ChatBoxPanel = new GTADSPanel();
		GTADSPanel InputChatBoxPanel = new GTADSPanel();
		GTADSPanel ButtonsPanel = new GTADSPanel();
		
		this.setTitle(locale.printLocale("IM Session with") + " " + IMUser);
		
		MainPanel.setLayout(new BoxLayout(MainPanel, BoxLayout.Y_AXIS));
		ChatBoxPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		InputChatBoxPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		ButtonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		setupComponents();
		
		ChatBoxPanel.add(IMScroll);
		
		InputChatBoxPanel.add(IMInputChatBox);
		
		ButtonsPanel.add(IMSendButton);
		ButtonsPanel.add(Box.createHorizontalStrut(5));
		ButtonsPanel.add(IMCloseButton);
		
		MainPanel.add(ChatBoxPanel);
		MainPanel.add(Box.createVerticalStrut(10));
		MainPanel.add(InputChatBoxPanel);
		MainPanel.add(Box.createVerticalStrut(5));
		MainPanel.add(ButtonsPanel);

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(500,290);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent e){
				InstantMessageWindow.clearInstance(((InstantMessageWindow)e.getSource()).IMUser);
			}
		});
		
		this.setContentPane(MainPanel);
		this.show();
	}
	
	private void setupComponents(){
		this.setResizable(false);
		IMChatTextBox = new GTADSTextArea();
		IMInputChatBox = new GTADSTextField();
		
		IMChatTextBox.setColumns(40);
		IMChatTextBox.setRows(10);
		IMChatTextBox.setLineWrap(true);
		IMChatTextBox.setAutoscrolls(true);
		IMChatTextBox.setEditable(false);
		IMChatTextBox.setText("* " + locale.printLocale("Starting IM Conversation with") + " " + IMUser);
		
		 IMScroll = new GTADSScrollPane(IMChatTextBox, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		 IMScroll.setAutoscrolls(true);
		
		IMInputChatBox.setColumns(40);
		IMInputChatBox.addKeyListener(this);
		
		IMSendButton.addActionListener(this);
		IMCloseButton.addActionListener(this);
	}
	
	public void appendText(String sender, String text){
		String newIM = "(" + Helper.getDate(false) + ")" + " " + sender + ": " + text;
		IMChatTextBox.setText(IMChatTextBox.getText() + "\n" + newIM);
		int end = IMChatTextBox.getText().length();
		IMChatTextBox.select(end,end);
	}
	
	public void sendIMToUser(String user, String text){
		if (IMInputChatBox.getText() != ""){
			try {
				MetaData IMHeader = new MetaData(currentUser, MetaData.CHATROOM, user);
				MessageAdapter.sendData(null, DSChatClient.ClientSocket, IMHeader, text);
				IMInputChatBox.setText("");
			} catch (IOException ioe){
				return;
			}
		}
	}
	
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == IMSendButton) {
			sendIMToUser(IMUser, IMInputChatBox.getText());
		}
		if (e.getSource() == IMCloseButton){
			this.dispose();
			InstantMessageWindow.clearInstance(IMUser);
		}
	}
	
	public void keyPressed (KeyEvent key){
		if (key.getKeyCode() == KeyEvent.VK_ENTER){
			sendIMToUser(IMUser, IMInputChatBox.getText());
		}
	}
	public void keyTyped(KeyEvent key){}
	public void keyReleased(KeyEvent key){}
	
	public static void main(String[] args) {
		InstantMessageWindow.getInstance("Joe");
		InstantMessageWindow.getInstance("Jane");
		InstantMessageWindow.getInstance("Harry");
	}
}

⌨️ 快捷键说明

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