package com.hellojava.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.hellojava.database.dao.UserDao;
import com.hellojava.entity.User;
import com.hellojava.tools.Tools;
/**
* 登陆窗口
* @author Administrator
*
*/
public class LoginFrame extends JFrame implements ActionListener{
private JTextField userName_Field;
private JPasswordField userPwd_Field;
private JLabel userName_msg;
private JLabel userPwd_msg;
//登陆界面
public LoginFrame(){
this.setTitle("我的第一个窗口");
File fileImage =new File(this.getClass().getClassLoader().getResource("p1.jpg").getPath());
BufferedImage image=null;
try{
image=ImageIO.read(fileImage);
}catch(IOException e){
e.printStackTrace();
}
this.getContentPane().setLayout(null);
JLabel wel_label =new JLabel("欢迎使用");
wel_label.setBounds(190, 30, 180, 30);
wel_label.setFont(new Font("宋体",Font.BOLD,24));
this.getContentPane().add(wel_label);
JLabel userName_label=new JLabel("账号:");
userName_label.setBounds(100, 100, 80, 25);
this.getContentPane().add(userName_label);
userName_Field=new JTextField();
userName_Field.setBounds(180, 100, 180, 25);
this.getContentPane().add(userName_Field);
userName_msg=new JLabel();
userName_msg.setBounds(380, 100, 90, 25);
userName_msg.setBackground(Color.red);
this.getContentPane().add(userName_msg);
JLabel userPwd_label=new JLabel("密码:");
userPwd_label.setBounds(100, 150, 80, 25);
this.getContentPane().add(userPwd_label);
userPwd_Field=new JPasswordField();
userPwd_Field.setBounds(180, 150, 180, 25);
this.getContentPane().add(userPwd_Field);
userPwd_msg=new JLabel();
userPwd_msg.setBounds(380, 150, 90, 25);
userPwd_msg.setBackground(Color.red);
this.getContentPane().add(userPwd_msg);
JButton button_ok =new JButton("登陆");
button_ok.setBounds(120, 235, 80, 25);
this.getContentPane().add(button_ok);
JButton button_cancel=new JButton("取消");
button_cancel.setBounds(260, 235, 80, 25);
this.getContentPane().add(button_cancel);
button_ok.setActionCommand("ok");
button_cancel.setActionCommand("cancel");
button_ok.addActionListener(this);
button_cancel.addActionListener(this);
this.setIconImage(image);
this.setSize(500,360);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* 覆盖actionPerformed() 验证身份
*/
@Override
public void actionPerformed(ActionEvent e) {
String command=e.getActionCommand();
switch(command){
case "ok":
String name=userName_Field.getText();
String pwd=userPwd_Field.getText();
User us =new User();
us.setUserName(name);
us.setUserPwd(pwd);
UserDao usdao =new UserDao();
User us_check=usdao.loadUser(us);
if(Tools.check_null(name)){
userName_msg.setText(" 账号不能为空!");
userName_msg.setOpaque(true);
}else if(Tools.check_null(pwd)){
resetMsg(userName_msg);
userPwd_msg.setOpaque(true);
userPwd_msg.setText(" 密码不能为空!");
}
else if(us_check!=null){
Tools.currentUser=us_check;
new MainFrame();
this.dispose();//释放资源
}else{
resetMsg(userPwd_msg);
JOptionPane.showMessageDialog(null, "登陆失败!");
}
break;
case "cancel":
System.exit(0);
break;
}
}
/**
* 设置label标签显示
* @param j1
*/
public void resetMsg(JLabel j1){
j1.setOpaque(false);
j1.setText("");
}
}