在输入对话框中输入时如何隐藏文本

时间:2014-04-09 14:32:08

标签: java user-interface input dialog

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不起作用

3 个答案:

答案 0 :(得分: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()更改输入时显示给用户的字符。

答案 1 :(得分:0)

为了隐藏文本框中的文本,您需要像这样使用JPassword

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

以下是更多详情的链接

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

答案 2 :(得分:-1)

这会在您输入时隐藏文字。

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