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

📄 login.java

📁 JAVA项目开发实践-网上范例8: 1)首先建立数据源
💻 JAVA
字号:
/**************************************************
*文件名:	Login.java
*功能:	用户登陆界面
***************************************************/

import java.sql.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;

class Login extends JFrame 
{
   private JButton okButton = new JButton("确定");
   private JButton cancelButton = new JButton("取消");
   private JPanel buttonPanel = new JPanel();
   
   private JLabel userLabel = new JLabel("用户名");
   private JLabel passLabel = new JLabel("口令");
   private JPanel labelPanel = new JPanel();
   
   private JTextField userNameTextField = new JTextField(10);
   private JPasswordField passwordField = new JPasswordField(10);
   private JPanel txtPanel = new JPanel();

   private JPanel label_txt_panel = new JPanel();
   
   private int    login = 0 ;
   private conDB conIns;
   
   public Login(conDB conapp) 
   {
	this.conIns = conapp;
	
	setTitle("登录窗口");
    	setSize(250,150);
      	setResizable(false);
      	addWindowListener(new WindowAdapter() 
      	{
        	public void windowClosing(WindowEvent e) 
        	{
            	System.exit(0);
        	}
      	});

	 setLocation(350,200);

	 //"确定"按钮事件监听
	 okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) 
            {
            		//事件处理函数
               	loginOperate();
               	if(login == 1)
               	{
               		//用户名和口令正确,打开主界面
               		JFrame mainGUI = new createGUI(conIns);
				mainGUI.show();

               		dispose();
               	}
            }
        });	 
	 
	 //"取消"按钮事件监听
	 cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) 
            	{
            		//直接退出程序
            		System.exit(0);
            	}
        });	 

	//以下是界面设置
	 buttonPanel.setLayout(new GridLayout(1,2));
	 buttonPanel.add(okButton);
	 buttonPanel.add(cancelButton);
	 
	 labelPanel.setLayout(new GridLayout(2,1));
	 labelPanel.add(userLabel);
	 labelPanel.add(passLabel);

	txtPanel.setLayout(new GridLayout(2,1));
	txtPanel.add(userNameTextField);
	txtPanel.add(passwordField);

	label_txt_panel.setLayout(new GridLayout(1,2));
	label_txt_panel.add(labelPanel);
	label_txt_panel.add(txtPanel);

	Container contentPane=getContentPane();
    	contentPane.add(label_txt_panel,"Center");
    	contentPane.add(buttonPanel,"South");


	
   }
   
    //"确定"按钮的事件处理函数
     private void loginOperate() {
     		String loginquery;
     		String loginusename = userNameTextField.getText();
         	String loginpassword = passwordField.getText();
     	try
     	{
         	
         	loginquery = "select * from login where (username='"+loginusename +
   							  "' and	password = '"+loginpassword+"')";
   							  
   		conIns.stm = conIns.con.createStatement(); 
        	conIns.rs = conIns.stm.executeQuery( loginquery ); 
        	boolean Records = conIns.rs.next();
        	if ( ! Records ) 
      		{ 
      			//弹出消息框
        		JOptionPane.showMessageDialog( this,"登录失败" );
			//清空文本框,等待用户的再次输入
			userNameTextField.setText("");
			passwordField.setText("");
        		return; 
      		} 
      		else
      		{
      			login = 1 ;	
      		}
      		//conIns.con.close();
        }
     	catch(SQLException sqlex)
     	{
     		sqlex.printStackTrace();
     	}	
     }
}

⌨️ 快捷键说明

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