2012-01-16 36 views
40

JOptionPane可以用来从用户那里得到的字符串输入,但对我来说,我想在showInputDialog显示密码字段。的JOptionPane获取口令

我需要的方式是应该被屏蔽由用户给定的输入和返回值必须在char[]。我需要一个带有消息,密码字段和两个按钮的对话框。可以这样做吗?谢谢。

回答

60

是的,有可能使用JOptionPane.showOptionDialog()。是这样的:

JPanel panel = new JPanel(); 
JLabel label = new JLabel("Enter a password:"); 
JPasswordField pass = new JPasswordField(10); 
panel.add(label); 
panel.add(pass); 
String[] options = new String[]{"OK", "Cancel"}; 
int option = JOptionPane.showOptionDialog(null, panel, "The title", 
         JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, 
         null, options, options[1]); 
if(option == 0) // pressing OK button 
{ 
    char[] password = pass.getPassword(); 
    System.out.println("Your password is: " + new String(password)); 
} 
+4

JPasswordField中(10)不接受比10更长的密码。更好的做法是让这个更宽或使用下面的@Adamski这样的无参数构造函数。此外,确定应该是默认选项,因为很多用户在输入密码后都不会出现Enter键。如果取消是默认的,那么输入密码后按回车就会取消对话框。 – gb96 2012-12-21 04:24:46

+0

这是一个很好的例子,在JOptionPane中有一个带有JPasswordField的标签,在我的情况下它接受的密码长度超过了10个。 – 2013-07-05 06:57:38

+3

当对话框出现时,你如何给密码字段赋予焦点? – mjaggard 2015-05-05 15:43:34

4

您可以创建你自己的对话框,扩展的JDialog,然后你可以把任何你在它想要的。

32

的最简单的就是用JOptionPaneshowConfirmDialog方法和在给JPasswordField一个引用传递;例如

JPasswordField pf = new JPasswordField(); 
int okCxl = JOptionPane.showConfirmDialog(null, pf, "Enter Password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 

if (okCxl == JOptionPane.OK_OPTION) { 
    String password = new String(pf.getPassword()); 
    System.err.println("You entered: " + password); 
} 

编辑

下面是使用定制JPanelJPasswordField一起显示的消息的例子。每最近的评论,我也(匆匆)添加代码,以允许JPasswordField获得焦点时,首先显示的对话框。

public class PasswordPanel extends JPanel { 
    private final JPasswordField passwordField = new JPasswordField(12); 
    private boolean gainedFocusBefore; 

    /** 
    * "Hook" method that causes the JPasswordField to request focus the first time this method is called. 
    */ 
    void gainedFocus() { 
    if (!gainedFocusBefore) { 
     gainedFocusBefore = true; 
     passwordField.requestFocusInWindow(); 
    } 
    } 

    public PasswordPanel() { 
    super(new FlowLayout()); 

    add(new JLabel("Password: ")); 
    add(passwordField); 
    } 

    public char[] getPassword() { 
     return passwordField.getPassword(); 
    } 

    public static void main(String[] args) { 
     PasswordPanel pPnl = new PasswordPanel(); 
     JOptionPane op = new JOptionPane(pPnl, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 

     JDialog dlg = op.createDialog("Who Goes There?"); 

     // Wire up FocusListener to ensure JPasswordField is able to request focus when the dialog is first shown. 
     dlg.addWindowFocusListener(new WindowAdapter() { 
     @Override 
     public void windowGainedFocus(WindowEvent e) { 
      pPnl.gainedFocus(); 
     } 
     }); 

     if (op.getValue() != null && op.getValue().equals(JOptionPane.OK_OPTION)) { 
      String password = new String(pPnl.getPassword()); 
      System.err.println("You entered: " + password); 
     } 
    } 
} 
+0

正是我在找的东西。 BTW是有办法**设置邮件/体字符串**该对话框的? – InamTaj 2014-08-19 00:22:16

+0

创建包含JPasswordField和包含所需文本的JLabel的JPanel的最简单方法。 – Adamski 2014-08-20 13:49:22

+0

我知道,但我不想在应用程序正在运行时打开另一个框架。所以我想用**消息字符串**来使用你的解决方案。 – InamTaj 2014-08-21 19:59:58

3

这种对话看起来好多了,如果你这样做

dlg.setVisible(true); 

如果没有你看不到它。

而且

pPnl.gainedFocus(); 

应该比它的伟大工程的其他

pPnl.gainedFocus(); 

。感谢代码。为Swing节省了时间。

另外,如果你不想留在后台每次你打开它时运行的对话,你需要的东西关闭它像

dlg.dispatchEvent(new WindowEvent(dlg, WindowEvent.WINDOW_CLOSING)); 
dlg.dispose(); // else java VM will wait for dialog to be disposed of (forever)