• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
              1 import javax.swing.*;
              2 import javax.swing.border.EmptyBorder;
              3 import java.awt.*;
              4 import java.awt.event.*;
              5 import java.util.*;
              6 
              7 
              8 /**
              9  * Title:        Login Panel
             10  * Description:  A simple yet complete login/logout panel with user callbacks
             11  *               for approving login attempts and getting notification of logouts.
             12  * Copyright:    Copyright (c) 2004
             13  * Company:      Superliminal Software
             14  * @author Melinda Green
             15  * @version 1.0
             16  */
             17 
             18 public class LoginPanel extends JPanel {
             19     public final static String
             20         LOG_IN  = "Login",
             21         LOG_OUT = "Logout";
             22     protected JButton logButt;
             23     public JButton getLogButton() { return logButt; }
             24     private final static int DEFAULT_PSWD_CHARS = 10;
             25     private JTextField nameField = new JTextField(DEFAULT_PSWD_CHARS);
             26     public String getUserName() { return nameField.getText(); }
             27 
             28     /**
             29      * override this method to return true if approved, false otherwise.
             30      * default is true.
             31      */
             32     public boolean approveLogin(String uname, String pswd) {
             33         return true;
             34     }
             35 
             36     /**
             37      * override this method to learn about logout events.
             38      */
             39     public void loggedOut(String uname) {
             40     }
             41 
             42     public LoginPanel() {
             43         this(false);
             44     }
             45 
             46     public LoginPanel(final boolean clearPasswords) {
             47         this(clearPasswords, truenullnull);
             48     }
             49 
             50     /**
             51      * @param clearPasswords if true, clears password field on successful login.
             52      * @param initial_user optional default text to load into the 'user' type-in.
             53      * @param initial_password optional default text to load into the 'password' type-in.
             54      */
             55     public LoginPanel(final boolean clearPasswords, final boolean displayFailures, String initial_user, String initial_password) {
             56         final JPasswordField pswdField = new JPasswordField(DEFAULT_PSWD_CHARS);
             57         logButt = new JButton(LOG_IN);
             58         KeyListener quickLogin = new KeyAdapter() {
             59             public void keyTyped(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 grid
             72         JPanel grid = new JPanel(new GridLayout(22));
             73         grid.setBackground(new Color(255,255,255));
             74         grid.add(new JLabel("User Name"));
             75         grid.add(nameField);
             76         grid.add(new JLabel("Password"));
             77         grid.add(pswdField);
             78 
             79         // create login button row
             80         JPanel row = new JPanel();
             81         row.setBorder(new EmptyBorder(5050));
             82         row.setOpaque(false);
             83         row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
             84         row.add(logButt);
             85         logButt.setBackground(new Color(220,220,220));
             86 
             87         logButt.addActionListener(new ActionListener() {
             88             public void actionPerformed(ActionEvent ae) {
             89                 if(logButt.getText().equals(LOG_IN)) {
             90                     // seek login approval from derived class
             91                     if(approveLogin(nameField.getText(), new String(pswdField.getPassword()))) {
             92                         // note: must set logout text *before* clearing password
             93                         // otherwise component dependancy handler will disable the
             94                         // login button w/out password text before later setting logout text
             95                         // this closes bug #2336
             96                         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                     else
            104                         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 dependancies
            118        // new ComponentDependencyHandler(nameField, pswdField) {
            119          //   public void dependencyNotification() {
            120            //     String
            121              //       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 layout
            132         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            133         add(grid);
            134         add(row);
            135     }
            136 
            137     public interface LoginListener {
            138         void loggedIn(String uname);
            139         void loggedOut(String uname);
            140     }
            141     public static class LoginAdapter implements LoginListener {
            142         public void loggedIn(String uname){}
            143         public void loggedOut(String uname){}
            144     }
            145     private Vector loginListeners = new Vector();
            146     public void addLoginListener(LoginListener ll) { loginListeners.add(ll); }
            147     public void removeLoginListener(LoginListener ll) { loginListeners.remove(ll); }
            148     protected void fireLoginEvent(String uname, boolean in) {
            149         for(Enumeration e=loginListeners.elements(); e.hasMoreElements(); ) {
            150             LoginListener ll = (LoginListener)e.nextElement();
            151             if(in)
            152                 ll.loggedIn(uname);
            153             else
            154                 ll.loggedOut(uname);
            155         }
            156     }
            157 
            158     /**
            159      * simple example test program for LoginPanel class
            160      */
            161     public static void main(String[] args) {
            162         final String NOT_LOGGED_IN = "LoginPanel Test - Currently Logged Out";
            163         final JFrame frame = new JFrame(NOT_LOGGED_IN);
            164         frame.getContentPane().add(new LoginPanel() {
            165             public boolean approveLogin(String uname, String pswd) {
            166                 // this is where to make the server call to approve or reject login attempt
            167                 frame.setTitle("LoginPanel Test - Currently logged in as " + uname);
            168                 return true;
            169             }
            170             public void loggedOut(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 

            Posted on 2008-12-11 09:44 鄒敏 閱讀(1466) 評論(0)  編輯 收藏 引用
            久久婷婷色综合一区二区| 怡红院日本一道日本久久 | 狠狠色丁香久久婷婷综合图片| 狠狠88综合久久久久综合网| 久久精品国产2020| 伊人久久大香线蕉综合Av| 亚洲欧美国产精品专区久久| 久久中文字幕无码专区| 久久中文字幕视频、最近更新| 久久精品国产精品亚洲下载| 精品久久人人做人人爽综合 | 国产99精品久久| 色成年激情久久综合| 伊人久久免费视频| 久久久久国产日韩精品网站| 久久综合鬼色88久久精品综合自在自线噜噜 | 欧美日韩中文字幕久久久不卡| 久久精品国产99国产精品| 久久久久久av无码免费看大片| 亚洲精品成人久久久| 国产毛片欧美毛片久久久| 精品久久久久久无码专区不卡| 色综合久久精品中文字幕首页| 久久久久久久综合综合狠狠| 婷婷久久五月天| 99精品久久久久中文字幕| 久久精品二区| 久久精品国产99久久无毒不卡 | 狠狠色丁香婷婷综合久久来来去| 精品久久人人妻人人做精品 | 久久夜色精品国产亚洲| 人人狠狠综合久久亚洲高清| 久久亚洲春色中文字幕久久久 | 中文字幕乱码人妻无码久久| 精品久久久久久国产91| 日韩一区二区三区视频久久| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 7777精品伊人久久久大香线蕉| 久久99国产乱子伦精品免费| 久久亚洲欧洲国产综合| 91久久精一区二区三区大全|