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

📄 loginframe.java

📁 学生考试系统 有学生和老师登录。 学生登录可以选择不同的题库进行考试或练习
💻 JAVA
字号:
package com.svse.view;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.svse.dao.QuestionDao;
import javax.swing.JOptionPane;
import com.svse.bean.QuestionBean;

public class LoginFrame extends JFrame {
    JPanel contentPane;
    JPanel jPanel1 = new JPanel();
    JLabel jLabel1 = new JLabel();
    JLabel lbl_userName = new JLabel();
    JTextField txt_userName = new JTextField();
    JLabel lbl_pwd = new JLabel();
    JPasswordField pf_pwd = new JPasswordField();
    JLabel lbl_loginWise = new JLabel();
    JComboBox cbo_loginWise = new JComboBox();
    JButton btn_login = new JButton();
    JButton btn_exit = new JButton();
    public LoginFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(301, 300));
        setTitle("登陆窗口");
        jPanel1.setBounds(new Rectangle(110, 0, 301, 300));
        jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
        jLabel1.setText("欢 迎 登 陆");
        jLabel1.setBounds(new Rectangle(103, 22, 101, 24));
        lbl_userName.setText("用户名:");
        lbl_userName.setBounds(new Rectangle(49, 74, 58, 15));
        txt_userName.setBounds(new Rectangle(104, 71, 122, 21));
        lbl_pwd.setText(" 密 码:");
        lbl_pwd.setBounds(new Rectangle(54, 115, 50, 15));
        pf_pwd.setBounds(new Rectangle(104, 112, 122, 21));
        lbl_loginWise.setText("登陆方式:");
        lbl_loginWise.setBounds(new Rectangle(36, 162, 69, 15));
        cbo_loginWise.setBounds(new Rectangle(104, 158, 122, 21));
        btn_login.setBounds(new Rectangle(60, 204, 66, 25));
        btn_login.setText("登 陆");
        btn_login.addActionListener(new LoginFrame_btn_login_actionAdapter(this));
        btn_exit.setBounds(new Rectangle(157, 204, 66, 25));
        btn_exit.setText("退 出");
        btn_exit.addActionListener(new LoginFrame_btn_exit_actionAdapter(this));
        contentPane.add(jLabel1);
        contentPane.add(lbl_loginWise);
        contentPane.add(lbl_pwd);
        contentPane.add(lbl_userName);
        contentPane.add(pf_pwd);
        contentPane.add(txt_userName);
        contentPane.add(cbo_loginWise);
        contentPane.add(btn_login);
        contentPane.add(btn_exit);
        setItem();
    }

    //给下拉列表赋值
    public void setItem() {
        this.cbo_loginWise.addItem("管理员登陆");
        this.cbo_loginWise.addItem("学生登陆");

    }

    //登陆按钮的单击事件
    public void btn_login_actionPerformed(ActionEvent e) {
        //先取得用户的登陆方式
        String type = (String)this.cbo_loginWise.getSelectedItem();
        String dbtype = "";
        if (type.equals("管理员登陆")) {
            dbtype = "adminName";
        } else if (type.equals("学生登陆")) {
            dbtype = "stuName";
        }
        //界面中文本得到用户输入的信息
        String userName = this.txt_userName.getText().trim();
        String userPwd = new String(this.pf_pwd.getPassword());
        //判断用户是否输入信息
        if (userName.length() < 1 || userPwd.length() < 1) {
            JOptionPane.showMessageDialog(this, "请输入登陆名和密码");
        }

        //再验证用户是否登陆成功
        QuestionDao dao = new QuestionDao();
        QuestionBean bean = dao.getInfo(dbtype);
        if (bean != null) {
            if (dbtype.equals("adminName")) {
                if (userName.equals(bean.getAdminName()) ||
                    userPwd.equals(bean.getAdminPwd())) {
                    //弹出管理员登陆界面(该界面只有功能选择,而没有实质文本内容)
                    AdministerFrame admin = new AdministerFrame(this);
                    this.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(this, "登陆失败");
                    this.txt_userName.setText("");
                    this.pf_pwd.setText("");
                    this.txt_userName.requestFocus();
                }
            } else {
                if (userName.equals(bean.getStuName()) ||
                    userPwd.equals(bean.getStuPwd())) {
                    //弹出学生登陆界面(该界面只有功能选择,而没有实质文本内容)
                    TypeFrame typea = new TypeFrame(this);
                    this.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(this, "登陆失败");
                    this.txt_userName.setText("");
                    this.pf_pwd.setText("");
                    this.txt_userName.requestFocus();

                }
            }

        }

    }

    //退出按钮的单击事件
    public void btn_exit_actionPerformed(ActionEvent e) {
        System.exit(0);

    }
}


class LoginFrame_btn_exit_actionAdapter implements ActionListener {
    private LoginFrame adaptee;
    private ActionEvent e;
    LoginFrame_btn_exit_actionAdapter(LoginFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        adaptee.btn_exit_actionPerformed(e);
    }
}


class LoginFrame_btn_login_actionAdapter implements ActionListener {
    private LoginFrame adaptee;
    private ActionEvent e;
    LoginFrame_btn_login_actionAdapter(LoginFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        adaptee.btn_login_actionPerformed(e);
    }
}

⌨️ 快捷键说明

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