2014-04-09 26 views
0
if(e.getSource() == credLimit){ // event handling for user to change credit limit 
    String input = JOptionPane.showInputDialog(null, "Enter Password"); 
    input.setEchoChar('*'); <<-----here is what I have tried 
    if(input.equals(password)) { // password check for credit limit 
     double credLimitTotal = Double.parseDouble(changeCredLimit.getText()); 
     JOptionPane.showMessageDialog(null, currentCreditCard.changeCreditLimit(credLimitTotal)); 
     changeCredLimit.setText(""); 
    } 
} 

打字所以,我希望隱藏從使用inputdialog文本時隱藏文本,setEchoChar不起作用如何使用inputdialog

回答

-1

這將隱藏文本您輸入它。

<input type="password" name="txtPassword" placeholder="Enter Password"/> 
+0

這是HTML,而不是Java –

0

您不能直接在InputDialog中隱藏字符。您可以使用默認隱藏字符的JPasswordField來創建自己的Dialog。試試這個代碼:

Box box = Box.createHorizontalBox(); 

JLabel jl = new JLabel("Password: "); 
box.add(jl); 

JPasswordField jpf = new JPasswordField(24); 
box.add(jpf); 

int button = JOptionPane.showConfirmDialog(null, box, "Enter your password", JOptionPane.OK_CANCEL_OPTION); 

if (button == JOptionPane.OK_OPTION) { 
    char[] input = jpf.getPassword(); 
    if(input.equals(password)) { 
     ... 
    } 
} 

使用setEchoChar()改變打字時向用戶顯示的字符。

+0

它對你有幫助嗎? –