Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 防止用户在计算器中使用某些字符_Java_User Interface_Calculator - Fatal编程技术网

Java 防止用户在计算器中使用某些字符

Java 防止用户在计算器中使用某些字符,java,user-interface,calculator,Java,User Interface,Calculator,我正在制作一个计算器,我想弹出一个窗口,显示一条消息,如果用户在文本字段中键入一个不是数字的字符,则只接受数字,所以这就是我所做的,根本不工作。 谢谢你的阅读 private void txtPantallaActionPerformed(java.awt.event.ActionEvent evt) { String invalids; invalids = "|@#¢∞¬÷“”≠´‚πø

我正在制作一个计算器,我想弹出一个窗口,显示一条消息,如果用户在文本字段中键入一个不是数字的字符,则只接受数字,所以这就是我所做的,根本不工作。 谢谢你的阅读

private void txtPantallaActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String invalids;
    invalids = "|@#¢∞¬÷“”≠´‚πø  ¥†®€æœå∫∂ƒ™¶§ ~–…„µ ß√©∑Ωqwertyuiopñlkjhgfdsazxcvbnm!$%&/=?¿*^QWERTYUIOPÑLKJHGFDSAZXCVBNM";
    for (int i = 0 ; i < invalids.length() ; i++)
    {
        if(txtPantalla.getText().equals (invalids.substring(i, i+1)))
        {
            JOptionPane.showMessageDialog(null,"Only works with numbers");
        }
    }


}
private void txtPantallaActionPerformed(java.awt.event.ActionEvent evt){
字符串无效;
残疾人士=“|@#∞¬÷“”≠´‚πø  ¥†®€æœå∫∂ƒ™¶§ ~–…„µ ß√©∑Ωqwertyuiopñlkjhgfdsazxcvnm!$%&/=?*^qwertyuiopñlkjhgfdsazxcvnm”;
for(int i=0;i
为了可读性和避免循环,您可以使用正则表达式:

String value = txtPantalla.getText();
if(Pattern.matches(".*[^0-9].*", value)) {
  // there are some invalid characters!
  JOptionPane.showMessageDialog(null, "Only works with numbers");
}

任何要求您硬编码的解决方案都可能是错误的。

为什么不直接按下按键,这样就可以很好地发送信息了。