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

📄 login.java

📁 简单的系统
💻 JAVA
字号:
package views;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import main.ManagerMain;
import main.TeacherMain;

public class Login extends JFrame implements ActionListener {
	/**
	 * 登录界面
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton button, button1, button2;

	private JTextField textField;

	private JPasswordField passwordField;

	private JComboBox comboBox;

	static Connection con = null;

	static String option;

	public Login() {

		super("登录界面");

		getContentPane().setLayout(null);

		JLabel label = new JLabel();
		label.setFont(new Font("宋体", Font.PLAIN, 16));
		label.setText("用户名:");
		label.setBounds(32, 43, 68, 36);
		getContentPane().add(label);

		JLabel label_1 = new JLabel();
		label_1.setFont(new Font("宋体", Font.PLAIN, 15));
		label_1.setText("密码:");
		label_1.setBounds(32, 85, 68, 40);
		getContentPane().add(label_1);

		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 15));
		textField.setBounds(106, 52, 119, 22);
		getContentPane().add(textField);

		passwordField = new JPasswordField();
		passwordField.setFont(new Font("宋体", Font.PLAIN, 15));
		passwordField.setBounds(106, 94, 119, 24);
		getContentPane().add(passwordField);

		JLabel label_2 = new JLabel();
		label_2.setFont(new Font("宋体", Font.PLAIN, 15));
		label_2.setText("选择身份:");
		label_2.setBounds(32, 131, 88, 39);
		getContentPane().add(label_2);

		comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 15));
		comboBox.addItem("教师");
		comboBox.addItem("管理员");
		comboBox.setBounds(106, 139, 119, 24);
		option = comboBox.getSelectedItem().toString();
		System.out.println(option);
		comboBox.addActionListener(new ActionListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				option = comboBox.getSelectedItem().toString();
				System.out.println(option);
			}
		});
		getContentPane().add(comboBox);

		button = new JButton();
		button.setFont(new Font("宋体", Font.PLAIN, 12));
		button.setText("登录");
		button.setBounds(22, 222, 62, 27);
		button.addActionListener(this);
		getContentPane().add(button);

		button1 = new JButton();
		button1.setFont(new Font("宋体", Font.PLAIN, 12));
		button1.setText("重填");
		button1.setBounds(119, 222, 58, 27);
		button1.addActionListener(this);
		getContentPane().add(button1);

		JLabel label_3 = new JLabel();
		label_3.setFont(new Font("宋体", Font.PLAIN, 15));
		label_3.setText("多媒体教室钥匙管理系统");
		label_3.setBounds(58, 0, 167, 46);
		getContentPane().add(label_3);

		button2 = new JButton();
		button2.setFont(new Font("宋体", Font.PLAIN, 12));
		button2.setText("退出");
		button2.setBounds(211, 222, 58, 27);
		button2.addActionListener(this);
		getContentPane().add(button2);
	}

	public void ShowFrame() {
		Dimension screen = getToolkit().getScreenSize(); // 封装组件,工具包中设置屏幕大小的方法
		int width = (int) ((screen.getWidth() - 300) / 2);
		int height = (int) ((screen.getHeight() - 300) / 2);// 设置对象宽 ,高
		this.setBounds(width, height, 300, 300);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		Login login = new Login();
		login.ShowFrame();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		boolean b = false;
		String s0, s1;
		s0 = this.textField.getText();
		s1 = this.passwordField.getText();
		System.out.println("你输入的用户名和密码:" + s0 + s1);
		if (e.getSource() == button) {
			if ("教师".equalsIgnoreCase(option)) {
				/* 验证是否符合通过查数据库 */
				ResultSet rs = null;
				Connection conn = null;
				try {
					try {
						Class.forName(
								"com.microsoft.jdbc.sqlserver.SQLServerDriver")
								.newInstance();
					} catch (InstantiationException e0) {
						// TODO Auto-generated catch block
						e0.printStackTrace();
					} catch (IllegalAccessException e0) {
						// TODO Auto-generated catch block
						e0.printStackTrace();
					}
					String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=KeyManager";
					String user = "sa";
					String password = "sa";
					conn = DriverManager.getConnection(url, user, password);

				} catch (SQLException e0) {
					e0.getStackTrace();
				} catch (ClassNotFoundException e0) {
					e0.getStackTrace();
				}
				String statement = "select *  from TeacherInfo where TeacherId = '"
						+ s0 + "'and  TeacherPwd ='" + s1+"'";
				System.out.println(statement);
				try {
					rs = conn.createStatement(
							ResultSet.TYPE_SCROLL_INSENSITIVE,
							ResultSet.CONCUR_UPDATABLE).executeQuery(statement);
					while (rs.next()) {
						b = true;
					}
				} catch (SQLException e0) {
					// TODO 自动生成 catch 块
					e0.printStackTrace();
				}
				if (b == true) {
					this.setVisible(false);
					TeacherMain teachermain = new TeacherMain();
					teachermain.setUserName(s0);
					teachermain.setUserPwd(s1);
					teachermain.ShowFrame();
				} else {
					JOptionPane.showMessageDialog(this, "用户名和密码输入错误");
					this.textField.setText("");
					this.passwordField.setText("");
				}
			} else if ("管理员".equalsIgnoreCase(option)) {
				/* 验证是否符合通过查数据库 */
				ResultSet rs = null;
				Connection conn = null;
				try {
					try {
						Class.forName(
								"com.microsoft.jdbc.sqlserver.SQLServerDriver")
								.newInstance();
					} catch (InstantiationException e0) {
						// TODO Auto-generated catch block
						e0.printStackTrace();
					} catch (IllegalAccessException e0) {
						// TODO Auto-generated catch block
						e0.printStackTrace();
					}
					String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=KeyManager";
					String user = "sa";
					String password = "sa";
					conn = DriverManager.getConnection(url, user, password);

				} catch (SQLException e0) {
					e0.getStackTrace();
					// conn = getJOConnection();
				} catch (ClassNotFoundException e0) {
					e0.getStackTrace();
					// conn = getJOConnection();
				}
				String statement = "select *  from UserInfo where UserId = '"
						+ s0 + "'and  UserPwd ='" + s1 + "'";
				System.out.println(statement);
				try {
					rs = conn.createStatement(
							ResultSet.TYPE_SCROLL_INSENSITIVE,
							ResultSet.CONCUR_UPDATABLE).executeQuery(statement);
					while (rs.next()) {
						b = true;
					}
				} catch (SQLException e0) {
					// TODO 自动生成 catch 块
					e0.printStackTrace();
				}
				if (b == true) {
					this.setVisible(false);
					ManagerMain managermain = new ManagerMain();
					managermain.setUserName(s0);
					managermain.setUserPwd(s1);
					managermain.ShowFrame();
				} else {
					JOptionPane.showMessageDialog(this, "用户名和密码输入错误");
					this.textField.setText("");
					this.passwordField.setText("");
				}

				System.out.println("管理员");

			}

		}
		if (e.getSource() == button1) {// 重填
			textField.setText("");
			passwordField.setText("");
		}
		if (e.getSource() == button2) {// 退出
			System.exit(0);
		}
	}
}

⌨️ 快捷键说明

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