2012-01-16 37 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)