Java登录界面简单设计

  1 package cn.com.view;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.SystemColor;
  6 import java.awt.Toolkit;
  7 import java.awt.event.ActionEvent;
  8 import java.awt.event.MouseEvent;
  9 
 10 import javax.swing.ImageIcon;
 11 import javax.swing.JButton;
 12 import javax.swing.JFrame;
 13 import javax.swing.JLabel;
 14 import javax.swing.JOptionPane;
 15 import javax.swing.JPanel;
 16 import javax.swing.JPasswordField;
 17 import javax.swing.JProgressBar;
 18 import javax.swing.JTextField;
 19 
 20 import sun.awt.HorizBagLayout;
 21 
 22 
 23 import cn.com.beans.UserInfoBean;
 24 import cn.com.beans.UserInfoViewBean;
 25 import cn.com.daos.UserInfoDAO;
 26 import cn.com.listeners.LoginFrame_btnLogin_ActionListener;
 27 import cn.com.listeners.LoginFrame_btnReset_ActionListener;
 28 import cn.com.listeners.LoginFrame_lblRegist_MouseListener;
 29 
 30 
 31 public class LoginFrame extends JFrame {
 32     JPanel pnlMain;
 33     JButton btnLogin;
 34     JButton btnReset;
 35     JLabel lblUserName;
 36     JLabel lblUserPwd;
 37     JLabel lblTitle;
 38     JLabel lblRegist;
 39     JTextField txtUserName;
 40     JPasswordField pwdUserPwd;
 41     JProgressBar progressBar;
 42     JLabel lblLoginStat;
 43     
 44     public LoginFrame() {
 45         pnlMain = new JPanel(null);
 46         btnLogin = new JButton("登录");
 47         btnReset = new JButton("重置");
 48         lblUserName = new JLabel("用户名:");
 49         lblUserPwd = new JLabel("密  码:");
 50         lblTitle = new JLabel("用户登录");
 51         lblRegist = new JLabel("注册");
 52         txtUserName = new JTextField();
 53         pwdUserPwd = new JPasswordField();
 54         progressBar = new JProgressBar();
 55         progressBar.setIndeterminate(false);
 56         progressBar.setOrientation(JProgressBar.HORIZONTAL);
 57         progressBar.setForeground(new Color(0, 255, 0));
 58         progressBar.setBounds(286, 348, 146, 14);
 59         init();
 60     }
 61 
 62     private void init() {
 63         // TODO Auto-generated method stub
 64         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 65         setIconImage(Toolkit.getDefaultToolkit().getImage("image\\e.jpg"));
 66         this.setBounds(100, 100, 559, 351);
 67         this.setLocationRelativeTo(null);
 68         this.setResizable(false);
 69         this.getContentPane().setLayout(null);
 70         getContentPane().setBackground(new Color(255, 255, 255));
 71         
 72         
 73         JLabel label = new JLabel("人事管理系统用户登录");
 74         label.setFont(new Font("楷体", Font.PLAIN, 40));
 75         label.setBackground(Color.WHITE);
 76         label.setBounds(79, 10, 400, 43);
 77         this.getContentPane().add(label);
 78         
 79         JLabel lblNewLabel = new JLabel("图片");
 80         lblNewLabel.setBounds(10, 63, 250, 250);
 81         this.getContentPane().add(lblNewLabel);
 82         lblNewLabel.setIcon(new ImageIcon("D:\\Workspaces\\PMSys\\image\\login.jpg"));
 83         
 84         JLabel lblUserName = new JLabel("用户名:");
 85         lblUserName.setFont(new Font("华文楷体", Font.BOLD, 15));
 86         lblUserName.setBounds(270, 100, 65, 28);
 87         this.getContentPane().add(lblUserName);
 88         
 89         JLabel lblUserPwd = new JLabel("密   码:");
 90         lblUserPwd.setFont(new Font("华文楷体", Font.BOLD, 15));
 91         lblUserPwd.setBounds(270, 163, 65, 25);
 92         this.getContentPane().add(lblUserPwd);
 93         
 94         txtUserName = new JTextField();
 95         txtUserName.setText("请输入用户名");
 96         txtUserName.setBounds(345, 97, 173, 31);
 97         txtUserName.setBackground(new Color(255, 255, 204));
 98         this.getContentPane().add(txtUserName);
 99         txtUserName.setColumns(10);
100         
101         pwdUserPwd = new JPasswordField();
102         pwdUserPwd.setFont(new Font("宋体", Font.BOLD, 20));
103         pwdUserPwd.setBounds(345, 160, 173, 28);
104         pwdUserPwd.setBackground(new Color(255, 255, 204));
105         this.getContentPane().add(pwdUserPwd);
106         
107         JButton btnLogin = new JButton("登录");
108         
109         btnLogin.setBounds(272, 217, 93, 43);
110         this.getContentPane().add(btnLogin);
111         
112         JButton btnReset = new JButton("重置");
113         
114         btnReset.setBounds(427, 217, 93, 43);
115         this.getContentPane().add(btnReset);
116         
117         lblLoginStat = new JLabel("登录中>>>>");
118         lblLoginStat.setForeground(Color.RED);
119         lblLoginStat.setFont(new Font("华文楷体", Font.BOLD, 15));
120         lblLoginStat.setBounds(270,270,246,14);
121         this.getContentPane().add(lblLoginStat);
122         lblLoginStat.setVisible(false);
123         
124         progressBar.setBounds(272,284,246,14);
125         progressBar.setIndeterminate(true);
126         progressBar.setVisible(false);
127         this.getContentPane().add(progressBar);
128         this.setVisible(true);
129         
130         //添加监听事件
131         btnLogin.addActionListener(new LoginFrame_btnLogin_ActionListener(this));
132         btnReset.addActionListener(new LoginFrame_btnReset_ActionListener(this));
133         lblRegist.addMouseListener(new LoginFrame_lblRegist_MouseListener(this));
134     }
135 
136     public void btnLogin_actionPerformed(ActionEvent e){
137         // TODO Auto-generated method stub
138         
139         progressBar.setVisible(true);
140         lblLoginStat.setVisible(true);
141         UserInfoDAO dao = new UserInfoDAO();
142         String userName = this.txtUserName.getText();
143         String userPwd = new String(this.pwdUserPwd.getPassword());
144         if(dao.validateByNameAndPwd(userName, userPwd)) {
145             UserInfoViewBean ab = dao.getUserListInfoByUserName(userName);
146             new MainFrame(ab);
147             this.dispose();
148         }else {
149 //            JOptionPane.showMessageDialog(null, "用户名或密码错误","错误",JOptionPane.ERROR_MESSAGE);
150             int n = JOptionPane.showConfirmDialog(null, "密码错误,是否重新登录?", "提示",JOptionPane.YES_NO_OPTION);
151             if(n == 0){
152                 //progressBar.setIndeterminate(false);
153                 progressBar.setVisible(false);
154                 lblLoginStat.setVisible(false);
155             }else{
156                 System.exit(0);
157             }
158 
159         }
160     }
161         //重置监听事件
162     public void btnReset_actionPerformed(ActionEvent e) {
163         // TODO Auto-generated method stub
164         this.txtUserName.setText("");
165         this.pwdUserPwd.setText("");
166     }
167 
168     public void lblRegist_mouseClicked(MouseEvent e) {
169     } 
170     
171 }