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

📄 clintframe.java

📁 初学java,最近帮同学作的一些实验
💻 JAVA
字号:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.*;
import java.util.StringTokenizer;

public class ClintFrame extends Frame {
	private static final long serialVersionUID = 1L;
	private TextField name,input,info;
	private TextArea output;
	Boolean open=false;                //当用户正确进入且油有出来的时候OPEN为TRUE,
	Boolean isprivate=false;           //判断是否是私聊信息
	int goodname=0;                     //用来控制是否重名,当他为1时无重名,为2时重名了,为0是不确定状态
	InetAddress serverip;
    private final int serverport=8080;
	Button b; //显示名字判断信息
    Label check;
	List groupmember;           //用来显示组内成员的列表
	DatagramSocket dsocket;
	final String newline=System.getProperty("line.separator"); //打印换行

	public ClintFrame(){
		super("毛小丽的聊天室");
		try {
			dsocket=new DatagramSocket();
			serverip=InetAddress.getByName("127.0.0.1");
		} catch (SocketException e) {
				System.out.println("怎么回事啊?");
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		setSize(500,700);
		setVisible(true);
		setLayout(new BorderLayout(30,20));
		addSouthObject();
	    addCenterObject();
		addNorthObject();
		addEastObject();
		addWindowListener(new DealwithClose());
		this.setBackground(Color.blue);
		this.validate();		
	}
	
	private void addEastObject(){             //添加东面的PANEL
		Label l=new Label("组员(双击昵称可发送悄悄话)");
		l.setBackground(new Color(236,233,216));
		groupmember=new List(14,false);
		groupmember.setBackground(new Color(236,233,216));
		Panel p=new Panel();
		p.setLayout(new BorderLayout());
		p.add("North",l);
		p.add("Center",groupmember);;
		this.add("East",p);	
		groupmember.addActionListener(new DealwithPrivateInfo());
	}
	
	private void addSouthObject(){
		Panel p=new Panel();
		Label l=new Label("输入昵称:");
		l.setBackground(new Color(236,233,216));
		name=new TextField(10);
		name.addActionListener(new DealwithJoin());
		check=new Label();
		check.setBackground(new Color(236,233,216));
		b=new Button("加入聊天室");
		p.setLayout(new GridLayout(1,1,3,4));
		p.add(l);
		p.add(name);
		p.add(b);
		p.add(check);
		b.addActionListener(new DealwithJoin());
		this.add("South",p);
	}
	
	private void addCenterObject(){
		info=new TextField();
		info.setEditable(false);
		input=new TextField(100);
		input.setEditable(false);
		Panel p=new Panel();
		p.setLayout(new BorderLayout());
		p.add("North",info);
		p.add("Center",input);
		this.add("Center",p);
		input.addActionListener(new DealwithInput());
	}
	
	private void addNorthObject(){
		output=new TextArea(25,30);
		output.setEditable(false);
		this.add("North",output);
	}
	
	public class DealwithJoin implements ActionListener{  //新用户登陆时触发,并检查用户名合法性
		public void actionPerformed (ActionEvent e){
			 try {
				 if(getOpen()==false){               //只有当OPEN为FALSE时才执行触发过程
					if(!isGoodName())
					{
						return;
					}
					setOpen(true);
					String s="1#"+name.getText();
					byte buf1[]=new byte[512];
					buf1=s.getBytes();
				    DatagramPacket dp=new DatagramPacket(buf1,buf1.length,serverip,serverport);
				    dsocket.send(dp);
				    while(goodname==0);
				    if(goodname==1){
						s="3#"+name.getText()+"加入该聊天室了";
						byte buf2[]=s.getBytes();
						DatagramPacket dp1=new DatagramPacket(buf2,buf2.length,serverip,serverport);
						dsocket.send(dp1);
						name.setEditable(false);
						input.setEditable(true);
				    }
				    else
				    {
				    	goodname=0;
				    	setOpen(false);
				    }
				 }
			} catch (SocketException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}         
		}
	}
	
	public class DealwithInput implements ActionListener{            //客户端送出信息!

		public void actionPerformed(ActionEvent arg0) {
		     if(open==true){
				try {
					info.setText("群发中.......");
					if(isprivate==false)
					{
						inputPublic();
					}
					else
					{
						inputPrivate();
						isprivate=false;
					}
				} catch (SocketException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		    	
		     }		
		}
		
		private void inputPrivate() throws IOException{     //送出悄悄话
			String s="2#"+name.getText()+"#"+groupmember.getSelectedItem()+"#"+input.getText();
			byte array[]=s.getBytes();
			output.append("你悄悄对"+groupmember.getSelectedItem()+"说:  "+input.getText());
			output.append(newline);
			DatagramPacket dpacket=new DatagramPacket(array,array.length,serverip,serverport);
			dsocket.send(dpacket);
			input.setText(null);
		}
		
		private void inputPublic() throws IOException{  //群发
			String s="3#"+name.getText()+"说:  "+input.getText();
    	    byte array[]=new byte[s.length()+1];
    	    array=s.getBytes();
    	    DatagramPacket dpacket=new DatagramPacket(array,array.length,serverip,serverport);
    	    dsocket.send(dpacket);	 
    	    input.setText(null);
		}
		
	}
	
	public class DealwithPrivateInfo implements ActionListener{   //当用户选择了列表框时触发他,将ISPRIVATE标志置为真
		public void actionPerformed(ActionEvent argo){
			if(groupmember.getSelectedItem()!=null){
				isprivate=true;
				info.setText("发消息给"+groupmember.getSelectedItem()+"中......");
			}
		}
	}
	
	public class DealwithClose implements WindowListener{

		public void windowActivated(WindowEvent arg0) {}

		public void windowClosed(WindowEvent arg0) {}
		
		public void windowClosing(WindowEvent arg0) {
			if(open==true){
				open=false;
				try {
						String s="4#"+name.getText();
						byte buf[]=s.getBytes();
						DatagramPacket dpacket=new DatagramPacket(buf,buf.length,serverip,serverport);
						dsocket.send(dpacket);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			System.exit(0);
			dsocket.close();
			
		}

		public void windowDeactivated(WindowEvent arg0) {}

		public void windowDeiconified(WindowEvent arg0) {}
		
		public void windowIconified(WindowEvent arg0) {}

		public void windowOpened(WindowEvent arg0) {}
		
	}
	
		public void dealwithOutput(){
				byte array[]=new byte[1000];
				DatagramPacket dpacket=new DatagramPacket(array,array.length);
				try {
					dsocket.receive(dpacket);
					String record[]=dealwithPacket(dpacket);
					if(record[0].equals("0"))
					{
						if(record[1].equals("1"))
							goodname=1;
						else{
							check.setText("用户名已有存在!");
							goodname=2;
							return;
						}
					}
					if(record[0].equals("2")||record[0].equals("3"))
				         {output.append(record[1]); output.append(newline);}
					else if(record[0].equals("1"))
						 updateGroupInfo(record[1]);
					else if(record[0].equals("4"))
						deleteGroupInfo(record[1]);
				} catch (IOException e) {
					System.out.println("you are wrong");
					e.printStackTrace();
				}
		}
		

		private void updateGroupInfo(String s){
			if(!ismember(s))
			    groupmember.add(s);
		}
		
		private void deleteGroupInfo(String s){
			if(ismember(s))
			    groupmember.remove(s);
		}

	public Boolean getOpen() {
		return open;
	}

	public void setOpen(Boolean open) {
		this.open = open;
	}
	
	private String[] dealwithPacket(DatagramPacket packet){
		String s1=new String(packet.getData(),0,packet.getLength());
		StringTokenizer s=new StringTokenizer(s1,"#");
		String[] record=new String[6];
		int i=0;
		for(;s.hasMoreTokens();i++){
			record[i]=s.nextToken();
		}
		if(record[0].equals("3")&&i>2)
			for(int k=2;k<i;k++)
				record[1]=record[1].concat("#"+record[k]);
		if(record[0].equals("2")&&i>3)
			for(int k=3;k<i;k++)
				record[2]=record[2].concat("#"+record[k]);
		return record;
	}
	
	private boolean isGoodName(){
		String temp=name.getText();
		if(temp.contains("#")==true)
		{
			check.setText("名字不能含有非法字符");
			return false;
	    }
		if(name.getText().equals(""))
		{
			check.setText("用户名不能为空");
			return false;
		}
			
		return true;
	}
	
	private boolean ismember(String s ){
		String temp[]=groupmember.getItems();
		for(int i=0;i<temp.length;i++)
		{
			if(temp[i].equals(s))
				return true;
		}
		return false;
	}
}

⌨️ 快捷键说明

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