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

📄 loginclient.java

📁 Java scoket 网络聊天室(加入QQ表情)
💻 JAVA
字号:
/**
 *    Socket 网络聊天室
 *     客户端登陆界面
 *
 *@author 董利伟
 *@version 1.0 2006/07/26
 */

package dongliwei.chat.client;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.regex.*;

public class LoginClient extends JFrame
{
	JPanel jp1;//窗体上的默认容器
	JFrame jf;//窗体
	
	int frame_X;
	int frame_Y;
	
	JLabel title;//标题
	
	JLabel userinfo;//用户信息
	JTextField usertxt;//用户信息
	
	JButton btConfigure;//配置IP以及端口号
	JButton btLogin;//进入聊天室
	JButton btPause;//退出
	
	JProgressBar p;//进度条
	JLabel jinfo;//状态信息
	
	JButton BtPhoto;
	
	PhotoDialog fd;
	
	public LoginClient()
	{
		jf = this;
		jp1 = (JPanel)this.getContentPane();
		jf.setLayout(null);
		
		String ip = ClientInfo.serverIP;
		int part = ClientInfo.serverPart;
		
		title = new JLabel("欢迎登陆聊天室");
		title.setBounds(100,33,450,50);
		title.setFont(new Font("宋体",Font.BOLD,35));
		title.setForeground(Color.red);
		jp1.add(title);
		
		userinfo = new JLabel("用户昵称  ");
		userinfo.setBounds(125,100,100,25);
		jp1.add(userinfo);
		
		int a = ClientInfo.imageURL.indexOf("file:");
		int b = ClientInfo.imageURL.lastIndexOf("width");
		
		String path = ClientInfo.imageURL.substring(a + 6,b);
		System.out.println (" ClientInfo.imageURL.substring(a + 6,b)++++ the path is " + path);
		
		BtPhoto = new JButton(new ImageIcon("image/0a.gif"));
		BtPhoto.setBounds(350,80,70,50);
		BtPhoto.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					new PhotoDialog();					
				}
			});
		jp1.add(BtPhoto);
		
		
		usertxt = new JTextField();
		usertxt.setColumns(20);
		usertxt.setBounds(225,100,100,25);
		jp1.add(usertxt);
		
		btConfigure = new JButton("设置");
		btConfigure.setBounds(60,140,100,25);
		btConfigure.addActionListener(new BtEvent());
		jp1.add(btConfigure);
		
		btLogin = new JButton("进入");
		btLogin.addActionListener(new BtEvent());
		btLogin.setBounds(180,140,100,25);
		jp1.add(btLogin);

		btPause = new JButton("退出");
		btPause.setBounds(300,140,100,25);
		btPause.addActionListener(new BtEvent());
		jp1.add(btPause);
		
		p = new JProgressBar();
		p.setMinimum(0);
		p.setMaximum(100);
		p.setStringPainted(true);
		p.setBounds(80,190,300,13);
		jp1.add(p);
		
		jinfo = new JLabel("尚未登陆......");
		jinfo.setBounds(80,205,200,25);
		jp1.add(jinfo);
		
		this.setSize(480,270);
		this.setTitle("欢迎登陆聊天室");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Toolkit tk = this.getToolkit();
		Dimension dms = tk.getScreenSize();
		this.frame_X = (int)(dms.getWidth()) / 2 - this.getWidth() / 2;
		this.frame_Y = (int)(dms.getHeight()) / 2 - this.getHeight() / 2;
		this.setLocation(this.frame_X,this.frame_Y);
		this.setVisible(true);
	}
	
	public static void main(String args[])
    {
        new LoginClient();
    }
    
    class BtEvent implements ActionListener
    {
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == btLogin)
    		{
    			String str = usertxt.getText().trim();
    			if(str.equals(""))
    			{
    				JOptionPane.showMessageDialog(null, "请您输入昵称", "信息提示", JOptionPane.ERROR_MESSAGE);
    			}
    			else
    			{
    				ClientInfo.userName = str;
	    			jinfo.setText("正在登陆.....");
	    			for (int i = 0; i <= 100; i++)
				    {
				    	try 
					    {
					    	Thread.sleep(10);
					    	p.setValue(i);
					        
					    }
					    catch (Exception ex) 
					    {
					    	System.out.println(ex);
					    }
					    jinfo.setText("成功登陆"); 
    				}

					new ChatClient();
					jf.dispose();
				    
			    }//end if
    		}    		
    		else if(e.getSource() == btConfigure)
    		{
    			new ConfigureDialog();
    		}
    		else if(e.getSource() == btPause)
    		{
    			System.exit(0);
    		}
    	}
    }
    
    class ConfigureDialog
    {
    	JDialog jdg;
    	
    	JLabel locaIP;
    	JTextField txtLocaIP;
    	
    	JLabel locaPart;
    	JTextField txtLocaPart;
    	
    	JButton btOk;
    	JButton btClose;
    	
    	String ip;
    	int part;
    	
    	public ConfigureDialog()
    	{
    		ip = ClientInfo.serverIP;
    		part = ClientInfo.serverPart;
    		
    		jdg = new JDialog(jf);
    		jdg.setLayout(null);
    		
    		locaIP = new JLabel("服务器 IP ");
    		locaIP.setBounds(100,30,100,25);
    		jdg.add(locaIP);
    		
    		txtLocaIP = new JTextField(ip);
    		txtLocaIP.setBounds(200,30,100,25);
    		txtLocaIP.setColumns(20);
    		jdg.add(txtLocaIP);
    		
    		locaPart = new JLabel("服务器端口号");
    		locaPart.setBounds(100,70,100,25);
    		jdg.add(locaPart);
    		
    		txtLocaPart = new JTextField(String.valueOf(part));
    		txtLocaPart.setBounds(200,70,100,25);
    		txtLocaPart.setColumns(20);
    		jdg.add(txtLocaPart);
    		
    		btOk = new JButton("确定");
    		btOk.setBounds(100,110,100,25);
    		btOk.addActionListener(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					String strip = txtLocaIP.getText().trim();
    					String strpart = txtLocaPart.getText().trim();
    					int int_part = 0;
    					try 
					    {
					     	   int_part = Integer.parseInt(strpart);
					    }
					    catch (Exception ex) 
					    {
					    	System.out.println(ex);
					    }
    					if(strip.equals(""))
    					{
    						JOptionPane.showMessageDialog(null, "您的IP不能为空", "错误提示", JOptionPane.ERROR_MESSAGE);
    						txtLocaIP.setText(ip);
    						txtLocaPart.setText(String.valueOf(part));
    					}
    					else if(strpart.equals(""))
    					{
    						JOptionPane.showMessageDialog(null, "您的端口号不能为空", "错误提示", JOptionPane.ERROR_MESSAGE);
    						txtLocaIP.setText(ip);
    						txtLocaPart.setText(String.valueOf(part));
    					}
    					else if(int_part < 1024 || int_part > 65535)
    					{
    						JOptionPane.showMessageDialog(null, "端口号应该设置在 1024--65535 之间", "错误提示", JOptionPane.ERROR_MESSAGE);
    						txtLocaIP.setText(ip);
    						txtLocaPart.setText(String.valueOf(part));
    					}
    					else
    					{
    						Pattern p = null;
							Matcher m = null;
							
							String dstr = "(\\p{Digit}{0,3})\\.(\\p{Digit}{0,3})\\.(\\p{Digit}{0,3})\\.(\\p{Digit}{0,3})";

							p = Pattern.compile(dstr);
							m = p.matcher(strip);
							boolean bb = m.matches();
							System.out.println (bb);
							if(bb)
							{
								String s = m.group(0);
								String s1 = m.group(1);
								String s2 = m.group(2);
								String s3 = m.group(3);
								String s4 = m.group(4);
								int a1 = Integer.parseInt(s1);
								int a2 = Integer.parseInt(s2);
								int a3 = Integer.parseInt(s3);
								int a4 = Integer.parseInt(s4);
								if(a1 < 0 || a1 > 255 || a1 < 0 || a1 > 255 || a1 < 0 || a1 > 255 || a1 < 0 || a1 > 255)
								{
									JOptionPane.showMessageDialog(null, "IP 地址格式错误,应为(如:192.168.0.1)", "错误提示", JOptionPane.ERROR_MESSAGE);
									txtLocaIP.setText(ip);
									return;
								}
								else
								{
									ClientInfo.serverIP = txtLocaIP.getText().trim();
								}																	
							}
							else
							{
								JOptionPane.showMessageDialog(null, "IP 地址格式错误,应为(如:192.168.0.1)", "错误提示", JOptionPane.ERROR_MESSAGE);
								txtLocaIP.setText(ip);
								return;
							}						
							txtLocaPart.setText(String.valueOf(part));
    						JOptionPane.showMessageDialog(null, "您将要登录的服务器IP " + ClientInfo.serverIP + "端口号 " + ClientInfo.serverPart, "信息提示", JOptionPane.INFORMATION_MESSAGE);
    						jdg.dispose();
    					}
    				}
    			});
    		jdg.add(btOk);
    		
    		btClose = new JButton("取消");
    		btClose.setBounds(230,110,100,25);
    		btClose.addActionListener(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					jdg.dispose();
    				}
    			});
    		
    		jdg.add(btClose);
    		jdg.setTitle("设置服务器参数");
    		jdg.setSize(450,220);
    		jdg.setLocation(frame_X,frame_Y);
    		jdg.setVisible(true);
    	}    	
    }
 
    class PhotoDialog
	{
		JDialog jdg;
		JPanel fp;
		JButton[] bt;
		JScrollPane sp;
		
		public PhotoDialog()
		{
			jdg = new JDialog(jf);
			fp = new JPanel();
			fp.setLayout(new GridLayout(22,5,2,2));
			sp = new JScrollPane(fp);
			jdg.add(sp);
			
			bt = new JButton[104];
			for (int i = 0; i < 104; i++)
			{
				bt[i] = new JButton(new ImageIcon("./image/" + i + "a.gif"));
				fp.add(bt[i]);
				bt[i].addActionListener(new BtFaceEvent());
			}
			jdg.setSize(400,250);
			jdg.setTitle("请选择人物");
			jdg.setLocation(frame_X,frame_Y);
			jdg.setAlwaysOnTop(true);
			jdg.setFocusCycleRoot(true);
			jdg.setResizable(false);
			jdg.addWindowListener(
				new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						fd = null;
						jdg.dispose();
					}					
				});
			jdg.setVisible(true);
		}
		
		class BtFaceEvent implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				for (int i = 0; i < 104; i++)
				{
					if(e.getSource() == bt[i])
					{						
						ClientInfo.imageURL = "<img src= file:./image/" + i + "a.gif width=20 height=20>";						
						BtPhoto.setIcon(new ImageIcon("./image/" + i + "a.gif"));
						fd = null;					
						jdg.dispose();						
					}
				}
			}
		}		
	}    
}

⌨️ 快捷键说明

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