📄 jxloginpanel.java
字号:
gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 1; gridBagConstraints.anchor = GridBagConstraints.WEST; gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new Insets(0, 0, 5, 0); loginPanel.add(namePanel, gridBagConstraints); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = GridBagConstraints.WEST; gridBagConstraints.insets = new Insets(0, 0, 11, 11); loginPanel.add(passwordLabel, gridBagConstraints); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 1; gridBagConstraints.anchor = GridBagConstraints.WEST; gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new Insets(0, 0, 11, 0); loginPanel.add(passwordField, gridBagConstraints); gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new Insets(0, 0, 0, 0); loginPanel.add(saveCB, gridBagConstraints); return loginPanel; } /** * Listener class to implement saving of passwords and usernames. * * */ class SaveListener implements LoginListener { public void loginFailed(LoginEvent source) { } public void loginSucceeded(LoginEvent source) { if (getSaveMode() == SaveMode.PASSWORD || getSaveMode() == SaveMode.BOTH) { savePassword(); } else if (getSaveMode() == SaveMode.USER_NAME) { userNameStore.addUserName(nameField.getText()); userNameStore.saveUserNames(); } } public void loginStarted(LoginEvent source) { } public void loginCanceled(LoginEvent source) { } } void savePassword() { if (passwordStore != null) { passwordStore.set(getUserName(),getLoginService().getServer(),getPassword()); } } /** * @return Returns the saveMode. */ public SaveMode getSaveMode() { return saveMode; } /** * The save mode indicates whether the "save" password is checked by default. This method * makes no difference if the passwordStore is null. * * @param saveMode The saveMode to set either SAVE_NONE, SAVE_PASSWORD or SAVE_USERNAME */ public void setSaveMode(SaveMode saveMode) { namePanel.removeAll(); this.saveMode = saveMode; switch (saveMode) { case USER_NAME: case PASSWORD: case BOTH: namePanel.add(nameCombo, BorderLayout.CENTER); saveCB.setVisible(!(passwordStore instanceof NullPasswordStore)); revalidate(); break; default: namePanel.add(nameField, BorderLayout.CENTER); saveCB.setSelected(false); saveCB.setVisible(false); revalidate(); } } /** * Sets the <strong>LoginService</strong> for this panel. * * @param service service */ public void setLoginService(LoginService service) { loginService = service; } /** * Gets the <strong>LoginService</strong> for this panel. * * @return service service */ public LoginService getLoginService() { return loginService; } /** * Sets the <strong>PasswordStore</strong> for this panel. * * @param store PasswordStore */ public void setPasswordStore(PasswordStore store) { passwordStore = store; } /** * Gets the <strong>PasswordStore</strong> for this panel. * * @return store PasswordStore */ public PasswordStore getPasswordStore() { return passwordStore; } /** * Sets the <strong>User name</strong> for this panel. * * @param username User name */ public void setUserName(String username) { if(saveMode == SaveMode.NONE) { nameField.setText(username); } else { nameCombo.setSelectedItem(username); } } /** * Gets the <strong>User name</strong> for this panel. * @return the user name */ public String getUserName() { if(saveMode != SaveMode.NONE) { return (String)nameCombo.getSelectedItem(); } else { return nameField.getText(); } } /** * Sets the <strong>Password</strong> for this panel. * * @param password Password */ public void setPassword(char[] password) { passwordField.setText(new String(password)); } /** * Gets the <strong>Password</strong> for this panel. * * @return password Password */ public char[] getPassword() { return passwordField.getPassword(); } /** * */ public void startLogin() { oldCursor = getCursor(); try {// loginPanel.progressIndicator.setIndeterminate(true);// loginPanel.cancelLogin.setEnabled(true); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); String name = getUserName(); char[] password = getPassword(); loginService.startAuthentication(name, password, null); if (saveMode != SaveMode.NONE && !userNameStore.containsUserName(name)) { userNameStore.addUserName(name); userNameStore.saveUserNames(); } if (saveCB.isSelected() && (saveMode == SaveMode.BOTH || saveMode == SaveMode.PASSWORD)) { passwordStore.set(name, loginService.getServer(), password); }// UserPermissions.getInstance().setRoles(service.getUserRoles()); } catch(IOException ioerr) {// loginPanel.loginProgress.setText(ioerr.getMessage());// finishedLogin(false); } finally { setCursor(oldCursor); } } public void cancelLogin() { loginService.cancelAuthentication(); setCursor(oldCursor); } /** * Simple login service that allows everybody to login. This is useful in demos and allows * us to avoid having to check for LoginService being null */ private static final class NullLoginService extends LoginService { public boolean authenticate(String name, char[] password, String server) throws IOException { return true; } } /** * Simple PasswordStore that does not remember passwords */ private static final class NullPasswordStore extends PasswordStore { private static final char[] EMPTY = new char[0]; public boolean set(String username, String server, char[] password) { //null op return false; } public char[] get(String username, String server) { return EMPTY; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -