login.java

来自「基于Java——SQL2005 的java课程设计 仓库管理系统。主要功能」· Java 代码 · 共 148 行

JAVA
148
字号
package Login;


import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.*;

import windows.mainFrame;
import sql.sql;
import sun.util.logging.resources.logging;
import model.TbUserlist;
public class Login extends JFrame {
	private JLabel userLabel;
	private JLabel passLabel;
	private JButton exit;
	private JButton login;
	private static TbUserlist user;
	private static String userName="";
	public Login() {
		setTitle("E时代仓库信息管理系统登录");
		final JPanel panel = new LoginPanel();
		panel.setLayout(null);
		getContentPane().add(panel);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//合适的位置
		setBounds((d.width-panel.getWidth())/2, (d.height/3*2-panel.getHeight())/2, panel.getWidth(), panel.getHeight());//居中显示
		userLabel = new JLabel();
		userLabel.setText("用户名:");
		userLabel.setFont(new Font("", Font.PLAIN, 18));//设置字体
		userLabel.setForeground(Color.RED);//设置颜色为红色
		//userLabel.setBounds(100, 135, 200, 20);
		userLabel.setBounds(80,80, 200, 20);
		//userLabel.setBounds(160, 100, 200, 20);
		panel.add(userLabel);
		//final JTextField userName = new JTextField();
		//userName.setBounds(90, 100, 200, 20);
		//panel.add(userName);
		
		final JComboBox userCombo = new JComboBox();
		userCombo.setFont(new Font("", Font.PLAIN, 16));
		//userCombo.setBounds(90, 100, 200, 20);
		userCombo.setBounds(160, 80, 200, 20);
		//userCombo.setEditable(true);
		List khInfo = sql.getUsers();//初始化用户下拉列表
		List<Item> items = new ArrayList<Item>();
		userCombo.removeAllItems();
		for (Iterator iter = khInfo.iterator(); iter.hasNext();) {
			List element = (List) iter.next();
			Item item = new Item();
			item.setId(element.get(0).toString().trim());
			item.setName(element.get(1).toString().trim());
			if (items.contains(item))
				continue;
			items.add(item);
			userCombo.addItem(item);
			}
		final TbUserlist usertemp = sql.getUser((Item)userCombo.getItemAt(0));//设置当前用户为第一个读入的用户
		userName=usertemp.getUsername();
		panel.add(userCombo);
		
		passLabel = new JLabel();
		passLabel.setText("密   码:");
		passLabel.setFont(new Font("", Font.PLAIN, 18));
		passLabel.setForeground(Color.RED);
		passLabel.setBounds(80, 130, 200, 20);
		panel.add(passLabel);
		
		final JPasswordField userPassword = new JPasswordField();
		userPassword.setFont(new Font("", Font.PLAIN, 16));
		//Font f = new Font("Arial",Font.PLAIN,28);
		//userPassword.setFont(f);
		userCombo.addActionListener(new ActionListener() {//用户下拉框监听
			public void actionPerformed(ActionEvent e) {
				Item selectedItem;
				if (!(userCombo.getSelectedItem() instanceof Item)) {
					return;
				}
				selectedItem = (Item) userCombo.getSelectedItem();
				TbUserlist user = sql.getUser(selectedItem);
				userName=user.getUsername();	
				userPassword.requestFocus();
				//JOptionPane.showMessageDialog(null, userName);
			}
			});
		userCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
					userPassword.requestFocus();
			}
		});//侦听键盘事件,选择用户后回车就激活按钮
		userPassword.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.getKeyCode() == 10)
					login.doClick();
			}
		});//侦听键盘事件,输完密码后回车就激活登录按钮
		userPassword.setBounds(160, 130, 200, 20);
		panel.add(userPassword);
		login = new JButton();
		login.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				user = sql.getUser(userName, userPassword.getText());
				if (user.getUsername() == null || user.getName() == null) {
					userPassword.setText(null);//登录成功后初始化各个控件
					userCombo.requestFocus();
					return;
				}
				setVisible(false);
				new mainFrame();
			}
		});
		login.setText("登录");
		login.setFont(new Font("", Font.PLAIN, 16));
		login.setBounds(170, 180, 80, 25);
		panel.add(login);
		exit = new JButton();
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				System.exit(0);
			}
		});
		exit.setText("退出");
		exit.setFont(new Font("", Font.PLAIN, 16));
		exit.setBounds(270, 180, 80, 25);
		//Icon icon = new ImageIcon("Img/login.jpg");
		//exit.setIcon(icon);
		//exit.setHorizontalTextPosition(exit.CENTER);
		//exit.setVerticalTextPosition(exit.CENTER);
		panel.add(exit);
		setVisible(true);
		setResizable(false);
		//setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);//窗体上的关闭按钮
	}
	public static TbUserlist getUser() {
		return user;
	}
	public static void setUser(TbUserlist user) {
		Login.user = user;
	}
	/*public static void main(String[] args) {
		new Login();
	}*/
	
}

⌨️ 快捷键说明

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