两个文本框的内容相乘写入第三个文本框,对其是否数字格式进行验证。如验证无效,清空对应文本框的内容。
我给JTextField注册了DocumentListener监听器。

解决方案 »

  1.   

    DocumentListener有另外的用处吧 约束只要继承并覆盖就可以了
    static class IntegerDocument extends PlainDocument {        public void insertString(int offs, String str, AttributeSet a)
                    throws BadLocationException {            if (str == null) {
                    return;
                }
                int length = 0;
                char[] upper = str.toCharArray();
                for (int i = 0; i < upper.length; i++) {
                    if (upper[i] >= '0' && upper[i] <= '9') {
                        upper[length++] = upper[i];
                    }
                }
                super.insertString(offs, new String(upper, 0, length), a);
            }
        }
      

  2.   

    监听JTextFiled的Document的事件, 对每次输入都验证, 进行相应的操作.
      

  3.   

    像lz这种需求 只要对结果框进行了约束就可以了 修改并测试过了static class IntegerDocument extends PlainDocument {        public void insertString(int offs, String str, AttributeSet a)
                    throws BadLocationException {            if (str == null) {
                    return;
                }
                int length = 0;
                char[] upper = str.toCharArray();
                for (int i = 0; i < upper.length; i++) {
                    if (upper[i] >= '0' && upper[i] <= '9') {
                        upper[length++] = upper[i];
                    }
                    if (upper[i] < '0' || upper[i] > '9') {
                        length = 0;
                        break;
                    }
                }
                super.insertString(offs, new String(upper, 0, length), a);
            }
        }
    粘贴 打印都可以限制
    比如复制12ab34 就不会有任何效果
      

  4.   

      
    我的源代码是这样的:    
        private class ResultListener
                implements DocumentListener {
            public void insertUpdate(DocumentEvent e) {
                getAnswer();
            }
            public void removeUpdate(DocumentEvent e) {
                getAnswer();
            }
            public void changedUpdate(DocumentEvent e) {
            }
        }  //获取单价和数量文本域的内容,进行求积计算,并写入总价文本域中
        public void getAnswer() {
            String nu=this.number.getText().trim();
            String pr=this.pric.getText().trim();
            if(!nu.equals("")&&!pr.equals("")){
                
                if(nu.matches("^\\d+$")&&pr.matches("^\\d+[\\.]?\\d{0,2}$")){
                    try {
                        int add1;
                        double add2,answer;
                        //获取第一个和第二个文本域的内容,并将其解析为数字类型变量
                        add1 = Integer.parseInt(nu);
                        add2 = Double.parseDouble(pr);
                        //求积
                        answer = add1 * add2;
                        DecimalFormat df2  = new DecimalFormat("###.00");
                        //将结果写入第三个文本域中
                        this.sumpric.setText(df2.format(answer));
                        //对于解析错误的情况,将抛出异常
                    }catch (NumberFormatException e) {
                        JOptionPane.showMessageDialog(this,"请输入数字格式!","提示!",JOptionPane.INFORMATION_MESSAGE);
                    }
                }else{
                    try {
                        if(!nu.matches("^\\d+$")){
                            JOptionPane.showMessageDialog(this,"请输入数字格式!","提示!",JOptionPane.INFORMATION_MESSAGE);
                            //执行下面这一语句总是抛出IllegalStateException异常
                            this.number.setText("0");
                        }
                        if(!pr.matches("^\\d+[\\.]?\\d{0,2}$")){
                            JOptionPane.showMessageDialog(this,"请输入数字格式!(最多两位小数)","提示!",JOptionPane.INFORMATION_MESSAGE);
                            //执行下面这一语句总是抛出IllegalStateException异常
                            this.pric.setText("0.00");
                        }
                    }catch (IllegalStateException e) {
                        System.out.println("已经捕捉到IllegalStateException异常");
                    }
                    this.sumpric.setText("0.00");
                }
            }
        }