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

📄 chatappletthree.java

📁 初学java,最近帮同学作的一些实验
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class chatappletthree extends Applet implements Runnable,ActionListener,ItemListener{
	
	public static final int PORT =1234;
    public static String name,xingbie=new String("蘑菇");
	Socket socket;
	int jilu,enter=0;
	DataInputStream in;
	static DataOutputStream out;
    Thread thread;
    String line;
    static Apanel a;
    static Bpanel b;
    static Cpanel c;
    static Mywindow mywindow;
    InetAddress adrr;
    public void init()
    {
    	System.out.println(2);
    	mywindow=new Mywindow();
    	setBackground(new Color(113,163,139));
    	setLayout(new BorderLayout());
    	a=new Apanel();
    	b=new Bpanel();
    	c=new Cpanel();
    	add("North",a);
    	add("Center",b);
    	add("South",c);
    	a.button1.addActionListener(this);
    	a.button2.addActionListener(this);
    	c.button.addActionListener(this);
    	c.button2.addActionListener(this);
    	c.button3.addActionListener(this);
    	a.box1.addItemListener(this);
    	a.box2.addItemListener(this);
    	a.box3.addItemListener(this);
    	b.b2.list.addActionListener(this);   	
    	add("East",new Label());
    	add("West",new Label());
    	jilu=0;
    	this.setForeground(Color.white);
    	b.chat_txt.setBackground(Color.white);
    	b.chat_txt.setFont(new Font("TimeRoman",Font.PLAIN,12));
    	System.out.println(3);

    	try {
			  adrr=InetAddress.getByName(null);
    		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(11);
		}
    	System.out.println(4);
    	//this.getCodeBase().getHost();
    	System.out.println(6);
		try{
			//socket =new Socket(this.getCodeBase().getHost(),PORT);
		    socket=new Socket(adrr,1234);
			in=new DataInputStream(socket.getInputStream());
			out=new DataOutputStream(socket.getOutputStream());
		}catch(IOException e){
			this.showStatus(e.toString());
			say("欢迎来这里");
			System.exit(1);
		}
		say("    欢迎来到幽蓝聊天室     ");
		System.out.println(7);
		if(thread==null)
		{
			thread=new Thread(this);
			thread.setPriority(Thread.MIN_PRIORITY);
			thread.start();
		}
    }
    
   /* public void stop()
    {
    	try{
    		socket.close();
    	}catch(IOException e){}
    }*/
    public void desdroy()
    {
    	try{
    		socket.close();
    	}catch(IOException e){
    		this.showStatus(e.toString());
    	}
    	if((thread!=null)&&thread.isAlive())
    	{
    	  thread.yield();
    	  thread=null;
    	}
    }
   public void run()
   {
	   String line;
	   //System.out.println(5);
	   try{
		   while(true)
		   {
			   line=in.readUTF();
			   if(line.startsWith("people"))
			   {
				   String listString=line;
				   if(line.endsWith("*"))
				   {
					   listString=line.substring(0,(line.length()-1));
					}
					b.b2.list.add(listString.substring(6));
					if(!line.endsWith("*"))
					{
						   b.chat_txt.append(line.substring(6)+"爬上了幽蓝网--"+"\n");					   
					}
			   }
			   if(line.startsWith("quit"));
			   {
				   String str=line.substring(10);
				   try{
					   for(int i=0,k=0;i<=120;i++)
					   {
						   String s=b.b2.list.getItem(i);
						   if(s.equals(str))
						   {
							   k=i;
							   b.b2.list.remove(k);
							   b.chat_txt.append(line.substring(10)+"离开了幽蓝网"+"\n");
						   }
					   }
				   }catch(ArrayIndexOutOfBoundsException e){}
			   }
			   if(line.startsWith("MSG"))
			   {
				   //System.out.println("appletm");
				   b.chat_txt.append(line.substring(3)+"\n");
			   }
			   if(line.startsWith("悄悄地对"))
			   {
				   b.chat_txt.append(line+"\n");
			   }
	   }
	   }catch(IOException e){
		   say("再见!欢迎再来幽蓝聊天室!如想重新进入本聊天室,单击浏览器的刷新选项!");
	   }catch(NullPointerException e){}
   }
   public void actionPerformed(ActionEvent e)
   {
	   if(e.getSource()==a.button1)
	   {
		   if(a.name_txt.getText()==null)
		   {
			   a.name_txt.setText("昵称不能为空");
			   return;
		   }
		   name=new String(a.name_txt.getText());
		 try{
			   for(int i=0;i<=120;i++)
			   {
				   if(((a.name_txt.getText()!=null)&&(a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.list.getItem(i)))||(a.name_txt.getText().equals("该名已被使用")))
				   {
					   //System.out.println(b.b2.list.getItem(i));
					   //System.out.println(a.name_txt.getText());
					   jilu=1;name=null;}
			   }
		   }catch(ArrayIndexOutOfBoundsException e3){}
		   if(jilu==0)
		   {
			   try{
				   out.writeUTF("people"+a.name_txt.getText()+"["+xingbie+"]");
				   enter=1;
			   }catch(IOException e1){}
		   }else if(jilu==1)
		   {
			   a.name_txt.setText("该名已被使用");
			   jilu=0;
		   }
	   }
	   if(e.getSource()==a.button2)
	   {
		   try{
			   out.writeUTF("quit");
			   enter=0;
		   }catch(IOException e1){}
		   //b.b2.removeAll();
	   }
	   if(e.getSource()==c.button&&enter==1)
		   
	   {   //System.out.println("发");
		   if(name!=null)
		   {
			  try{
				  out.writeUTF("MSG"+name+"["+xingbie+"]"+"说-->"+": "+c.msg_txt.getText());
				  c.msg_txt.setText(null);
			  } catch(IOException e1){}
		   }
	   }
	   if(e.getSource()==b.b2.list&&enter==1)
	   {
		   mywindow.setVisible(true);
		   mywindow.text1.setText(((List)e.getSource()).getSelectedItem());
	   }
	   if(e.getSource()==c.button2)
	   {
		   b.chat_txt.setText(null);
	   }
	   if(e.getSource()==c.button3)
	   {
		   try{
			   b.b2.list.removeAll();
			   out.writeUTF("newlist");
		   }catch(IOException e1){}
	   }
   }
   public void itemStateChanged(ItemEvent e1)
   {
	   if(e1.getItemSelectable()==a.box1)
	   {xingbie=new String("男");}
	   if(e1.getItemSelectable()==a.box2)
	   {xingbie=new String("女");} 
	   if(e1.getItemSelectable()==a.box3)
	   {xingbie=new String("蘑菇");}
   }
   public void say(String msg)
   {
	   b.chat_txt.append("******          "+msg+"         ******\n");
   }
}

⌨️ 快捷键说明

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