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

📄 guiclient.java

📁 提供用户登陆
💻 JAVA
字号:
package MyPackage;
import MyPackage.VECTOR;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.text.*;

public class GuiClient{
	static JFrame jf;
	static JList jList;//名字输入域
    static JComboBox jCob;
	static JTextArea jtaChat;//显示聊天信息
	static JTextArea jtaInput;//输入消息
	static JButton jbSend;//发送消息按钮
	static JButton jbClear;//取消息按钮
	static Socket socket;
    static String UserName1;
	public GuiClient(String s){
		jf=new JFrame("Gui多人聊天室                              ");
	     try{
				socket=new Socket("172.40.83.44",4700);
				BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				new RecieveThread(is).start();
			}catch(Exception e){
				System.out.println("异常1:"+e);
				}
		
	    UserName1=s;//UserName用来存放用户名
		Container container=jf.getContentPane();//得到容器
		JPanel p=new JPanel();//初始化一个面板
		p.setLayout(new FlowLayout(FlowLayout.LEFT));
		new FileRead();
		X.list=X.temp1;
        jCob=new JComboBox(X.temp1);
		p.add(new JLabel("欢迎您进入Gui多人情感聊天室    "));//增加聊天题标签
	    p.add(new JLabel("您的昵称:"+UserName1));
		p.add(new JLabel("    发送给"));
		p.add(jCob); 

		container.add(p,BorderLayout.NORTH);
		jtaChat=new JTextArea();
		jtaChat.setEditable(false);//设置文本输入框不可编辑
		JScrollPane scrollpane1=new JScrollPane(jtaChat);
		scrollpane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		//scrollpane1.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		//scrollpane1.setVerticalScrollBar(new JScrollBar(JScrollBar.VERTICAL,10,10,0,100));
		container.add(scrollpane1,BorderLayout.CENTER);
		Box box=new Box(BoxLayout.X_AXIS);//初始化一个Box
		jtaInput=new JTextArea(5,20);//初始化一个消息输入域
		jbSend=new JButton("发送");
		jbClear=new JButton("关闭");
        
		//jCob.addItemListener(new ListListener());
		jbClear.addActionListener(new Close());//侦听按钮的事件
		jbSend.addActionListener(new SendListener());//侦听事件	 
        JScrollPane scrollpane2=new JScrollPane(jtaInput);
        scrollpane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollpane2.setComponentOrientation(ComponentOrientation.UNKNOWN);  
		box.add(scrollpane2);//增加消息输入域
		box.add(jbClear);
		box.add(jbSend);
		container.add(box,BorderLayout.SOUTH);
		jf.setSize(500,400);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口退出程序
	}
/*	public static void main(String[] args) 
	{
		new GuiClient("shuermao");
		
	}*/
}

class SendListener implements ActionListener{
	public void actionPerformed(ActionEvent e){
		String txt;
		txt=GuiClient.jtaInput.getText();
		String Item=(String)GuiClient.jCob.getSelectedItem();
		if(GuiClient.UserName1.equals(Item)){
			new Message("你不能与自己进行私聊");
			System.out.println("非法操作!");
			return;
		}
		if(txt.length()==0){
			new Message("空消息");
		}
		else{
			GuiClient.jtaInput.replaceRange("",0,txt.length());//用一个空字符来替换TextArea中的所有字符 
			new Client(txt,GuiClient.socket);
			}
	}
}

class Client
{
	public Client(String s,Socket scoket)
	{
		try{
			PrintWriter os=new PrintWriter(GuiClient.socket.getOutputStream());
			String username=GuiClient.UserName1;
			String Item=(String)GuiClient.jCob.getSelectedItem();
			String s1,s2;
			Date date=new Date();
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//将日期转换为年月日时分秒的形式
            System.out.println(dateFormat.format(date));
			s1="\n"+username+">对"+"<"+Item+">说("+dateFormat.format(date)+")\n"+s;
			s2=s1.replace('\n','♀');//用♀来替换字符串中所有的回车符
			os.println(s2);
			os.flush();
		}catch(IOException e){
			System.out.println("异常1:"+e);
		}
	}
}

class RecieveThread extends Thread{
	public BufferedReader is;
	RecieveThread(BufferedReader is){
		this.is=is;
	}
	public void run(){
	    try{
			String s;
			while(true){
				s=is.readLine();
				if(s!=null){
                    System.out.println(s);
					if(s.equals("※♂℉〒á☆"))
						new Message("※♂℉〒á☆");
					else
						GuiClient.jtaChat.append(s+"\n");
					//
				}
			}
		}
		catch(Exception e){System.out.println("异常3:"+e);}
	}
};

class Close implements ActionListener{
	public void actionPerformed(ActionEvent e){
		try{
			GuiClient.socket.close();//关闭socket
		}
		catch(IOException e2){
			System.out.println("异常2:"+e2);
			}
		System.exit(1);
	}
}


class ListListener implements ItemListener{//选择列表项时发生的事件
	public void itemStateChanged(ItemEvent e){
		int i,j;
		System.out.println("长度="+X.temp1.size());
		for(i=0;i<X.temp1.size();i++){
		    System.out.println("我到过此处"+i);
			X.temp1.remove(i);
		}
		new FileRead();
		System.out.println("flag");
		//System.out.println("X.list的长度="+X.list.size()+" X.temp的长度="+X.temp.size());
		if(X.list.size()!=X.temp1.size()){//将新注册的用户加入到用户列表当中
		 //   System.out.println("你到过此处吗?");
			for(i=0;i<X.temp1.size();i++){
				String s1=(String)(X.temp1.get(i));
				boolean flag=true;
				for(j=0;j<X.list.size();j++){
					String s2=(String)(X.list.get(j));
					if(s1.equals(s2)){
						flag=false;
						break;
					}
				}
				if(flag){
					GuiClient.jCob.addItem(X.temp1.get(i));
					X.list.add((String)(X.temp1.get(i)));
				}
			}
		}
		//System.out.println("主人");
	}
};

class A{//user容器中的内容
    public String name;
	public String key;
};

class X
{
	public static Vector temp1=new Vector();
	public static Vector list=new Vector();
};

class FileRead 
{
	public FileRead(){
        int i,j;
		String s,s1,s2;
		try{
		RandomAccessFile raf=new RandomAccessFile("user.txt","rw");
		while((s=raf.readLine())!=null){
            i=s.indexOf(58);//查找:在字符串中的位置
			s1=s.substring(0,i);
			s2=s.substring(i+1,s.length());
			A arr=new A();
			arr.name=s1;

			
			X.temp1.add(arr.name);//在容器中增加一项
		}
		System.out.println(X.temp1.size());
		raf.close();
		}
		catch(Exception e){}
	}
}


class Message{//消息对话框
	public Message(String s){
		int type=JOptionPane.PLAIN_MESSAGE;
		String title="Message Dialog";
		String message="";
		if(s.equals("你不能与自己进行私聊")){
			type=JOptionPane.WARNING_MESSAGE;
            message="警告:你选择了与自己进行私聊";
		}
		else if(s.equals("※♂℉〒á☆")){
			type=JOptionPane.INFORMATION_MESSAGE;
			message="该用户当前不在线\n因此该用户无法收到您刚才发送的消息!";
		}
		else if(s.equals("空消息")){
			type=JOptionPane.WARNING_MESSAGE;
			message="发送的内容不能为空,请重新输入...";
		}
		JOptionPane.showMessageDialog(GuiClient.jf,message,title,type);
	}		
		
};

⌨️ 快捷键说明

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