package com.library.view; |
import java.awt.GridLayout; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import javax.swing.ImageIcon; |
import javax.swing.JButton; |
import javax.swing.JComboBox; |
import javax.swing.JFrame; |
import javax.swing.JLabel; |
import javax.swing.JOptionPane; |
import javax.swing.JPanel; |
import javax.swing.JTextField; |
import com.library.biz.impl.UsersBiz; |
import com.library.biz.impl.UsersBizImpl; |
import com.libraty.entity.Users; |
public class UserLoginView extends JFrame{ |
/** |
* UserLoginView自创建的 |
*/ |
private static final long serialVersionUID = 1L; |
|
/** 窗体内的组件 */ |
private JPanel panel_main= null ; /**主面板 */ |
private JPanel panel_left= null ; /** 左面板 */ |
private JPanel panel_right= null ; /**右面板*/ |
|
/**存放图片的标签 */ |
private JLabel lb_img= null ; /**存放图片*/ |
|
private JLabel lb_username= null ; /**用户姓名*/ |
private JLabel lb_password= null ; /**用户密码*/ |
private JLabel lb_user_test= null ; /**用户类型*/ |
|
private JTextField tf_username= null ; /**用户姓名输入框*/ |
private JTextField tf_password= null ; /**用户密码输入框*/ |
private JComboBox<String> cb_type= null ; /**用户类型下拉框*/ |
|
private JButton btn_login= null ; /** 登陆按钮 */ |
private JButton btn_register= null ; /** 注册按钮 */ |
|
private UsersBiz usersBiz; /** 属性依赖业务层*/ |
|
|
private void init() /** 定义一个方法将以上属性初始化 */ |
{ |
panel_main= new JPanel(); |
panel_left= new JPanel(); |
panel_right= new JPanel( new GridLayout( 4 , 2 )); /** 网格布局 */ |
|
lb_img= new JLabel( new ImageIcon(ClassLoader.getSystemResource( "images/logo.png" ))); |
lb_username= new JLabel( "用户姓名:" ,JLabel.CENTER); /** 用户姓名文本显示 */ |
lb_password= new JLabel( "密 码:" ,JLabel.CENTER); /** 用户密码文本显示 */ |
lb_user_test= new JLabel( "用户类型:" ,JLabel.CENTER); /** 用户类型文本显示 */ |
|
tf_username= new JTextField( 10 ); /** 用户姓名输入框 */ |
tf_password= new JTextField( 10 ); /** 用户密码输入框 */ |
cb_type= new JComboBox<String>( new String[]{ "普通用户" , "管理员" }); /** 用户类型下拉框 */ |
|
btn_login= new JButton( "登陆" ); /** 登陆按钮 */ |
btn_register= new JButton( "注册" ); /** 注册按钮 */ |
|
|
panel_left.add(lb_img); /** 把组件放入窗体中,或者说放入布局中 */ |
|
panel_right.add(lb_username); |
panel_right.add(tf_username); |
panel_right.add(lb_password); |
panel_right.add(tf_password); |
panel_right.add(lb_user_test); |
panel_right.add(cb_type); |
panel_right.add(btn_login); |
panel_right.add(btn_register); |
|
panel_main.add(panel_left); |
panel_main.add(panel_right); |
|
this .getContentPane().add(panel_main); /** 实例化组件 */ |
|
this .setTitle( "用户登录" ); /** 窗体标题 */ |
this .setSize( 320 , 148 ); /** 设置窗体大小 */ |
this .setDefaultCloseOperation(EXIT_ON_CLOSE); /** 设置点击关闭程序退出 */ |
this .setResizable( false ); /** 不可放大 */ |
this .setLocationRelativeTo( null ); /** 居中显示 */ |
|
this .usersBiz= new UsersBizImpl(); /**对业务对象进行初始化 */ |
} |
|
public UserLoginView() |
{ |
init(); /** 调用初始化方法 */ |
this .setVisible( true ); |
registerListener(); /** 传感器时间间隔(只出现一个窗体) */ |
} |
|
private void registerListener() /** 给所有按钮注册侦听器 */ |
{ |
/** btn_register.addActionListener(new ActionListener() { |
public void actionPerformed(ActionEvent e) { |
new UserRegisterView(); |
} |
}); */ |
|
/**1.先给注册按钮注册侦听器*/ |
btn_register.addActionListener((ActionEvent e)-> |
{ |
new UserRegisterView(); |
}); |
/**2.给login注册按钮注册侦听事件*/ |
btn_login.addActionListener( new ActionListener() |
{ |
@Override |
public void actionPerformed(ActionEvent e) |
{ |
/** 登录的业务逻辑:1.先获取用户填写的用户名和密码 */ |
String user_name=tf_username.getText().trim(); |
String password=tf_password.getText().trim(); |
|
/**2.进行非空判断*/ |
if ( "" .equals(user_name)) |
{ |
JOptionPane.showMessageDialog(UserLoginView. this , "请输入用户名" ); |
return ; |
} |
if ( "" .equals(password)) |
{ |
JOptionPane.showMessageDialog(UserLoginView. this , "请输入密码" ); |
return ; |
} |
|
/**3.调用底层biz进行登录判断*/ |
Users user_info= new Users(user_name,password); |
Users query_user=usersBiz.login(user_info); |
|
/**4.对返回值一般是user对象进行判断,如果为空则弹出消息提示框,用户名或者密码输入错误,请重新输入,否则跳往用户操作的主界面*/ |
if (query_user!= null ) |
{ |
if (query_user.getUser_type()== 1 ) |
{ |
JOptionPane.showMessageDialog(UserLoginView. this , "恭喜" +query_user.getUser_name()+ "登录成功" ); |
new UserMainView(query_user); /** 普通用戶登錄之後的主界面 */ |
|
} else { |
JOptionPane.showMessageDialog(UserLoginView. this , "恭喜管理员" +query_user.getUser_name()+ "登录成功" ); |
new UserMainView(query_user); /** 管理員登錄之後的主界面 */ |
} |
} else { |
JOptionPane.showMessageDialog(UserLoginView. this , "用户名或者密码输入错误" ); /** 如果查询用户为空 */ |
} |
} |
}); |
} |
|
} |
初级程序员
by: 234432 发表于:2021-06-16 08:41:40 顶(0) | 踩(0) 回复
很不错
回复评论