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

📄 login.java

📁 完整的JAVA工程
💻 JAVA
字号:
package employee;

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

public class Login extends JFrame implements KeyListener,ActionListener{
	Container contentPane;
	JPanel centerPanel=new JPanel();
	JPanel upPanel=new JPanel();
	JPanel downPanel=new JPanel();
	String uid;
	String ukey;
	ResultSet rs;
	
	//框架大小
	Dimension faceSize=new Dimension(400,300);
	
	JLabel jLabel1=new JLabel();
	JLabel jLabel2=new JLabel();
	JLabel jLabel3=new JLabel();
	JTextField user=new JTextField(10);
	JPasswordField password=new JPasswordField(10);
	JButton jButton1=new JButton();
	JButton jButton2=new JButton();
	JButton jButton3=new JButton();
	
	GridBagLayout gridBag=new GridBagLayout();
	GridBagConstraints gridBagCon;
	
	Database DB = new Database();
	
	public Login(){
		this.setSize(faceSize);
		this.setTitle("管理员登陆");
		this.setResizable(false);
		try{
			Init();
			upInit();
			downInit();
			DB.OpenConn();
			String sql="select said,password from password";
			rs = DB.executeQuery(sql);
			if(rs.next()){
				uid=rs.getString("said");
			    ukey=rs.getString("password");
			}
			
		}
		catch(Exception e){e.printStackTrace();
		}
		finally {
			DB.closeStmt();
			DB.closeConn();
		}
	}
	public void Init() throws Exception{
		contentPane=this.getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		//中间面板的布局
		centerPanel.setLayout(gridBag);
		
		jLabel1.setText("用户名:");
		jLabel1.setFont(new Font("Dialog",0,12));
		gridBagCon = new GridBagConstraints();
		gridBagCon.gridx = 0;
		gridBagCon.gridy = 0;
		gridBagCon.insets = new Insets(10,10,10,1);
		gridBag.setConstraints(jLabel1,gridBagCon);
		centerPanel.add(jLabel1);

		gridBagCon = new GridBagConstraints();
		gridBagCon.gridx = 1;
		gridBagCon.gridy = 0;
		gridBagCon.insets = new Insets(10,1,10,10);
		gridBag.setConstraints(user,gridBagCon);
		centerPanel.add(user);
		
		jLabel2.setText("密码:");
		jLabel2.setFont(new Font("Dialog",0,12));
		gridBagCon = new GridBagConstraints();
		gridBagCon.gridx = 0;
		gridBagCon.gridy = 1;
		gridBagCon.insets = new Insets(10,15,10,1);
		gridBag.setConstraints(jLabel2,gridBagCon);
		centerPanel.add(jLabel2);

		gridBagCon = new GridBagConstraints();
		gridBagCon.gridx = 1;
		gridBagCon.gridy = 1;
		gridBagCon.insets = new Insets(10,1,10,10);
		gridBag.setConstraints(password,gridBagCon);
		centerPanel.add(password);
		
		contentPane.add(centerPanel,BorderLayout.CENTER);
		
		user.setEditable(true);
		password.setEditable(true);
	}
	//上部面板的布局
	public void upInit(){
		jLabel3.setText("管理员登陆窗口");
		jLabel3.setFont(new Font("Dialog",0,12));
		upPanel.add(jLabel3);
		contentPane.add(upPanel,BorderLayout.NORTH);
	}
	//下部面板的布局
	public void downInit(){
		jButton1.setText("确定");
		jButton1.setFont(new Font("Dialog",0,12));
		downPanel.add(jButton1);
		jButton2.setText("清空");
		jButton2.setFont(new Font("Dialog",0,12));
		downPanel.add(jButton2);
		jButton3.setText("退出");
		jButton3.setFont(new Font("Dialog",0,12));
		downPanel.add(jButton3);
		
		contentPane.add(downPanel,BorderLayout.SOUTH);
		
		user.addKeyListener(this);
		password.addKeyListener(this);
		jButton1.addKeyListener(this);
		jButton2.addKeyListener(this);
		jButton3.addKeyListener(this);
		
		jButton1.addActionListener(this);
		jButton2.addActionListener(this);
		jButton3.addActionListener(this);
	}
	//事件处理
	public void keyPressed(KeyEvent e){
		if(e.getKeyCode()==KeyEvent.VK_ENTER){//按下回车键
		   Object obj=e.getSource();
		   if(obj==user){
		   	password.requestFocus();//使密码框获得焦点
		   }
		   else if(obj==password){
		   	jButton1.requestFocus();//使"确认"按钮获得焦点
		   }
		   else if(obj==jButton1){
		   	jButton1.doClick();//触发"确认"按钮
		   }
		   else if(obj==jButton2){
		   	jButton2.doClick();
		   }
		   else if(obj==jButton3){
		   	jButton2.doClick();
		   }
		}
	}
	public void keyReleased(KeyEvent e){}
	public void keyTyped(KeyEvent e){}
	
	public void actionPerformed(ActionEvent e){
		boolean packFrame = false;
		Object obj=e.getSource();
		if(obj==jButton1){//确定
		  String userid=user.getText();
		  String key=new String(password.getPassword());
		  //截取字符串的头尾空格
		  userid.trim();
		  key.trim();
		  if(userid.equals(uid)&&key.equals(ukey)){
				JOptionPane.showMessageDialog(null, "登录成功!");
				this.setVisible(false);
				EmpMain frame = new EmpMain();
				if (packFrame) {
			           frame.pack();
		        }
		        else {
			          frame.validate();
		        }
		  }
		  else{
		  	JOptionPane.showMessageDialog(null,"密码错误,请输入正确密码!");
		  	user.setText("");//清空用户名文本框
		  	password.setText("");//清空密码文本框
		  	user.requestFocus();//将光标停在用户名文本框中
		  }
		}
		else if(obj==jButton2){//清空
		   setNull();
		}
		else if(obj==jButton3){//退出
		   this.dispose();
		}
	}
	void setNull(){
		user.setText(null);
		password.setText(null);
		user.requestFocus();
	}
}

⌨️ 快捷键说明

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