java swing 教务管理系统

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemColor;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.tjcu.entity.UserInfo;
import com.tjcu.factory.DAOFactory;
import com.tjcu.util.GlobalUser;

public class Login extends JFrame {
	private final Action action = new SwingAction();
	private JTextField textField;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JPasswordField passwordField;
	private JRadioButton rdbtnNewRadioButton = null;
	private JRadioButton rdbtnNewRadioButton_1 = null;
	private JRadioButton rdbtnNewRadioButton_2 = null;
	public JTextField captchaTextField = new JTextField();//输入验证码框
	public String captchaText;

	public Login() {
		JLabel captchaLabel = new JLabel();
		captchaTextField.setColumns(15);
		captchaText = CaptchaGenerator.generateCaptchaText(4); // 生成4位验证码
		BufferedImage captchaImage = CaptchaGenerator.createCaptchaImage(captchaText, 100, 30);
		captchaLabel.setIcon(new ImageIcon(captchaImage));
		setResizable(true);
		getContentPane().setBackground(SystemColor.menu);
		setSize(new Dimension(800, 600));
		setTitle("信管教务管理系统");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("菜单");
		mnFile.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		menuBar.add(mnFile);
		JMenuItem mntmNewMenuItem = new JMenuItem("退出");
		mntmNewMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mntmNewMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_STOP, 0));
		mnFile.add(mntmNewMenuItem);

		JMenu mnHelp = new JMenu("帮助");
		mnHelp.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		menuBar.add(mnHelp);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("关于");
		mntmNewMenuItem_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mntmNewMenuItem_1actionPerformed(e);
			}
		});
		mnHelp.add(mntmNewMenuItem_1);

		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/images/login1.jpg")));

		JLabel lblNewLabel_1 = new JLabel("账号");
		JLabel lblNewLabel_2 = new JLabel("密码");
	    JLabel lblNewLabel_3 = new JLabel("验证码");

		textField = new JTextField();
		textField.setColumns(0);

		rdbtnNewRadioButton = new JRadioButton("学生");
		rdbtnNewRadioButton.setSelected(true);
		buttonGroup.add(rdbtnNewRadioButton);

		rdbtnNewRadioButton_1 = new JRadioButton("教师");
		buttonGroup.add(rdbtnNewRadioButton_1);

		rdbtnNewRadioButton_2 = new JRadioButton("教务人员");
		buttonGroup.add(rdbtnNewRadioButton_2);

		JButton btnNewButton = new JButton("登录");
		btnNewButton.setDoubleBuffered(true);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				btnNewButtonactionPerformed(event);
			}
		});

		JButton zhuceButton = new JButton("注册");
		zhuceButton.setDoubleBuffered(true);
		zhuceButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e2) {
				zhuceNewButtonactionPerformed(e2);
			}
		});

		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		passwordField = new JPasswordField();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
						.createSequentialGroup().addGap(70)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
										.addGap(220)//验证码靠左70
										.addComponent(captchaLabel, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
										.addContainerGap(18, Short.MAX_VALUE))
								.addGroup(groupLayout.createSequentialGroup()
										.addGap(41)//填写框靠左70
										.addContainerGap(18, Short.MAX_VALUE))
								.addGroup(groupLayout.createSequentialGroup().addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
										.addGap(8).addComponent(passwordField, 166, 166, 166))
								.addGroup(groupLayout.createSequentialGroup().addComponent(rdbtnNewRadioButton).addGap(18)
										.addComponent(rdbtnNewRadioButton_1).addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(rdbtnNewRadioButton_2))
								.addGroup(groupLayout.createSequentialGroup().addComponent(lblNewLabel_1).addGap(18)
										.addComponent(textField, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup().addComponent(lblNewLabel_3).addGap(7)
										.addComponent(captchaTextField, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)))
						.addContainerGap(18, Short.MAX_VALUE))
				.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 341, Short.MAX_VALUE)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup().addContainerGap(82, Short.MAX_VALUE)
						.addComponent(btnNewButton).addGap(27).addComponent(zhuceButton).addGap(27).addComponent(btnNewButton_1).addGap(118)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup()
				.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE).addGap(18)
				.addGroup(groupLayout
						.createParallelGroup(Alignment.LEADING).addComponent(lblNewLabel_1).addComponent(
						textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(ComponentPlacement.RELATED)
				.addGroup(groupLayout
						.createParallelGroup(Alignment.LEADING).addComponent(lblNewLabel_3).addComponent(
								captchaTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(captchaLabel, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(ComponentPlacement.RELATED)
				.addGroup(groupLayout
						.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2).addComponent(passwordField,
								GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(rdbtnNewRadioButton_2)
						.addComponent(rdbtnNewRadioButton_1).addComponent(rdbtnNewRadioButton))
				.addGap(18).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton_1)
						.addComponent(zhuceButton).addComponent(btnNewButton))
				.addContainerGap(40, Short.MAX_VALUE)));
		getContentPane().setLayout(groupLayout);
		addTrayIcon();
	}

	private void addTrayIcon() {
		// Auto-generated method stub
		SystemTray st = SystemTray.getSystemTray();
		if (SystemTray.isSupported()) {
			URL imgurl = this.getClass().getClassLoader().getResource("images/login_main.jpg");
			Image img = Toolkit.getDefaultToolkit().createImage(imgurl);
			PopupMenu popup = new PopupMenu();
			MenuItem item = new MenuItem("退出程序");
			item.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// Auto-generated method stub
					System.exit(0);
				}
			});
			popup.add(item);
			TrayIcon icon = new TrayIcon(img, "信管教务管理系统", popup);
			icon.addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						UserInfo u = GlobalUser.LOGIN_USER;
						if (u == null) {
							Login.start();
						} else if (u.getGrade() == 0) {
							AdminFrame.start();
						} else if (u.getGrade() == 1) {
							TeacherFrame.start();
						} else if (u.getGrade() == 2) {
							StudentFrame.start();
						}

					}

				}
			});
			icon.setImageAutoSize(true);
			try {
				st.add(icon);
			} catch (AWTException e1) {
				e1.printStackTrace();
			}
		}

	}

	protected void mntmNewMenuItem_1actionPerformed(ActionEvent e) {

		About.start();
	}

	protected void zhuceNewButtonactionPerformed(ActionEvent e2){
		register add=new register(this,"用户注册",true);
		userinfo ad=new userinfo();
	}
	protected void btnNewButtonactionPerformed(ActionEvent event) {
		String name = textField.getText();// 获得用户名
		String pass = String.valueOf(passwordField.getPassword());
		UserInfo user = null;
		// 未输入用户名
		String userInputCaptcha = captchaTextField.getText();

		if (userInputCaptcha.equalsIgnoreCase(captchaText)) {

		} else {
			JOptionPane.showMessageDialog(this, "验证码错误!", "消息", 2);
			return;
		}

		if (name.equals("")) {
			JOptionPane.showMessageDialog(this, "用户名不允许为空!", "提示", 2);
			return;
		}

		try {
			user = DAOFactory.getIUserDAOInstance().findById(name);
			// System.out.println(user);
			if (user != null) {
				if (user.getUserPass() != null && user.getUserPass().equals(pass)) {

					GlobalUser.LOGIN_USER = user; // 记录当前用户

					// 进入主界面
					if (rdbtnNewRadioButton.isSelected()) {
						if (user.getGrade() != 2) {
							JOptionPane.showMessageDialog(this, "该用户不是学生!", "消息", 2);
							return;
						}
						// 学生界面
						StudentFrame.start();
					} else if (rdbtnNewRadioButton_1.isSelected()) {
						if (user.getGrade() != 1) {
							JOptionPane.showMessageDialog(this, "该用户不是老师!", "消息", 2);
							return;
						}
						// 教师界面
						TeacherFrame.start();
					} else if (rdbtnNewRadioButton_2.isSelected()) {
						if (user.getGrade() != 0) {
							JOptionPane.showMessageDialog(this, "该用户不是教务人员!", "消息", 2);
							return;
						}
						// 教务人员界面
						AdminFrame.start();
					} else {
						JOptionPane.showMessageDialog(this, "别急!权限没选呢");
						return;
					}

					this.dispose();
				} else {
					JOptionPane.showMessageDialog(this, "用户名或密码错误!", "消息", 2);
					return;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (user == null) {
			JOptionPane.showMessageDialog(this, "用户名或密码错误!", "消息", 0);
			return;
		}
	}

	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}

		public void actionPerformed(ActionEvent e) {
		}
	}

	public static void start() {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Login().setVisible(true);
			}
		});
	}

	public static void main(String[] args) {
		new Login().setVisible(true);
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值