如何在inputDialog中键入时隐藏文本


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("");
    }
}

所以,我想从输入对话框中隐藏文本,setEchoChar不起作用

不能直接在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()可以在键入时更改显示给用户的字符。

为了从文本框中隐藏文本,您需要使用类似的JPassword

passwordField = new JPasswordField(10);
passwordField.setActionCommand(OK);

以下是的链接以了解更多详细信息

http://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html

这将在您输入文本时隐藏文本。

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

相关内容

  • 没有找到相关文章