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

📄 login.java

📁 主要是对于JAVA的编程的基本语言 希望能够帮得上你。
💻 JAVA
字号:
package StudentManagement;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Login extends JFrame {

	/*
	 * 声明部分
	 */	
	JLabel lblUserName, lblPassword;
	JTextField txtUserName;
	JPasswordField txtPassword;
	JButton btnOK, btnCancel;
	JPanel panTop, panMiddle, panBottom;
	String strUserName, strPassword;
	static Login login;
	MainWindow main;
	Connection conn;
	Statement stmt;
	ResultSet rs;

	/*
	 * 构造方法
	 */
	public Login(String title) {
		super(title);
		initialize();
	}

	private void initialize() {
		/*
		 * 创建对象 
		 */
		lblUserName = new JLabel("输入用户名:");
		lblPassword = new JLabel("请输入密码:");
		txtUserName = new JTextField("", 20);
		txtPassword = new JPasswordField("", 20);
		btnOK = new JButton("确定[O]");
		btnCancel = new JButton("取消[C]");
		panTop = new JPanel();
		panMiddle = new JPanel();
		panBottom = new JPanel();
		main = new MainWindow("学生档案管理系统V1.0");

		/*
		 * 添加组件 
		 */
		this.getContentPane().setLayout(new GridLayout(3,1));
		this.getContentPane().add(panTop);
		this.getContentPane().add(panMiddle);
		this.getContentPane().add(panBottom);
		
		panTop.add(lblUserName);
		panTop.add(txtUserName);
		panMiddle.add(lblPassword);
		panMiddle.add(txtPassword);
		panBottom.add(btnOK);
		panBottom.add(btnCancel);		

		/*
		 * 设置属性 
		 */		
		this.setSize(250, 200); // 设置窗口大小
		this.setLocationRelativeTo(null);// 让窗口居中
		this.setResizable(false); // 窗口不可调整大小
		this.pack(); // 使窗口恰好容纳组件
		this.setVisible(true); // 让窗口可见		
		btnOK.setMnemonic(KeyEvent.VK_O); //设置热键字母
		btnCancel.setMnemonic(KeyEvent.VK_C);//设置热键字母	
		txtPassword.setEchoChar('*');//设置回显字符
		
		try {
			//安装JDBC-ODBC桥接器
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			//连接数据库
			conn=DriverManager.getConnection("jdbc:odbc:student");
			//创建SQL语句
			stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			
		} catch (ClassNotFoundException e1) {			
			JOptionPane.showMessageDialog(login, e1.getMessage(),"学生管理系统",JOptionPane.ERROR_MESSAGE);;
		} catch (SQLException e1) {			
			JOptionPane.showMessageDialog(login, e1.getMessage(),"学生管理系统",JOptionPane.ERROR_MESSAGE);;
		}

		/*
		 * 注册监听器,编写事件处理代码
		 * 采用匿名内部类方式来实现
		*/		

		//确定按钮单击事件
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				strUserName = txtUserName.getText().trim();
				strPassword = txtPassword.getText().trim();				
				//执行SQL命令获得结果集
				try {
					rs=stmt.executeQuery("select * from user where 用户名='"+strUserName+"'");
					if(rs.first()){
						if(strPassword.equals(rs.getString(2))){
							login.setVisible(false);
							JOptionPane.showMessageDialog(login, "欢迎使用学生管理系统V1.0!","学生管理系统",JOptionPane.INFORMATION_MESSAGE);
							main.setVisible(true);
							login.dispose();
						}else{
							login.setVisible(false);
							JOptionPane.showMessageDialog(login, "密码不正确,请重新输入密码!","学生管理系统",JOptionPane.ERROR_MESSAGE);
							login.setVisible(true);
							txtPassword.setSelectionStart(0);
							txtPassword.setSelectionEnd(txtPassword.getPassword().length);
							txtPassword.requestFocus();
						}
					}else{
						login.setVisible(false);
						JOptionPane.showMessageDialog(login, "用户名不存在,请重新输入用户名!","学生管理系统",JOptionPane.ERROR_MESSAGE);
						login.setVisible(true);
						txtPassword.setText("");
						txtUserName.requestFocus();
					}
				} catch (SQLException e1) {					
					JOptionPane.showMessageDialog(login, e1.getMessage(),"学生管理系统",JOptionPane.ERROR_MESSAGE);;
				}
			}
		});

		//确定按钮按键事件
		btnOK.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 10) {
					strUserName = txtUserName.getText().trim();
					strPassword = String.valueOf(txtPassword.getPassword());				
					//执行SQL命令获得结果集
					try {
						rs=stmt.executeQuery("select * from user where 用户名='"+strUserName+"'");
						if(rs.first()){
							if(strPassword.equals(rs.getString(2))){
								login.setVisible(false);
								JOptionPane.showMessageDialog(login, "欢迎使用学生管理系统V1.0!","学生管理系统",JOptionPane.INFORMATION_MESSAGE);
								main.setVisible(true);
								login.dispose();
							}else{
								login.setVisible(false);
								JOptionPane.showMessageDialog(login, "密码不正确,请重新输入密码!","学生管理系统",JOptionPane.ERROR_MESSAGE);
								login.setVisible(true);
								txtPassword.setText("");
								txtPassword.requestFocus();
							}
						}else{
							login.setVisible(false);
							JOptionPane.showMessageDialog(login, "用户名不存在,请重新输入用户名!","学生管理系统",JOptionPane.ERROR_MESSAGE);
							login.setVisible(true);
							txtUserName.setSelectionStart(0);
							txtUserName.setSelectionEnd(txtUserName.getText().length());
							txtUserName.requestFocus();
						}
					} catch (SQLException e1) {					
						JOptionPane.showMessageDialog(login, e1.getMessage(),"学生管理系统",JOptionPane.ERROR_MESSAGE);;
					}
				}
			}
		});

		//取消按钮单击事件
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		//用户名文本框按键事件
		txtUserName.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 10) {
					txtPassword.requestFocus();
				}
			}
		});

		//密码文本框按键事件
		txtPassword.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 10) {
					btnOK.requestFocus();
				}
			}
		});
	}

	//主方法
	public static void main(String[] args) {
		login = new Login("登录");
	}
}

⌨️ 快捷键说明

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