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

📄 login.java

📁 实现TCP/UDP的socket编程
💻 JAVA
字号:
package Form;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.FocusManager;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.Border;

import DataLayer.SqlHelper;
import business.MD5;

public class Login extends JFrame {

	private JPasswordField passwordField;
	private JComboBox cboxuserName;
	private JButton button;
	private static int errorCount=0;
	SqlHelper sh ;
	JPanel panel;
	public static void main(String args[]) {
		try {
			Login frame = new Login();
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Create the frame
	 */
	/**
	 * 构造函数
	 * 用于初始化窗体控件
	 */
	public void setup()
	{
	    panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(5, 45, 310, 130);
		getContentPane().add(panel);
		panel.setBackground(new Color(241,250,255));
		Border normalBorder = BorderFactory.createEtchedBorder(0);   
		Border highBorder = BorderFactory.createBevelBorder(0);   
		panel.setBorder(normalBorder ); 
		 final JLabel lbluserName = new JLabel("QQ帐户");
		 lbluserName.setBounds(30, 40, 50, 15);
		 final JLabel lblpassword = new JLabel("QQ密码");
		 lblpassword.setBounds(30, 90, 50, 15);
		 final JLabel lblregister = new JLabel("申请帐户");
		 
		 //申请帐户的鼠标事件
		 lblregister.addMouseListener(new MouseAdapter() {
		 	public void mouseEntered(MouseEvent e) {
		 		
		 		lblregister.setForeground(Color.RED);
		 	}
		 	public void mouseExited(MouseEvent e) {
		 		lblregister.setForeground(Color.BLUE);
		 	}
		 	public void mouseClicked(MouseEvent e) {
		 		System.out.println("register");
		 	}
		 });
		 
		 lblregister.setForeground(new Color(0, 0, 255));
		 lblregister.setBounds(230, 40, 70, 15);
		 final JLabel lblrevert = new JLabel("忘记密码?");
		 lblrevert.setForeground(new Color(0, 0, 255));
		 lblrevert.setBounds(230, 90, 70, 15);
		 panel.add(lbluserName);
		 panel.add(lblpassword);
		 panel.add(lblregister);
		 panel.add(lblrevert);
		 
		cboxuserName = new JComboBox();
		cboxuserName.setBounds(90, 38, 120, 20);
		cboxuserName.setEditable(true);
		panel.add(cboxuserName);
		

	    passwordField = new JPasswordField();
	    passwordField.setBounds(90, 88, 120, 21);
	    panel.add(passwordField);
	    initCombox();
	}

	public void initCombox()
	{
		File f = new File("loginUser.txt");
		if(!f.exists())
		{
			cboxuserName.addItem("请输入号码");
			return;
		}
		FileReader fis;
		try {
			fis = new FileReader(f);
			BufferedReader bif = new BufferedReader(fis);
			String temp = null;
			while((temp=bif.readLine())!=null)
			{
				cboxuserName.addItem(temp.trim());
			}
		} catch (FileNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		
	}
	
	public void writeLoginList(String userNum) throws Exception
	{
		userNum=userNum+"\n";
		File f = new File("loginUser.txt");
		if(f.exists())
		{
			FileReader fis;
			fis = new FileReader(f);
			BufferedReader bif = new BufferedReader(fis);
			String temp = null;
			while((temp=bif.readLine())!=null)
			{
				if(temp.trim().equals(userNum.trim()))
				{
						return;
				}
			}

		}
		FileOutputStream fos = new FileOutputStream(f,true);
		fos.write(userNum.getBytes());
	}
	
	public void register()
	{
		//账号输入框回车触发的事件
		cboxuserName.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FocusManager.getCurrentManager().focusNextComponent();
			}
		});
		
		//密码框回车触发的事件
	    passwordField.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent e) {
	    		button.doClick();
	    	}
	    });
	    
		//登录按钮触发的事件
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String userName =((String) cboxuserName.getSelectedItem());
				String password = new String(passwordField.getPassword());
				if(userName==null||password==null||userName.equals("")||password.equals(""))
				{
					JOptionPane.showMessageDialog(Login.this, "请补充完整信息!");
					return;
				}
				userName = userName.trim().replace("'", "");
				password = password.trim().replace("'", "");
				password = MD5.EncoderByMd5(password);
				int userNum = 0;
				try
				{
					userNum = new Integer(userName).intValue();
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(Login.this, "用户名不正确");
					return;
				}
				sh = new SqlHelper();
				sh.setStatement(sh.getConnection());
				ResultSet rs = sh.getQuery("select userName,isOnLine from tbl_Users where userNum="+userNum+" and password='"+ password +"'");
				try {
					if(rs.next())
					{
						if(rs.getInt(2)!=1)
						{
							sh.closeStatement();
							Client c = new Client(userNum);
							c.setVisible(true);
							try {
								writeLoginList(userNum+"");
							} catch (Exception e1) {
								// TODO 自动生成 catch 块
								e1.printStackTrace();
							}
							Login.this.dispose();
						}
						else
						{
							JOptionPane.showMessageDialog(Login.this, "该用户已经登录");
							return;
						}
					}
					else
					{
						errorCount++;
						if(errorCount>=3)
						{
							JOptionPane.showMessageDialog(Login.this, "连续三次错误,系统强行关闭!");
							Login.this.dispose();
							return;
						}
						JOptionPane.showMessageDialog(Login.this, "用户名或密码错误!");
					}
				} catch (SQLException e1) {
					// TODO 自动生成 catch 块
					e1.printStackTrace();
				}
			}
		});
	}
	public Login() {
		super("MyQQ登录窗口");
		getContentPane().setLayout(null);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int width = 330;
		int height = 250;
		this.setBounds((d.width - width) / 2, (d.height - height) / 2, width, height);
		this.setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ImageIcon logo = new ImageIcon("image\\top.jpg");
		final JLabel login = new JLabel(logo);
		login.setBounds(0, 0, 330, 40);
		getContentPane().add(login);
		getContentPane().setBackground(new Color(225,245,252));
		setup();
		
		/**
		 * *登录按钮触发的事件
		 */
	    button = new JButton();
		button.setText("登录");
		button.setBounds(220, 183, 80, 23);
		register();
		getContentPane().add(button);
		//
	}

}

⌨️ 快捷键说明

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