loginsystem.java

来自「用Jsp实现的图书管理系统代码」· Java 代码 · 共 97 行

JAVA
97
字号
//源程序名:LoginSystem.java
//这是开始程序时的登录界面
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class LoginSystem extends JFrame implements ActionListener
	{
		JPanel pBasePanel=new JPanel();
		JPanel pCenter=new JPanel();
		JPanel pWest=new JPanel();
		JLabel title=new JLabel("<html><font size=5>管理员登录");
		JLabel lbUserName=new JLabel("用户名:");
		JLabel lbUserPass=new JLabel("密     码:");
		JTextField tfUserName=new JTextField(10);
		JTextField infoTip=new JTextField("请输入用户名和密码...",15);
		JPasswordField tfUserPass=new JPasswordField(20);
		JButton btnLogin=new JButton("登录");
		JButton btnCancel=new JButton("取消");
		LoginSystem()
		{
			super("系统登录");
			Dimension scrDim = Toolkit.getDefaultToolkit().getScreenSize();
			int windowX=scrDim.width;int windowY=scrDim.height;
			this.setSize(windowX/3,windowY/3);//设置窗口大小
			int x = (scrDim.width - getSize().width) /2;
			int y = (scrDim.height -getSize().height) /2;
			this.setLocation(x, y);//设置窗口位置
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setContentPane(pBasePanel);pBasePanel.setLayout(new BorderLayout());
			pBasePanel.add("Center",pCenter);pCenter.setLayout(null);
			pBasePanel.add("West",pWest);pWest.add(new JLabel(new ImageIcon("images/login.gif")));
			pCenter.add(title);title.setBounds(12,0,130,50);
			pCenter.add(lbUserName);lbUserName.setBounds(0,70,50,30);
			pCenter.add(tfUserName);tfUserName.setBounds(45,70,120,30);
			pCenter.add(lbUserPass);lbUserPass.setBounds(0,110,50,30);
			pCenter.add(tfUserPass);tfUserPass.setBounds(45,110,120,30);
			pCenter.add(btnLogin);btnLogin.setBounds(15,150,60,30);
			pCenter.add(btnCancel);btnCancel.setBounds(100,150,60,30);
			pCenter.add(infoTip);infoTip.setBounds(0,200,170,20);infoTip.setEditable(false);
			this.validate();
			this.show();
			btnLogin.addActionListener(this);
			tfUserName.addActionListener(this);
			tfUserPass.addActionListener(this);
			btnCancel.addActionListener(this);
			}
		public void actionPerformed(ActionEvent e)
			{
			if (e.getSource()==btnCancel)
			{
				System.exit(0);
			}
			if (e.getSource()==btnLogin||e.getSource()==tfUserPass||e.getSource()==tfUserName)
			{
				try
					{
					String username=tfUserName.getText();
					String password=tfUserPass.getText();
					DatabaseConn loginCheck=new DatabaseConn();
					ResultSet rs=loginCheck.executeQuery("select * from admin");
					rs.next();
					String usernameT=loginCheck.rs.getString(2);
					String passwordT=loginCheck.rs.getString(3);
					if (username.equals(usernameT)&&password.equals(passwordT))
					{	
						infoTip.setText("成功登录系统!!");
						this.dispose();
						new Main();//进入程序主界面
						rs.close();
						loginCheck.rs.close();
						loginCheck.conn.close();
					}
					if (username.equals(usernameT)&&!password.equals(passwordT))
					{
						infoTip.setText("密码不正确!!");
						rs.beforeFirst();
					}
					if (!username.equals(usernameT))
					{	
						infoTip.setText("用户名不正确!!");
						rs.beforeFirst();
					}
					}
				catch(Exception sqlex)
					{
					System.out.println("登录系统时,出现异常情况....");
					System.out.println("异常信息:"+sqlex.getMessage());
						}
			}
			}
		public static void main(String args[])
			{
			LoginSystem loginsystem=new LoginSystem();
			}
	}

⌨️ 快捷键说明

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