java logout_Java Login/Logout Demo

1 importjavax.swing.*;2 importjavax.swing.border.EmptyBorder;3 importjava.awt.*;4 importjava.awt.event.*;5 importjava.util.*;6 7 8 /**9 * Title:        Login Panel10 * Description:  A simple yet complete login/logout panel with user callbacks11 *               for approving login attempts and getting notification of logouts.12 * Copyright:    Copyright (c) 200413 * Company:      Superliminal Software14 *@authorMelinda Green15 *@version1.016 */17 18 publicclassLoginPanelextendsJPanel {19 publicfinalstaticString20 LOG_IN="Login",21 LOG_OUT="Logout";22 protectedJButton logButt;23 publicJButton getLogButton() {returnlogButt; }24 privatefinalstaticintDEFAULT_PSWD_CHARS=10;25 privateJTextField nameField=newJTextField(DEFAULT_PSWD_CHARS);26 publicString getUserName() {returnnameField.getText(); }27 28 /**29 * override this method to return true if approved, false otherwise.30 * default is true.31 */32 publicbooleanapproveLogin(String uname, String pswd) {33 returntrue;34 }35 36 /**37 * override this method to learn about logout events.38 */39 publicvoidloggedOut(String uname) {40 }41 42 publicLoginPanel() {43 this(false);44 }45 46 publicLoginPanel(finalbooleanclearPasswords) {47 this(clearPasswords,true,null,null);48 }49 50 /**51 *@paramclearPasswords if true, clears password field on successful login.52 *@paraminitial_user optional default text to load into the 'user' type-in.53 *@paraminitial_password optional default text to load into the 'password' type-in.54 */55 publicLoginPanel(finalbooleanclearPasswords,finalbooleandisplayFailures, String initial_user, String initial_password) {56 finalJPasswordField pswdField=newJPasswordField(DEFAULT_PSWD_CHARS);57 logButt=newJButton(LOG_IN);58 KeyListener quickLogin=newKeyAdapter() {59 publicvoidkeyTyped(KeyEvent ke) {60 if(ke.getKeyChar()==KeyEvent.VK_ENTER) {61 logButt.doClick();62 logButt.requestFocus();63 }64 }65 };66 nameField.setText(initial_user);67 pswdField.setText(initial_password);68 logButt.setName(LOG_IN);69 nameField.addKeyListener(quickLogin);70 pswdField.addKeyListener(quickLogin);71 //create the grid72 JPanel grid=newJPanel(newGridLayout(2,2));73 grid.setBackground(newColor(255,255,255));74 grid.add(newJLabel("User Name"));75 grid.add(nameField);76 grid.add(newJLabel("Password"));77 grid.add(pswdField);78 79 //create login button row80 JPanel row=newJPanel();81 row.setBorder(newEmptyBorder(5,0,5,0));82 row.setOpaque(false);83 row.setLayout(newBoxLayout(row, BoxLayout.X_AXIS));84 row.add(logButt);85 logButt.setBackground(newColor(220,220,220));86 87 logButt.addActionListener(newActionListener() {88 publicvoidactionPerformed(ActionEvent ae) {89 if(logButt.getText().equals(LOG_IN)) {90 //seek login approval from derived class91 if(approveLogin(nameField.getText(),newString(pswdField.getPassword()))) {92 //note: must set logout text *before* clearing password93 //otherwise component dependancy handler will disable the94 //login button w/out password text before later setting logout text95 //this closes bug #233696 logButt.setText(LOG_OUT);97 if(clearPasswords)98 pswdField.setText(null);99 nameField.setEnabled(false);100 pswdField.setEnabled(false);101 fireLoginEvent(nameField.getText(),true);102 }103 else104 if(displayFailures)105 JOptionPane.showMessageDialog(LoginPanel.this,"Login Denied","Login Error", JOptionPane.ERROR_MESSAGE);106 }107 else{108 logButt.setText(LOG_IN);109 loggedOut(nameField.getText());110 nameField.setEnabled(true);111 pswdField.setEnabled(true);112 fireLoginEvent(nameField.getText(),false);113 }114 }115 });116 117 //implement component dependancies118 //new ComponentDependencyHandler(nameField, pswdField) {119 //public void dependencyNotification() {120 //String121 //logtext = logButt.getText(),122 //nametext = nameField.getText(),123 //pswdtext = String.copyValueOf(pswdField.getPassword());124 //boolean newstate = logtext.equalsIgnoreCase(LOG_OUT) ||125 //(nameField.getText() != null && nametext.length() > 0//has login text?126 //&& pswdtext.length() > 0);//has password text?127 //logButt.setEnabled(newstate);128 //}129 //};130 131 //construct final layout132 setLayout(newBoxLayout(this, BoxLayout.Y_AXIS));133 add(grid);134 add(row);135 }136 137 publicinterfaceLoginListener {138 voidloggedIn(String uname);139 voidloggedOut(String uname);140 }141 publicstaticclassLoginAdapterimplementsLoginListener {142 publicvoidloggedIn(String uname){}143 publicvoidloggedOut(String uname){}144 }145 privateVector loginListeners=newVector();146 publicvoidaddLoginListener(LoginListener ll) { loginListeners.add(ll); }147 publicvoidremoveLoginListener(LoginListener ll) { loginListeners.remove(ll); }148 protectedvoidfireLoginEvent(String uname,booleanin) {149 for(Enumeration e=loginListeners.elements(); e.hasMoreElements(); ) {150 LoginListener ll=(LoginListener)e.nextElement();151 if(in)152 ll.loggedIn(uname);153 else154 ll.loggedOut(uname);155 }156 }157 158 /**159 * simple example test program for LoginPanel class160 */161 publicstaticvoidmain(String[] args) {162 finalString NOT_LOGGED_IN="LoginPanel Test - Currently Logged Out";163 finalJFrame frame=newJFrame(NOT_LOGGED_IN);164 frame.getContentPane().add(newLoginPanel() {165 publicbooleanapproveLogin(String uname, String pswd) {166 //this is where to make the server call to approve or reject login attempt167 frame.setTitle("LoginPanel Test - Currently logged in as"+uname);168 returntrue;169 }170 publicvoidloggedOut(String uname) {171 frame.setTitle(NOT_LOGGED_IN);172 }173 });174 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);175 frame.pack();176 frame.setSize(500, frame.getHeight());177 frame.setVisible(true);178 }179 }180 181

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值