下面列出了javax.swing.JPasswordField#addKeyListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void keyListenerForFields(JPasswordField passwordField) {
KeyAdapter myAdapter = new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
infoLabel.setText(null);
markerLbl.setVisible(false);
super.keyTyped(e);
}
};
passwordField.addKeyListener(myAdapter);
}
private void initComponents(int columns) {
jpfPassword = new JPasswordField(columns);
GridBagConstraints gbc_jpfPassword = new GridBagConstraints();
gbc_jpfPassword.gridwidth = 1;
gbc_jpfPassword.gridheight = 1;
gbc_jpfPassword.gridx = 0;
gbc_jpfPassword.gridy = 0;
gbc_jpfPassword.insets = new Insets(0, 0, 1, 0);
gbc_jpfPassword.fill = GridBagConstraints.HORIZONTAL;
if (getMinPasswordQuality() >= 0) {
jpqmQuality = new JPasswordQualityMeter(getMinPasswordQuality());
} else {
jpqmQuality = new JPasswordQualityMeter();
}
jpqmQuality.setPreferredSize(new Dimension(0, 6));
initQualityBar();
GridBagConstraints gbc_jpbQuality = new GridBagConstraints();
gbc_jpbQuality.gridwidth = 1;
gbc_jpbQuality.gridheight = 1;
gbc_jpbQuality.gridx = 0;
gbc_jpbQuality.gridy = 1;
gbc_jpbQuality.insets = new Insets(0, 0, 0, 0);
gbc_jpbQuality.fill = GridBagConstraints.HORIZONTAL;
jpfPassword.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent evt) {
passwordChanged();
}
});
setLayout(new GridBagLayout());
add(jpfPassword, gbc_jpfPassword);
add(jpqmQuality, gbc_jpbQuality);
}
public void keyListenerForFields(JTextField textField, JPasswordField passwordField) {
KeyAdapter myAdapter = new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
infoLabel.setText(null);
super.keyTyped(e);
}
};
textField.addKeyListener(myAdapter);
passwordField.addKeyListener(myAdapter);
}
private Component getControls() {
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(40, 40, 10, 10));
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
panel.setLayout(gridbag);
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(2, 2, 12, 10);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
String applicationName = Session.getSession().getPrimaryModule().getDisplayName();
String announcement = Session.getSession().getApplication().getAnnouncementText();
String msg =
"<html><p>Please enter your " + applicationName + " username and password,<br>"
+ "or use the username 'guest' and password 'guest'<br/>to "
+ "have a look. Running tools is disabled for guests.</p>";
if (announcement != null) {
msg += announcement;
}
msg += "</html>";
JLabel infoLabel = new JLabel(msg);
panel.add(infoLabel, c);
c.gridy++;
c.gridwidth = 1;
if (previousAttemptFailed) {
c.gridwidth = 2;
JLabel failedLabel = new JLabel("Login failed, please check your username and password");
failedLabel.setFont(failedLabel.getFont().deriveFont(Font.ITALIC));
panel.add(failedLabel, c);
c.gridwidth = 1;
c.gridy++;
}
c.insets = new Insets(2, 2, 2, 10);
panel.add(new JLabel("Username"), c);
c.insets = new Insets(2, 2, 2, 2);
c.gridx++;
usernameField = new JTextField(24);
usernameField.addKeyListener(this);
panel.add(usernameField, c);
c.gridy++;
c.gridx = 0;
c.insets = new Insets(2, 2, 2, 10);
panel.add(new JLabel("Password"), c);
c.insets = new Insets(2, 2, 2, 2);
c.gridx++;
passwordField = new JPasswordField(24);
passwordField.addKeyListener(this);
panel.add(passwordField, c);
c.gridy++;
c.gridx = 1;
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(15, 2, 0, 2);
JPanel buttonPanel = new JPanel();
JButton loginButton = new JButton("Login");
loginButton.setActionCommand(LOGIN_ACTION);
loginButton.addActionListener(this);
loginButton.addKeyListener(this);
buttonPanel.add(loginButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand(CANCEL_ACTION);
cancelButton.addActionListener(this);
buttonPanel.add(cancelButton);
panel.add(buttonPanel, c);
return panel;
}
/**
* Prompt and return input.
*
* @param title the title of the dialog.
* @param description the dialog description.
* @param icon the icon to use.
* @param parent the parent to use.
* @return the user input.
*/
public String getPassword(String title, String description, Icon icon, Component parent) {
passwordField = new JPasswordField();
passwordField.setText(password);
TitlePanel titlePanel = new TitlePanel(title, description, icon, true);
// Construct main panel w/ layout.
final JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(titlePanel, BorderLayout.NORTH);
final JPanel passwordPanel = new JPanel(new GridBagLayout());
JLabel passwordLabel = new JLabel(Res.getString("label.enter.password") + ":");
passwordPanel.add(passwordLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
passwordPanel.add(passwordField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
//user should be able to close this dialog (with an option to save room's password)
passwordPanel.add(_savePasswordBox, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
ResourceUtils.resButton(_savePasswordBox, Res.getString("checkbox.save.password"));
final Object[] options = {Res.getString("ok"), Res.getString("cancel") , };
optionPane = new JOptionPane(passwordPanel, JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
mainPanel.add(optionPane, BorderLayout.CENTER);
// Lets make sure that the dialog is modal. Cannot risk people
// losing this dialog.
JOptionPane p = new JOptionPane();
dialog = p.createDialog(parent, title);
dialog.setModal(true);
dialog.pack();
dialog.setSize(width, height);
dialog.setContentPane(mainPanel);
dialog.setLocationRelativeTo(parent);
optionPane.addPropertyChangeListener(this);
// Add Key Listener to Send Field
passwordField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
optionPane.requestFocus();
}
else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
dialog.dispose();
}
}
});
passwordField.requestFocus();
dialog.setVisible(true);
return stringValue;
}