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

📄 login.java

📁 这是一个仿QQ的聊天程序
💻 JAVA
字号:
package com.cloudcloud.soft;

import java.awt.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.net.*;
import java.io.*;
public class Login extends JFrame implements ActionListener, KeyListener
{

	String path = "images/";
	JLabel lLogo;
	JLabel lQQNum;
	JLabel lQQPwd;

	JLabel lBGUI;

	JLabel lBGU;
	JTextField tQQNum;
	JPasswordField tQQPwd;
	JButton bNewNum;
	JButton bLogin;
	JButton bCancel;
	JButton bServer;
	JCheckBox cAutoL;
	JCheckBox cHide;

	JPanel pUser;

	JPanel pUserInfo;

	SetServerPane ssp;
	
	Socket s = null;
	Login()
	{
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("CloudQQ用户登陆");
		this.setBounds(300,200,331,240);
		this.setResizable(false);
		
		this.setLayout(null);

		lLogo = new JLabel();
		lLogo.setIcon(new ImageIcon(getImageSource(path + "logo1.gif")));
		lLogo.setBounds(0,0,324,47);

		lBGUI = new JLabel();
		lBGUI.setIcon(new ImageIcon(getImageSource(path + "bground.gif")));
		lBGUI.setBounds(0,0,315,117);

		lBGU = new JLabel();
		lBGU.setIcon(new ImageIcon(getImageSource(path + "bbground.gif")));
		lBGU.setBounds(0,0,331,169);

		lQQNum = new JLabel("QQ号码");
		lQQNum.setFont(new Font("宋体", Font.PLAIN, 12));
		lQQNum.setBounds(20,15,50,20);

		lQQPwd = new JLabel("QQ密码");
		lQQPwd.setFont(new Font("宋体", Font.PLAIN, 12));
		lQQPwd.setBounds(20,50,50,20);

		tQQNum = new JTextField();
		tQQNum.setBounds(70,15,130,20);
		tQQNum.setFont(new Font("Tamoha", Font.PLAIN, 12));
		tQQNum.addKeyListener(this);

		tQQPwd = new JPasswordField();
		tQQPwd.setBounds(70,50,130,20);
		tQQPwd.addKeyListener(this);

		bNewNum = new JButton("申请号码");
		bNewNum.setFont(new Font("宋体", Font.PLAIN, 12));
		bNewNum.setBounds(210,15,82,20);
		bNewNum.addActionListener(this);

		bServer = new JButton("设置服务器↓");
		bServer.setFont(new Font("宋体", Font.PLAIN, 12));
		bServer.setBounds(6,135,110,20);
		bServer.addActionListener(this);

		bCancel = new JButton("取消");
		bCancel.setFont(new Font("宋体", Font.PLAIN, 12));
		bCancel.setBounds(230,135,80,20);
		bCancel.addActionListener(this);

		bLogin = new JButton("登录");
		bLogin.setFont(new Font("宋体", Font.PLAIN, 12));
		bLogin.setBounds(140,135,80,20);
		bLogin.addActionListener(this);

		cAutoL = new JCheckBox("自动登录");
		cAutoL.setFont(new Font("宋体", Font.PLAIN, 12));
		cAutoL.setBackground(new Color(242,249,255));
		cAutoL.setBounds(65,85,75,13);

		cHide = new JCheckBox("隐身登录");
		cHide.setFont(new Font("宋体", Font.PLAIN, 12));
		cHide.setBackground(new Color(242,249,255));
		cHide.setBounds(140,85,75,13);

		pUserInfo = new JPanel();
		pUserInfo.setBackground(Color.WHITE);
		pUserInfo.setBounds(5,10,315,117);
		pUserInfo.setLayout(null);
		pUserInfo.add(lQQNum);
		pUserInfo.add(lQQPwd);
		pUserInfo.add(tQQNum);
		pUserInfo.add(tQQPwd);
		pUserInfo.add(cAutoL);
		pUserInfo.add(cHide);
		pUserInfo.add(bNewNum);
		pUserInfo.add(lBGUI);

		pUser = new JPanel();
		pUser.setBackground(Color.WHITE);
		pUser.setLayout(null);
		pUser.setBounds(0,47,331,168);
		pUser.add(pUserInfo);
		pUser.add(bServer);
		pUser.add(bLogin);
		pUser.add(bCancel);
		pUser.add(lBGU);
		
		this.add(lLogo);
		this.add(pUser);
	}
	public static void main(String[] args) 
	{
		Login l = new Login();
		l.setVisible(true);
	}
	private BufferedImage getImageSource(String imagePath)
	{
		BufferedImage image = null;
		InputStream in = Class.class.getClass().getResourceAsStream("/" + imagePath);
		try 
		{
			image = ImageIO.read(in);
			return image;
		}
		catch (IOException e) 
		{
			e.printStackTrace();
			return image;
		}
	}
	public void setSocket(Socket s)
	{
		this.s = s;
	}
	public void actionPerformed(ActionEvent e) 
	{

		if(e.getSource() == bCancel)
		{
			System.exit(0);
		}

		if(e.getSource() == bLogin)
		{
			login();
		}
		

		if(e.getSource() == bNewNum)
		{
			this.setVisible(false);
			Register r = new Register(this);
			r.setVisible(true);
		}

		if(e.getSource() == bServer)
		{
			if(ssp == null || !ssp.isVisible())
			{
				this.setSize(331,300);
				ssp = new SetServerPane();
				this.add(ssp);
				this.setVisible(true);
				bServer.setText("设置服务器↑");
			}
			else
			{
				ssp.setVisible(false);
				bServer.setText("设置服务器↓");
				this.setSize(331,240);
				this.repaint();
				this.setVisible(true);
			}
		}
	}
	public void login()
	{
		String QQNum = tQQNum.getText();
		String QQPwd = tQQPwd.getText();
		if(QQNum.equals("") || QQPwd.equals(""))
		{
			asdwqer mb = new asdwqer();
			mb.setVisible(true);
			if(QQNum.equals(""))
			{
				mb.setLms("QQ号码不能为空");
			}
			else if(QQPwd.equals(""))
			{
				mb.setLms("QQ密码不能为空");
			}
		}
		else
		{
			try
			{
				if(s == null)
				{
					s = new Socket(sasdasd.SERVERIP, sasdasd.SERVERPORT);
				}
				Zdasdasd ms = new Zdasdasd(s);
				ms.println("LOGIN");
				ms.println(tQQNum.getText() + "==CloudQQ==" + tQQPwd.getText());
				
				String yorn = ms.readLine();
				if(yorn.equals("CloudQQ_OK"))
				{
					String userData = ms.readLine();
					cawesda m = new cawesda();
					String sep = "==QQ==";
					m.setID(Integer.parseInt(userData.split(sep)[0]));
					m.setPwd(userData.split(sep)[1]);
					m.setName(userData.split(sep)[2]);
					m.setHeadNum(Integer.parseInt(userData.split(sep)[3]));
					m.setSex(userData.split(sep)[4]);
					m.setAge(Integer.parseInt(userData.split(sep)[5]));
					m.setEmail(userData.split(sep)[6]);
					m.setShow(userData.split(sep)[7]);
					
					
					this.setVisible(false);
					cxzc f = new cxzc(m);
					f.setSocket(s);
					f.setVisible(true);
				}
				else if(yorn.equals("CloudQQ_ERR"))
				{
					s.close();
					s = null;
					asdwqer mb = new asdwqer();
					mb.setVisible(true);
					mb.setLms("用户名和密码不正确!");
					tQQNum.setText("");
					tQQPwd.setText("");
				}
				else if(yorn.equals("CloudQQ_DATABASE_ERR"))
				{
					s.close();
					s = null;
					asdwqer mb = new asdwqer();
					mb.setVisible(true);
					mb.setLms("服务器忙,请稍候再试!");
					tQQNum.setText("");
					tQQPwd.setText("");
				}
			}
			catch(Exception e0)
			{
				asdwqer mb = new asdwqer();
				mb.setVisible(true);
				mb.setLms("无法连接到服务器,请稍候再试!");
				tQQNum.setText("");
				tQQPwd.setText("");
			}
		}
		
	}
	public void keyTyped(KeyEvent e) 
	{
	}
	public void keyPressed(KeyEvent e) 
	{
		if(e.getKeyCode() == 10)
		{
			login();
		}		
	}
	public void keyReleased(KeyEvent e) 
	{
	}
}

class SetServerPane extends JPanel implements ActionListener
{
	JTextField tIP;
	JTextField tPort;
	JButton b;
	SetServerPane()
	{
		this.setBackground(new Color(231,243,255));
		this.setBounds(0,215,331,60);
		this.setLayout(null);
		JLabel l1 = new JLabel("请设置服务器IP地址和端口号:");
		l1.setFont(new Font("宋体", Font.PLAIN, 12));
		l1.setBounds(5,0,200,20);
		
		tIP = new JTextField("127.0.0.1");
		tIP.setFont(new Font("Tahoma", Font.PLAIN, 12));
		tIP.setBounds(5,25,102,20);
		
		JLabel l2 = new JLabel(":");
		l2.setFont(new Font("宋体", Font.BOLD, 12));
		l2.setBounds(110,22,10,20);
		
		tPort = new JTextField("8000");
		tPort.setFont(new Font("Tahoma", Font.PLAIN, 12));
		tPort.setBounds(120,25,50,20);
		
		b = new JButton("确定");
		b.setFont(new Font("宋体", Font.PLAIN,12));
		b.setBounds(220,20,60,25);
		b.addActionListener(this);
		
		this.add(l1);
		this.add(l2);
		this.add(tIP);
		this.add(tPort);
		this.add(b);
		this.setVisible(true);
	}
	private boolean getIsIPCorrect(String ipstr)
	{
		int[] ip = new int[4];
		try
		{
			for(int i = 0; i <= 3; i++)
			{
				ip[i] = Integer.parseInt(ipstr.split("\\.")[i]);
				if(ip[i] < 0 || ip[i] > 255)
				{
					return false;
				}
			}
			return true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	private boolean getIsPortCorrect(String portstr)
	{
		try
		{
			Integer.parseInt(portstr);
			return true;
		}
		catch(Exception e)
		{
			return false;
		}
	}
	private void dealIPAndPort(String ip, String port)
	{
		boolean isIPCorrect = getIsIPCorrect(ip);
		boolean isPortCorrect = getIsPortCorrect(port);
		if(isIPCorrect && isPortCorrect)
		{
			sasdasd.SERVERIP = ip;
			sasdasd.SERVERPORT = Integer.parseInt(port);
			asdwqer mb = new asdwqer();
			mb.setSize(300,100);
			mb.setTitle("消息");
			mb.setVisible(true);
			mb.setLms("新的服务器" + ip + ":" + port + "设置成功。");
		}
		else if(!isIPCorrect && isPortCorrect)
		{
			asdwqer mb = new asdwqer();
			mb.setVisible(true);
			mb.setLms("IP不合法");
		}
		else if(isIPCorrect && !isPortCorrect)
		{
			asdwqer mb = new asdwqer();
			mb.setVisible(true);
			mb.setLms("端口号不合法");
		}
		else if(!isIPCorrect && !isPortCorrect)
		{
			asdwqer mb = new asdwqer();
			mb.setVisible(true);
			mb.setLms("IP和端口号不合法");
		}
	}
	public void actionPerformed(ActionEvent e) 
	{
		String ip = tIP.getText();
		String port = tPort.getText();
		dealIPAndPort(ip , port);		
	}
}

⌨️ 快捷键说明

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