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

📄 userpass.java

📁 随着科学技术的发展
💻 JAVA
字号:
/*
 * UserPass.java
 *
 * Created on 2080年1月23日, 下午2:22
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package javaapplication6;

import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;

/**
 *
 * @author jianren
 */
public class UserPass extends Frame implements ActionListener{
    
    /** Creates a new instance of UserPass */
    TextField User,Pass;
    int wwidth = 1024;
    int whigth = 786;
    int iwidth = 250;
    int ihigth = 150;
    Frame thisframe;
    Component rframe;
    UserPass( String s, Component oframe){
        this( s);
        rframe = oframe;
        oframe.setVisible( false);
        new MSSSQLFather();
    }
    UserPass( String s){
        super(s);
        thisframe = this;
        setLayout( null);
        Label labelUser = new Label("账号");
        Label labelPass = new Label("密码");
        User = new TextField( 20);
        Pass = new TextField( 20);
        Pass.setEchoChar( '*');
        Dimension dim = getToolkit().getScreenSize();
        wwidth = dim.width;
        whigth = dim.height;
        this.setBounds( ( wwidth - iwidth) / 2, ( whigth - ihigth) / 2, iwidth, ihigth);
        add( labelUser);
        add( labelPass);
        add( User);
        add( Pass);
        User.addActionListener( this);
        Pass.addActionListener( this);
        labelUser.setBounds( iwidth / 4, ihigth / 3, 40, 20);
        labelPass.setBounds( iwidth / 4, 2 * ihigth / 3, 40, 20);
        User.setBounds( iwidth / 2, ihigth / 3, 100, 20);
        Pass.setBounds( iwidth / 2, 2 * ihigth / 3, 100, 20);
        this.setVisible( true);
        this.setResizable( false);
        addWindowListener( new WindowAdapter(){
            public void windowClosing( WindowEvent e){
                int ynn = javax.swing.JOptionPane.showConfirmDialog(
                        thisframe, "你真的要退出系统登陆吗?","确认对话框",
                        JOptionPane.YES_NO_OPTION);
                if( ynn == JOptionPane.YES_OPTION){
                    rframe.setVisible( true);
                    System.exit(0);
                }
            }
        });
    }
    
    public UserPass() {
    }

    public void actionPerformed(ActionEvent e) {
        if( e.getSource() == User){
            Pass.requestFocusInWindow();
            Pass.setSelectionStart( 0);
            Pass.setSelectionEnd( Pass.getText().length());
        }
        else if( e.getSource() == Pass){
            //
            if( User.getText() == ""){
                return ;
            }
            Statement sta = MSSSQLFather.getStatement();
                ResultSet rs;
                try {
                    rs = sta.executeQuery("select * from people where user='" + User.getText() + "'");
                    boolean flag = false;
                    while (rs.next()) {
                        if(  IsSame.isSame(rs.getString( "user"),User.getText()) && IsSame.isSame(rs.getString( "pass"),Pass.getText())){
                            flag = true;
                            JOptionPane.showMessageDialog(null, "登陆成功", "", JOptionPane.WARNING_MESSAGE);
                            rframe.setVisible(true);
                            this.dispose();
                            break;
                        }
                    }
                    if( !flag){
                        JOptionPane.showMessageDialog(null, "用户名或密码错误", "", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, "连接失败", "警告", JOptionPane.WARNING_MESSAGE);
                }
                try {
                    sta.close();
                    MSSSQLFather.close();
                } catch (SQLException ex) {
                    //ex.printStackTrace();
                }
        }
    }

}
class IsSame{
        //检查是否相同值                        ///////////// OK
    public static boolean isSame( char name[], char other[]){
        if( name.length != other.length){
            return false;
        }
        for(int Loop1 = 0; Loop1 < name.length; Loop1++){
            if( name[ Loop1 ] != other[ Loop1 ]){
                return false;
            }
        }
        return true;
    }
    public static boolean isSame( char name[], final String other){
        if( other == null){
            return false;
        }
        char other_name[] = new char[ other.length() ];
        other.getChars( 0, other.length(), other_name, 0);
        return isSame( name, other_name);
    }
    public static boolean isSame( final String name, final String other){
        if( name == null){
            return false;
        }
        char this_name[] = new char[ name.length() ];
        name.getChars( 0, other.length(), this_name, 0);
        return isSame( this_name, other);
    }
}

⌨️ 快捷键说明

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