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

📄 loginjdialog.java

📁 图书管理系统
💻 JAVA
字号:
import java.awt.HeadlessException;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import java.sql.*;

public class LoginJDialog extends JDialog
{    
    /** Creates a new instance of LoginJDialog */
    public LoginJDialog(JFrame owner) {
        super(owner,"登陆",true);
        this.setLayout(null);
        //初始化各个控件
        jLabelUserName=new JLabel("用户名:");
        jLabelPassword=new JLabel("密码:");
        jcombUserName=new JComboBox();
        jpfPassword=new JPasswordField("");
        jButtonOk=new JButton("确定");
        jButtonOk.registerKeyboardAction(new OkAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), JComponent.WHEN_IN_FOCUSED_WINDOW);
        jButtonCancel=new JButton("取消");
        jButtonCancel.registerKeyboardAction(new CancelAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0), JComponent.WHEN_IN_FOCUSED_WINDOW);
        //获取数据库中所有的用户名
        this.Update_Con();
        //设置控件位置
        jLabelUserName.setBounds(10,20,80,20);
        jLabelPassword.setBounds(10,50,80,20);
        jcombUserName.setBounds(100,20,150,20);
        jpfPassword.setBounds(100,50,150,20);
        jButtonOk.setBounds(80,90,60,20);
        jButtonCancel.setBounds(160,90,60,20);
        //为按钮添加监听器
        jButtonOk.addActionListener(new OkAction());
        jButtonCancel.addActionListener(new CancelAction());
        //将控件添加到容器中
        this.add(jLabelUserName);
        this.add(jLabelPassword);
        this.add(jcombUserName);
        this.add(jpfPassword);
        this.add(jButtonOk);
        this.add(jButtonCancel);
        //设置位置,大小,标题
        this.setBounds(280,320,300,150);
        this.setTitle("登陆");
        this.setResizable(false);
    }
    public String getUserName()
    {
        return (String)jcombUserName.getItemAt(index);
    }
    public void Update_Con()
    {
        user.clear();
        pwds.clear();
        jcombUserName.removeAllItems();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ex) {
            JOptionPane.showConfirmDialog(null,"找不到数据库驱动程序!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
            flag=0;
            return;
        }
        try {
            String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=sources\\Data.mdb;pwd=shujuyuan";
            Connection con=DriverManager.getConnection(url);
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select * from users");
            while(rs.next())
            {
                String s=new String(rs.getString("name"));  //相当于rs.getString(1)
                String p=new String(rs.getString("pwd"));  //相当于rs.getString(2)
                user.add(s);
                pwds.add(p);
            }
            con.close();
            st.close();
//            rs.close();
        } catch (Exception ex) {
            JOptionPane.showConfirmDialog(null,"用户数据库文件不存在!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
            flag=0;
            return;
        }
        for(String u:user) jcombUserName.addItem(u);
    }
    public void setFlag(int f)
    {
        flag=f;
    }
    public int getFlag()
    {
        return flag;
    }
    private JLabel jLabelUserName;
    private JLabel jLabelPassword;
    private JComboBox jcombUserName;
    private JPasswordField jpfPassword;
    private JButton jButtonOk,jButtonCancel;
    private ArrayList<String> user=new ArrayList<String>();
    private ArrayList<String> pwds=new ArrayList<String>();
    private int index=0,flag=0;    
    
    private class OkAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e) {
            index=jcombUserName.getSelectedIndex();
            String pwd=new String(jpfPassword.getPassword());
            if(pwd.equalsIgnoreCase(pwds.get(index)))
            {
                JOptionPane.showConfirmDialog(null,"欢迎使用!","Welcome",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);
                jpfPassword.setText(null);
                setVisible(false);
                flag=1;
            }
            else
            {                
                jpfPassword.setText(null);
                JOptionPane.showConfirmDialog(null,"账号或密码错误!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
            } 
        }//actionperformed        
    }
     private class CancelAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e) {
           LoginJDialog.this.setVisible(false);
        }
    }
}

⌨️ 快捷键说明

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