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

📄 clientapplet.java

📁 java的聊天室代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import java.lang.*;
import java.net.*;

class FileData extends Object implements java.io.Serializable//file send class
{
	byte b[];
	String custName,fileName;
	public void setData(byte d[],String str)
	{
		b=d;
		custName=str;
	}	
}

class Message extends Object implements java.io.Serializable//messageData 
{
	String customerName;
	String talkMessage;
	String personalName;
	int choiceWay;
	Vector nameListMessage;
	public Message()
	{
		customerName=personalName=talkMessage="";
		choiceWay=0;
		nameListMessage=new Vector(1,1);
	}
} 

class LoginPane extends JPanel implements ActionListener  ////loginPanel
{
	ClientApplet temp;
	JLabel loginTitle;
	JLabel labelCustemerName;
	JTextField textCustemerName;
	Button buttonNameTest;
	Button buttonLogin;
	Button buttonExit;
	Font wordFont,buttonFont;
	Color wordColor;
	GridBagLayout gridbagObjLogin;////login layout
 	GridBagConstraints gbcLogin;
 	Message tempMessage; 	 
 	 	 	
 	public LoginPane(ClientApplet a)
	{
		
		temp=a;////////////////////////mainClass use
		/////loginPanel built////////
		tempMessage=new Message();
		gridbagObjLogin=new GridBagLayout();
		gbcLogin=new GridBagConstraints();
	 	setLayout(gridbagObjLogin);
	    setBackground(new Color(0,80,80));
	    
	 	////button and word tpry form/////
	 	wordFont=new Font("楷体_GB2312",Font.BOLD,22);
	 	buttonFont=new Font("楷体_GB2312",Font.BOLD,18);
	 	wordColor=new Color(84,217,245);
	 	
	 	/////////login button built////////	 	
	 	loginTitle=new JLabel("欢迎来到了月聊天室!");
	 	loginTitle.setFont(wordFont);
	 	loginTitle.setForeground(wordColor);
	 	
		labelCustemerName=new JLabel("昵称:");
		labelCustemerName.setFont(wordFont);
		labelCustemerName.setForeground(wordColor);
		
		textCustemerName=new JTextField(10);
		
		buttonNameTest=new Button("测试");
		buttonNameTest.setFont(buttonFont);
		
		buttonLogin=new Button("进入聊天室");
		buttonLogin.setFont(buttonFont);
		
		buttonExit=new Button("退出");
		buttonExit.setFont(buttonFont);
		
		//////loginPanel typeset////////////
		gbcLogin.gridx=0;
	 	gbcLogin.gridy=0;
	 	gbcLogin.gridwidth=4;
	 	gbcLogin.fill=GridBagConstraints.CENTER;
	 	gridbagObjLogin.setConstraints(loginTitle,gbcLogin);
	    add(loginTitle);
		
		gbcLogin.gridx=0;
	 	gbcLogin.gridy=1;
	 	gbcLogin.gridwidth=1;
	  	gridbagObjLogin.setConstraints(labelCustemerName,gbcLogin);
	    add(labelCustemerName);
		
		gbcLogin.gridx=1;
	 	gbcLogin.gridy=1;
	  	gridbagObjLogin.setConstraints(textCustemerName,gbcLogin);			
		add(textCustemerName);			
				
		gbcLogin.gridx=2;
	 	gbcLogin.gridy=1;
	  	gridbagObjLogin.setConstraints(buttonNameTest,gbcLogin);
		add(buttonNameTest);
		
		gbcLogin.gridx=0;
	 	gbcLogin.gridy=2;
	 	gridbagObjLogin.setConstraints(buttonLogin,gbcLogin);
		add(buttonLogin);
		
		gbcLogin.gridx=1;
	 	gbcLogin.gridy=2;
	 	gridbagObjLogin.setConstraints(buttonExit,gbcLogin);
		add(buttonExit);
		
		//////button event/////////
		buttonLogin.addActionListener(this);
		buttonExit.addActionListener(this);
		buttonNameTest.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e)////inface event///
	{
	
		Object te=new Object();
		te=e.getSource();
				
		if(te==buttonNameTest)
		{
			String LoginCustomerName=textCustemerName.getText();
			
			if(LoginCustomerName.equals(""))
			{
				temp.warmShowMessage.showMessageDialog(this,"Null LoginName","LoginWarning",JOptionPane.WARNING_MESSAGE);
				return;
			}
			for(int i=0;i<temp.nameListFromServer.size();i++)
			{
				if(String.valueOf(temp.nameListFromServer.elementAt(i)).equals(LoginCustomerName))
				{
					temp.warmShowMessage.showMessageDialog(this,"this name is used","LoginWarning",JOptionPane.WARNING_MESSAGE);
					textCustemerName.setText("");
					return;
				}
			}
			temp.warmShowMessage.showMessageDialog(temp.loginPane,"OK this name no used!","Namewarning",JOptionPane.DEFAULT_OPTION);
		}
		
		if(te==buttonLogin)//login go/////
		{
			temp.talkPaneTalkAreaCenter.textTalkAreaPersonal.setText("");
			temp.talkPaneTalkAreaCenter.textTalkAreaPublic.setText("");
			
			String LoginCustomerName=textCustemerName.getText();
			temp.ClientAppletName=LoginCustomerName;
			
			if(LoginCustomerName.equals(""))
			{
				temp.warmShowMessage.showMessageDialog(this,"Null LoginName","LoginWarning",JOptionPane.WARNING_MESSAGE);
				return;
			}
			temp.cardObj.next(temp.mainPane);
			temp.setCusName(LoginCustomerName);					
			tempMessage.customerName=LoginCustomerName;
			tempMessage.choiceWay=1;
			tempMessage.talkMessage="欢迎"+LoginCustomerName+"进入聊天室";
			try
			{
				ObjectOutputStream buttonLoginMessageToServer=new ObjectOutputStream(temp.toServer.getOutputStream());
				buttonLoginMessageToServer.writeObject(tempMessage);
			}
			catch(IOException p)
			{
				System.out.print(p+"Couldn't Send LoginMessage to Server!");
			}
			textCustemerName.setText("");
		}
		
		if(te==buttonExit)
		{
			System.exit(0);
		}
	} 
}

/////////////TalkPaneTalkAreaCenter///////////////
class TalkPaneTalkAreaCenter extends JPanel
		{
			JPanel textTalkAreaPublicPanel,textTalkAreaPersonalPanel;
			JScrollPane textAreaPublicPane,textTalkAreaPersonalPane;
			JTextArea textTalkAreaPublic,textTalkAreaPersonal;
			JLabel titleTextTalkAreaPublic,titleTextTalkAreaPersonal;
			
			public void setPosPublic()////textTalkAreaPublic roll
			{
				textTalkAreaPublic.setCaretPosition(textTalkAreaPublic.getText().length()-1);
			}
			
			public void setPosPersonal()//////textTalkAreaPersonal roll
			{
				textTalkAreaPersonal.setCaretPosition(textTalkAreaPersonal.getText().length()-1);
			}
			
			public TalkPaneTalkAreaCenter()
			{
				///////TalkPaneTalkAreaCenter layout and controlButton built/////
				setLayout(new GridLayout(2,1));
				
				textTalkAreaPublicPanel=new JPanel();
				textTalkAreaPublicPanel.setLayout(new BorderLayout());
				textTalkAreaPersonalPanel=new JPanel();
				textTalkAreaPersonalPanel.setLayout(new BorderLayout());
				
				titleTextTalkAreaPublic=new JLabel("Public Talk Arear!");
				titleTextTalkAreaPublic.setFont(new Font("楷体_GB2312",Font.BOLD,17));
				titleTextTalkAreaPersonal=new JLabel("Personal Talk Arear!");
				titleTextTalkAreaPersonal.setFont(new Font("楷体_GB2312",Font.BOLD,17));
				
				textTalkAreaPublic=new JTextArea();
				textTalkAreaPublic.setEditable(false);
				textTalkAreaPublic.setWrapStyleWord(true);
				textTalkAreaPublic.setAutoscrolls(true);
				
				textTalkAreaPersonal=new JTextArea();
				textTalkAreaPersonal.setEditable(false);
				textTalkAreaPersonal.setWrapStyleWord(true);
				textTalkAreaPersonal.setAutoscrolls(true);
				
				textAreaPublicPane=new JScrollPane(textTalkAreaPublic);
				textTalkAreaPersonalPane=new JScrollPane(textTalkAreaPersonal);
				
				textTalkAreaPublicPanel.add("North",titleTextTalkAreaPublic);
				textTalkAreaPublicPanel.add("Center",textAreaPublicPane);
				
				textTalkAreaPersonalPanel.add("North",titleTextTalkAreaPersonal);
				textTalkAreaPersonalPanel.add("Center",textTalkAreaPersonalPane);
				 
				add(textTalkAreaPublicPanel);
				add(textTalkAreaPersonalPanel);
			}
		}
		
//////////////nameList class////////////////		
class TalkPaneTalkAreaList extends JPanel implements ListSelectionListener
{
	JList nameList;
	JScrollPane nameListScroll;
	JLabel nameListLabel;
	ClientApplet temp;
	
	public TalkPaneTalkAreaList(ClientApplet a)
	{
		/////////////nameList layout and controlButton built///////////
		temp=a;
		setLayout(new BorderLayout());
		nameList=new JList();
		
	 	nameListScroll=new JScrollPane(nameList);
	 	nameListLabel=new JLabel("Online Customer");
	 	nameListLabel.setFont(new Font("楷体_GB2312",Font.BOLD,17));
	 	 	
	 	add("North",nameListLabel);
	 	add("Center",nameList);
	 	
	 	
	 	nameList.addListSelectionListener(this);
	}
	
	////////list event////////////
	public void valueChanged(ListSelectionEvent e) 
	{
		System.out.print(temp.personalCustName);
		temp.personalCustName=String.valueOf(nameList.getSelectedValue());
	}
}

///////main talkPanel/////////////
class TalkPaneTalkArea extends JPanel
{
	
	public TalkPaneTalkArea()
	{
		setLayout(new BorderLayout());
	}
}

///////talkContorlButtonArea class/////////
class TalkPaneControlPORP extends JPanel implements ActionListener 
{
	JRadioButton personalTalkButton,toAllCustmoer,toSomeOne;
	ButtonGroup bt;
	ClientApplet temp;
	Font wordFont,buttonFont;
	Color wordColor;
	JLabel nowCustomerLabel,nowCustomer;
	public void setCustomerName(String str)
	{
		nowCustomer.setText(str);
	}
	public TalkPaneControlPORP(ClientApplet a)
	{
		////////talkContorlButtonArea layout and controlButton built//////
		setBackground(new Color(0,80,80));
		wordFont=new Font("楷体_GB2312",Font.BOLD,18);
		buttonFont=new Font("楷体_GB2312",Font.BOLD,13);
		wordColor=new Color(84,217,245);
		temp=a;
		
		bt=new ButtonGroup();
		
		nowCustomer=new JLabel(temp.ClientAppletName);
		nowCustomer.setFont(wordFont);
		nowCustomer.setBackground(new Color(0,80,80));
		nowCustomer.setForeground(wordColor);
		
		nowCustomerLabel=new JLabel("当前用户:");
		nowCustomerLabel.setFont(wordFont);
		nowCustomerLabel.setBackground(new Color(0,80,80));
		nowCustomerLabel.setForeground(wordColor);
		
		toAllCustmoer=new JRadioButton("大家",true);
		toAllCustmoer.setFont(buttonFont);
		toAllCustmoer.setBorderPainted(false);
		toAllCustmoer.setBackground(new Color(0,80,80));
		toAllCustmoer.setForeground(wordColor);
		 
		toSomeOne=new JRadioButton("和某某说");
		toSomeOne.setFont(buttonFont);
		toSomeOne.setBorderPainted(false);
		toSomeOne.setBackground(new Color(0,80,80));
		toSomeOne.setForeground(wordColor);
		 
		personalTalkButton=new JRadioButton("私聊");
		personalTalkButton.setFont(buttonFont);
		personalTalkButton.setBorderPainted(false);
		personalTalkButton.setBackground(new Color(0,80,80));
		personalTalkButton.setForeground(wordColor);
	
		bt.add(personalTalkButton);	
		bt.add(toSomeOne);	
		bt.add(toAllCustmoer);	
		
		add(nowCustomerLabel);
		add(nowCustomer); 		
		add(personalTalkButton);
		add(toSomeOne);
		add(toAllCustmoer);
		
		personalTalkButton.addActionListener(this);
		toSomeOne.addActionListener(this);
	 	toAllCustmoer.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		///////any button event//////////
		Object te=new Object();
		te=e.getSource();
				
		if(te==personalTalkButton)
		{
			temp.talkPaneTalkAreaList.nameList.setEnabled(true);
		}
		if(te==toSomeOne)
		{
			temp.talkPaneTalkAreaList.nameList.setEnabled(true);
		}
		if(te==toAllCustmoer)
		{
			temp.talkPaneTalkAreaList.nameList.setEnabled(false);
		}
	}
}

//class TalkPaneControlButton//////////
class TalkPaneControlButton extends JPanel implements ActionListener 
{
			ClientApplet temp;
			JLabel labelInput;
			JTextField textInput;
			Button buttonInput;	
			Button buttonClearPublic,buttonClearPersonal;
			Button buttonBack;
			Button buttonSend;	
			Font wordFont,buttonFont;
			Color wordColor;
			
						
			Message tempMessage;
			
			public TalkPaneControlButton(ClientApplet a)
			{
				tempMessage=new Message();
				temp=a;
				setBackground(new Color(0,80,80));
				wordFont=new Font("楷体_GB2312",Font.BOLD,18);
		 		buttonFont=new Font("楷体_GB2312",Font.BOLD,13);
		 		wordColor=new Color(84,217,245);
		 				 							 		
				labelInput=new JLabel("我想说:");
			 	labelInput.setFont(wordFont);
			 	labelInput.setForeground(wordColor);
			 	
				textInput=new JTextField(20);
				
				buttonInput=new Button("发送");
				buttonInput.setFont(buttonFont);
				
				buttonClearPublic=new Button("公聊区清屏");
				buttonClearPublic.setFont(buttonFont);
				
				buttonClearPersonal=new Button("私聊区清屏"); 
				buttonClearPersonal.setFont(buttonFont);
				
				buttonBack=new Button("离开聊天室");
				buttonBack.setFont(buttonFont);
				
				buttonSend=new Button("发送文件");
				buttonSend.setFont(buttonFont);
				
				add(labelInput);
				add(textInput);
				add(buttonInput);
				add(buttonClearPublic);
				add(buttonClearPersonal);
				add(buttonSend);
				add(buttonBack);
				
				
				buttonInput.addActionListener(this);
				buttonClearPublic.addActionListener(this);
				buttonClearPersonal.addActionListener(this);

⌨️ 快捷键说明

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