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

📄 loginwin.java

📁 java酒店管理系统
💻 JAVA
字号:
package myprojects.login; 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;  
import java.io.*; 
//-----------------导入自定义的包---------------------------
import myprojects.hotelmanager.HotelManagerFrame;
import myprojects.transactionController.*;
/**
 * @(#)HotelManager.java
 *
 * 登陆身份验证
 *
 * @author 
 * @version 1.00 05/12/25
 */
  
   
public class LoginWin extends JFrame    
{	
	//***********//用户身份验证程序界面   	
  	JPanel loginInfoPanel = new JPanel();
  	JLabel accountIdLabel = new JLabel();
  	JLabel passwordLabel = new JLabel();
  	JLabel hintLoginInfoLabel = new JLabel();
  	JTextField accountIdText = new JTextField();
  	JPasswordField passwordText = new JPasswordField();   
  	JPanel loginInfoButtonPanel = new JPanel();
  	JButton login = new JButton();  	
  	JButton newuser = new JButton();
  	JButton quit = new JButton();  	
  	int accountKindMark = 0;  //登陆标志,隐身登陆为1
  	//*********** 		
  	void closeframe()
  	{
  		this.dispose();
  	}
  	class ChooseLogModel extends JPanel
  	{	
  		JPanel OnlineHidePanel = new JPanel();
  		ButtonGroup group = new ButtonGroup();
  		JRadioButton destClerkMarkRadioButton = new JRadioButton("接待员",true);
  		JRadioButton managerMarkRadioButton = new JRadioButton("经理",false);  		
  		JLabel hintChOnlineHide = new JLabel("请选择登录方式               ");	
  		
  		
  		JPanel LookAndFeelPanel = new JPanel();
  		ButtonGroup group2 = new ButtonGroup();
  		JRadioButton MetalLF = new JRadioButton("Metal",true);
  		JRadioButton MotifLF = new JRadioButton("Motif",false);  	
  		JRadioButton WindowsLF = new JRadioButton("Windows",false);  		
  		
  		JLabel hintChModel = new JLabel("请选择登录界面方式             ");	  		
  		
  	
  		ChooseLogModel()
  		{  			
  			this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
  			add(Box.createRigidArea(new Dimension(0,15)));
  			this.add(hintChOnlineHide);
  			group.add(destClerkMarkRadioButton);
  			group.add(managerMarkRadioButton); 
  			  			
  			OnlineHidePanel.add(destClerkMarkRadioButton);
  			OnlineHidePanel.add(managerMarkRadioButton);
  			this.add(OnlineHidePanel);
  			
    		destClerkMarkRadioButton.addMouseListener(new java.awt.event.MouseAdapter() {
     	 		public void mouseClicked(MouseEvent e) {
      			 	accountKindMark = 0;
      			 	System.out.println ("accountKindMark = 0");
      			}
    		});    
    		managerMarkRadioButton.addMouseListener(new java.awt.event.MouseAdapter() {
     	 		public void mouseClicked(MouseEvent e) {
     	 			accountKindMark = 1;
      			 	System.out.println ("accountKindMark = 1");
      			}
    		});    
    		
    		//add(Box.createRigidArea(new Dimension(0,10)));
  			this.add(hintChModel);
  			group2.add(MetalLF);
  			group2.add(MotifLF); 
  			group2.add(WindowsLF);   			
  			LookAndFeelPanel.add(MetalLF);
  			LookAndFeelPanel.add(MotifLF);
  			LookAndFeelPanel.add(WindowsLF);
  			this.add(LookAndFeelPanel);
  			MetalLF.addMouseListener(new java.awt.event.MouseAdapter() {
     	 		public void mouseClicked(MouseEvent e) {
      			 	System.out.println ("Metal");
      			 	metalLookandfeel();
      			}
    		}); 
    		MotifLF.addMouseListener(new java.awt.event.MouseAdapter() {
     	 		public void mouseClicked(MouseEvent e) {
      			 	System.out.println ("Motif");
      			 	motifLookandfeel();
      			}
    		}); 
    		WindowsLF.addMouseListener(new java.awt.event.MouseAdapter() {
     	 		public void mouseClicked(MouseEvent e) {
      			 	System.out.println ("Windows");
      			 	windowsLookandfeel();
      			}
    		}); 
    		this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
  		}
  	}
  	void metalLookandfeel()
  	{	
  		String plaf = "javax.swing.plaf.metal.MetalLookAndFeel";
      	try{
      		UIManager.setLookAndFeel(plaf);
      		SwingUtilities.updateComponentTreeUI(this);
      	}catch(Exception ee)
      	{ee.printStackTrace();	}
      	
  	}
  	void windowsLookandfeel()
  	{
  		String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
      	try{
      		UIManager.setLookAndFeel(plaf);
      		SwingUtilities.updateComponentTreeUI(this);
      	}catch(Exception ee)
      	{ee.printStackTrace();	}
  	}
  	void motifLookandfeel()
  	{
  		String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
      	try{
      		UIManager.setLookAndFeel(plaf);
      		SwingUtilities.updateComponentTreeUI(this);
      	}catch(Exception ee)
      	{ee.printStackTrace();	}
  	}
  	
  	class LoginPanel extends JPanel
  	{
  		LoginPanel()
  		{
  			enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  			try
  			{
  				loginPanelInit();  				
    		} 
    		catch(Exception e) { e.printStackTrace(); }
  		}
  		private void loginPanelInit() throws Exception 
  		{ 
  			setLayout(null);
    		loginInfoPanel.setBounds(new Rectangle(2, 3, 350, 110));
    		loginInfoPanel.setLayout(null);
    		hintLoginInfoLabel.setText("请输入你的信息");
    		hintLoginInfoLabel.setBounds(new Rectangle(70, 13, 103, 18));
    		accountIdLabel.setText("帐号: ");
    		accountIdLabel.setBounds(new Rectangle(70, 43, 58, 18));
    		accountIdText.setBounds(new Rectangle(130, 42, 97, 22));
    		passwordLabel.setText("密码: ");
    		passwordLabel.setBounds(new Rectangle(70, 80, 67, 18));
    		passwordText.setBounds(new Rectangle(130, 79, 97, 22));
    		loginInfoButtonPanel.setBounds(new Rectangle(8, 114, 347, 151));
    		loginInfoButtonPanel.setLayout(null);
    		login.setText("登录");
    		login.setBounds(new Rectangle(30+25, 10, 79, 29));
    
    		//匿名内类处理登陆按钮事件
    		login.addMouseListener(new java.awt.event.MouseAdapter() {
     	 		public void mouseClicked(MouseEvent e) {
      			 	login_mouseClicked(e);
      			}
    		});
    		
    		login.addKeyListener(new java.awt.event.KeyAdapter(){	
      			public void keyReleased(KeyEvent e)
      			{

      			}
      			public void keyTyped(KeyEvent e)
      			{
        			login_keyTyped(e);
      			}
    		});        	
    
    		quit.setText("退出");
    		quit.setBounds(new Rectangle(210-40, 10, 79, 29));
    		//匿名内类处理退出按钮事件
    		quit.addMouseListener(new java.awt.event.MouseAdapter() {
      			public void mouseClicked(MouseEvent e) {
        			quit_mouseClicked(e);
      			}
    		});
    		
    
    		add(loginInfoPanel, null);    
    		loginInfoPanel.add(hintLoginInfoLabel, null);
    		loginInfoPanel.add(accountIdLabel, null);
    		loginInfoPanel.add(accountIdText, null);
    		loginInfoPanel.add(passwordLabel, null);
    		loginInfoPanel.add(passwordText, null);    
    		add(loginInfoButtonPanel, null);
    		loginInfoButtonPanel.add(login, null);
    		loginInfoButtonPanel.add(quit, null);
    		loginInfoButtonPanel.add(newuser, null);   
    
  		}
  	}
  	void login_keyTyped(KeyEvent e)		//按空格键代替按钮
  	{
  		loginKeyButton();
  	}
  	void quit_mouseClicked(MouseEvent e)//关闭按扭
  	{
  		closeframe();
  		System.exit(0);
  	}  	
  	
  	void login_mouseClicked(MouseEvent e) //登录按扭   
   	{
   		loginKeyButton();   		
   	}  
   	void loginKeyButton()
   	{
   		
   		//将密码框中字符数组转化为字符串
  		String passwd=new String(passwordText.getPassword());
		System.out.println(passwd);
   		String accountId = accountIdText.getText();
   		if(accountId.trim().equals("")){
   			JOptionPane.showMessageDialog(null,"帐号不能为空",
   						"Error",JOptionPane.INFORMATION_MESSAGE);
   			return ;      	
   		}
   		if(passwd.trim().equals("")){
   			JOptionPane.showMessageDialog(null,"密码不能为空",
   						"Error",JOptionPane.INFORMATION_MESSAGE);      	
   			return ;
   		}
   		System.out.println ("accountId: "+accountId) ;
   		System.out.println("password: "+passwd);        
        boolean isCorrent=true;
        Login logining=new Login();
        isCorrent=logining.validateLoginInfo(accountId,passwd);
   		      	
        if(!isCorrent) 
        	JOptionPane.showMessageDialog(this,"对不起,出错了,请重新输入!","Error",JOptionPane.INFORMATION_MESSAGE);
      	else{
          	//如果成功就打开主程序
       		closeframe();
       		//传入帐户信息,以便显示不同的功能菜单,前台接待员做前台的事情,
       		//权限管理是经理做的事情
            HotelManagerFrame frame = new HotelManagerFrame(logining.accountInfo);             	
        }    	
   	}
   	
   	public LoginWin()
   	{
   		LoginWinInit();
   	}
   	void LoginWinInit()
   	{   		
   	//	JFrame.setDefaultLookAndFeelDecorated(true);
     
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	//生成标签面板
        JComponent newContentPane = new LoginSetPanel();
        newContentPane.setOpaque(true); //content panes must be opaque
        getContentPane().add(newContentPane,BorderLayout.CENTER);

       //Display the window.
        pack();
        setResizable(false);	//使之不能改变大小
    	setSize(new Dimension(344, 245)); 
    	setTitle("酒店管理系统--用户登录");
        setLocation(280,220);
        setVisible(true);
   	}
   	class LoginSetPanel extends JPanel
   	{
   		LoginPanel loginpanel = new LoginPanel();	//登陆面板  	
  		ChooseLogModel chooselogmodel = new ChooseLogModel();	//选择登陆模式
  		//登陆外观和登陆方式,隐身还是上线
   		LoginSetPanel()
   		{
   			super(new GridLayout(1, 1));    	
        	
        	JTabbedPane tabbedPane = new JTabbedPane();
        	ImageIcon icon = createImageIcon(".\\images\\Picture1.jpg");       		
       		ImageIcon setmodelicon = createImageIcon(".\\images\\setmodel.gif");
         	
        	tabbedPane.addTab("登陆", icon, loginpanel);
        	tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        	JComponent panel3 = makeTextPanel("Panel #2");
        	tabbedPane.addTab("登陆模式", setmodelicon, chooselogmodel);
        	tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

        	add(tabbedPane);          
   		}   		
   	}
   	   
    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = LoginWin.class.getResource(path);
        if (imgURL != null) 
        {
            return new ImageIcon(imgURL);
        }else{
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

/*try{//Integer.parseInt(string)然后catch NumberFormatException, 
   			//如果没有触发Exception,那就是数字了
   			int checkint = Integer.parseInt(accountIdText.getText().toString().trim());  
   		}catch(NumberFormatException nfe)
   		{
   			JOptionPane.showMessageDialog(this,"输入错误,请重新输入!","Error",JOptionPane.INFORMATION_MESSAGE);
      		return;
}*/

/*
 *图片路径问题解决方法:
 * Java运行时候,看的是类,不是文件,Javac看的是文件,而Java命令看的是类
 * 因此,要装载图片文件时候,将图片目录放到图片所用的类文件,.class文件的同目录下
 *.\\images\\setmodel.gif 即可解决图片问题
 **/

⌨️ 快捷键说明

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