package ms;
import javax.print.DocFlavor.URL;
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JPanel;


public class Login extends JFrame{
	
    /*定义窗体组件*/
   JPanel mb1,mb2,mb3,mb4,mb5;
   JLabel bq1,bq2,bq3;
	JTextField wbk;
	JPasswordField  mmk;
	JButton queding,zhuce;
	String name;
	String password;
	Checkbox c1;
	Checkbox c2;
	//定义全局变量
	String username;
	String yhm;
	String mm;
	String mma;
	/*创建窗体*/
	public Login()
	{
		mb1=new JPanel();
		mb2=new JPanel();
		mb3=new JPanel();
		mb4=new JPanel();
		mb5=new JPanel();
		bq1=new JLabel("大学城校园美食联盟-餐饮信息系统");
		bq1.setFont(new Font("黑体",Font.BOLD,35));
		bq1.setForeground(Color.red);
		bq2=new JLabel("用户名:");
		bq3=new JLabel("密  码:");
		
		bq2.setFont(new Font("宋体",Font.BOLD,20));
		bq3.setFont(new Font("宋体",Font.BOLD,20));
		CheckboxGroup g=new CheckboxGroup();
		
		 c1=new Checkbox("用户",g,true);
		 c2=new Checkbox("管理员",g,false);
		c1.setFont(new Font("宋体",Font.BOLD,15));
		c2.setFont(new Font("宋体",Font.BOLD,15));
	
		
		
		wbk=new JTextField(10);
		mmk=new JPasswordField(10);
		queding= new JButton("登录");
		zhuce=new JButton("注册");
		mb1.add(bq1,JPanel.CENTER_ALIGNMENT);
		mb2.add(bq2);mb2.add(wbk);
		mb3.add(bq3);mb3.add(mmk);
		mb4.add(queding);
		mb4.add(zhuce);
		mb5.add(c1);mb5.add(c2);
		//设置面板为透明
		mb1.setOpaque(false);
		mb2.setOpaque(false);
		mb3.setOpaque(false);
		mb4.setOpaque(false);
		mb5.setOpaque(false);
		ImageIcon icon=new ImageIcon("1.jpg");
		JLabel label=new JLabel(icon);
		label.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());
		this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
		JPanel mb6=(JPanel)this.getContentPane();
		mb6.setOpaque(false);
		
	    //为窗体组件添加事件监听
		queding.addActionListener(new quedingListen());
		//quxiao.addActionListener(new quxiaoListen());
		zhuce.addActionListener(new zhuceListen());
		mmk.addKeyListener(new keylisten());
		//往窗体添加组件
		this.setLayout(new GridLayout(5,1));
		this.add(mb1);this.add(mb2);this.add(mb3);this.add(mb5);this.add(mb4);
		this.setVisible(true);
		this.setSize(600,350);
		this.setLocationRelativeTo(null);//居中显示
		this.setResizable(false);
	}
		
	
	
	/*输入用户名和密码的时候,按enter键快捷进入*/
	class keylisten implements KeyListener
	{
		public void keyPressed(KeyEvent arg0) {
			if(arg0.getKeyCode()==KeyEvent.VK_ENTER)
			{
				database();
				if(password==null)
				{JOptionPane.showMessageDialog(null, "用户名或密码不正确!");}
				else if(password.equals(mmk.getText()))
				{
					if(c1.getState())
					{
						new Best_Food();	
					}
					else 
					{
						new Admin_Login();
					}
				dispose();//登录后自动关闭本窗口
				}
				else{JOptionPane.showMessageDialog(null, "用户名或密码不正确!");}
			}
		}
		public void keyReleased(KeyEvent arg0) {
		}
		public void keyTyped(KeyEvent arg0) {
		}
	}
	
	/*登陆键添加事件监听*/
	class quedingListen implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			database();
			if(password==null)
			{JOptionPane.showMessageDialog(null, "用户不存在或用户名不正确!");}
			
			else if(password.equals(mmk.getText()))
			{
				if(c1.getState())
				{
					new Best_Food();//用户身份登陆	
				}
				else 
				{
					new Admin_Login();//管理员身份登陆
				}
			 dispose();//登录后自动关闭本窗口
			}
			else{JOptionPane.showMessageDialog(null, "用户名或密码不正确!");
			
			}
		}
	}
	/*database方法,用来连接数据库验证用户登录的密码*/
	public void database(){
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
			try{
				Class.forName(driverName);
				String url="jdbc:sqlserver://localhost:1433;DatabaseName=大学城校园美食联盟";
				String name="sa";
				String passWord="lch";
				Connection conn = DriverManager.getConnection(url,name,passWord);
				if(conn!=null)
				{
				}
				String user = wbk.getText();
				Statement state = conn.createStatement();
				String sql=null;
				if(c1.getState())
				{
			          sql = "select password  from login  where username = '"+user+"'";
				}
				else
				{
					 sql = "select password  from admin  where username = '"+user+"'";
				}
				ResultSet res = state.executeQuery(sql);
				while(res.next())
				{						
					 password = res.getString("password");
				}
				conn.close();	
		}catch(Exception e){
			JOptionPane.showMessageDialog(null,"数据库连接出现异常!");
		}
			
		}
		
	
	
	//注册
	class zhuceListen implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO 自动生成的方法存根
			new New_User();
		}
		
	}
	
	/*修改密码按钮的监控,需要连接数据库,修改数据库里的用户名和密码*/
	/*
	class xiugaiListen implements ActionListener{
		public void actionPerformed (ActionEvent ee)
		{	String checkname = JOptionPane.showInputDialog(null,"请输入旧用户名!");
			String checkpassword = JOptionPane.showInputDialog(null,"请输入你的旧密码!");
			String Sql1 = "select username from login where id = '1'";
			String Sql2 = "select password from login where id = '1'";
			String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
			
			try{
				Class.forName(driverName);
					
				String url="jdbc:sqlserver://localhost:1433;DatabaseName=大学城校园美食联盟";
				Connection conn = DriverManager.getConnection(url,"sa","lch");
				Statement state = conn.createStatement();
				ResultSet res = state.executeQuery(Sql1);
				while(res.next())
				{
				 yhm=res.getString("username");
				}
				ResultSet res1 = state.executeQuery(Sql2);
				while(res1.next())
				{
				mm=res1.getString("password");
				}
				if(yhm.equals(checkname)&&mm.equals(checkpassword))
				{
					String thename=JOptionPane.showInputDialog(null,"请输入新用户名!");
					String thepassword = JOptionPane.showInputDialog(null,"请输入你的新密码!");
					String SQL1="update login set username='"+thename+"' where id='1'";
					String SQL2="update login set password='"+thepassword+"' where id='1'";	
					state.executeUpdate(SQL1);
					state.executeUpdate(SQL2);
					JOptionPane.showMessageDialog(null, "修改成功!");
				}
				else
				{JOptionPane.showMessageDialog(null,"用户名或密码不正确!");}
				conn.close();	
			}catch(Exception e){
				//JOptionPane.showMessageDialog(null,"数据库连接出现异常!");
				e.printStackTrace(); 
			}
			}	
		}
		*/
	 public static void main(String [] args)
     {
    	 new Login();
    	 
     }
}
最近下载更多
Demo1111  LV30 2021年12月12日
tiashishiwo  LV5 2021年7月3日
fdsvsfds  LV2 2021年3月16日
此刀名为秋水  LV1 2021年2月20日
freedom2017  LV14 2020年6月2日
狗比lc  LV1 2020年1月2日
hanyuiqng  LV9 2019年12月24日
啊浩啊  LV7 2019年9月19日
CJ6872400  LV1 2019年7月27日
mangen  LV1 2019年6月25日
最近浏览更多
R8sixsix  LV1 2月27日
廖业贵  LV18 2月23日
adyxhss  LV2 2023年12月25日
shiaomon 2023年12月14日
暂无贡献等级
梅格妮  LV1 2023年12月5日
微信网友_6505997864357888  LV3 2023年11月10日
pangzhihui  LV12 2023年9月22日
zyb10086  LV2 2023年4月8日
jiyun2021  LV9 2023年2月3日
灬艾希  LV9 2023年2月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友