【问题标题】:How to hide text when typing in inputDialog在inputDialog中输入时如何隐藏文本
【发布时间】:2014-04-09 14:32:08
【问题描述】:
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 不起作用

【问题讨论】:

    标签: java user-interface input dialog


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      这里是更多详细信息的链接

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

      【讨论】:

        【解决方案3】:

        这将隐藏您输入的文本。

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

        【讨论】:

        • 这是 HTML,不是 Java
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-21
        • 2014-10-29
        • 1970-01-01
        • 2015-06-30
        • 1970-01-01
        • 2013-07-07
        相关资源
        最近更新 更多