请教高手swing 中表单验证如何实现!
以前做web开发时,有js脚本验证,现在做swing却不知道如何是好了,
我在网看到有的说用Document相关方法,还有的说用InputVerifier相关方法.
想请教大家平时你们都是怎样验证的,谢谢了!

解决方案 »

  1.   

    swing编程里 在web中有Document.方法的 具体的方法可以去查,和在JS里写验证方法差不多
      

  2.   

    我以前做过的项目采用的是在JAVA代码中取值去验证,若不正确则用对话框显示出来.
    你说的方法也可以.
      

  3.   

    你可以在提交的时间中做验证,也可以在输入的时候做验证。你可以直接取得表单中的数据,比如文本输入框你可以用对象.getText()方法获得。获得后你直接判断就可以拉
      

  4.   

    把验证写在控件的事件里就行了
    比如JTextField的keyReleased() JButton的actionPerformed()
      

  5.   

    新建一个 Frame1 
    package text;import java.awt.BorderLayout;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.Rectangle;
    import java.awt.Font;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextPane;public class Frame1 extends JFrame 
    {
        public Frame1()
        {
            try {
                jbInit();
            } catch (Exception exception) 
            {
                exception.printStackTrace();
            }
        }
        
        public static void main(String[] args)
        {
        Frame1 ff=new Frame1();
      
          ff.setSize(500,500);
          ff.show();
        }
        private void jbInit() throws Exception {
            getContentPane().setLayout(null);
            jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
            jLabel1.setText("登陆");
            jLabel1.setBounds(new Rectangle(169, 15, 49, 28));
            jLabel3.setText("用户:");
            jLabel3.setBounds(new Rectangle(92, 63, 78, 30));
            jTextField1.setBounds(new Rectangle(169, 67, 152, 20));
            jTextField2.setBounds(new Rectangle(169, 128, 152, 20));
            jButton1.setBounds(new Rectangle(99, 217, 81, 23));
            jButton1.setText("确定");
            jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
            jButton2.setBounds(new Rectangle(244, 215, 81, 23));
            jButton2.setText("取消");
            this.getContentPane().add(jLabel1);
            this.getContentPane().add(jLabel3);
            this.getContentPane().add(jLabel2);
            this.getContentPane().add(jTextField1);
            this.getContentPane().add(jTextField2);
            this.getContentPane().add(jButton1);
            this.getContentPane().add(jButton2);
            jLabel2.setText("密码:");
            jLabel2.setBounds(new Rectangle(92, 124, 78, 30));
        }    JLabel jLabel1 = new JLabel();
        JLabel jLabel2 = new JLabel();
        JLabel jLabel3 = new JLabel();
        JTextField jTextField1 = new JTextField();
        JTextField jTextField2 = new JTextField();
        JButton jButton1 = new JButton();
        JButton jButton2 = new JButton();
        public void jButton1_actionPerformed(ActionEvent e)
        {
            String name=jTextField1.getText().toString();
            String  pwd=jTextField2.getText().toString();
            if(name.equals("")||pwd.equals(""))
            {
                JOptionPane.showMessageDialog(null,"不能为空!");
            }
            else
            {
            run rr=new run();
            int i= rr.check(name,pwd);
            if(i==1)
            {
              JOptionPane.showMessageDialog(null,"成功了");
            }else
             {
                JOptionPane.showMessageDialog(null,"数据库异常!");
             }
            }
        }}class Frame1_jButton1_actionAdapter implements ActionListener {
        private Frame1 adaptee;
        Frame1_jButton1_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }    public void actionPerformed(ActionEvent e) {
            adaptee.jButton1_actionPerformed(e);
        }
    }再来一个连接类文件package text;import java.sql.*;public class run
    {
        public  Connection show()
        {
            try 
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                return    DriverManager.getConnection("jdbc:odbc:test");  
            }
            catch (Exception ex) 
            {
                ex.printStackTrace();
                 return null;
            }
        }  
        public int check(String name,String pwd)
        {
            String str="insert into DBtest select '"+name+"','"+pwd+"'";
            try 
            {
               Connection conn=this.show();
               Statement st= conn.createStatement();
               return st.executeUpdate(str);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                return 9;
            }
        }
     public static void main(String[] args) {
         run rr=new run();
         int i= rr.check("chu2","9999");
         System.out.print(i);
     }
    }
      

  6.   

    我一般用Document来进行输入项目的过滤。比如实现一个只能输入指定类型数字的TextBox。
    先写下面这个类来OverWrite insertString这个方法。private static class NumDocument extends PlainDocument {
        public static final int UINT    = 0;
        public static final int UFLOAT  = 1;
        public static final int INT     = 2;
        public static final int FLOAT   = 3;    protected int   figure;  // 位数
        protected int   type;    // 类型    public NumDocument(int figure, int type) {
            this.figure = figure;
            this.type = type;
        }
        
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            if (str == null) {
                return;
            }
            
            if (figure < getLength()) {
                return;
            }
            
            if (figure < getLength() + str.length()) {
                return;
            }
            
            String  text = new String("");        
            for (int i = 0; i < str.length(); i++) {
                char    c = str.charAt(i);
                if (c >= '0' && c <= '9') {
                    text += c;
                }            
                if (type == INT || type == FLOAT) {
                    if (c == '-') {
                        text += c;
                    }
                }            
                if (type == FLOAT || type == UFLOAT) {
                    if (c == '.') {
                        text += c;
                    }
                }
            }
            
            super.insertString(offs, text, a);
        }
    }
    然后就是NumberField extends JTextField。
    在NumberField的构造函数中,加入:
         Document doc = new NumDocument(figure, type);
         setDocument(doc);

    这样一来,对NumberField输入框不管是输入文字还是粘贴文字过来,Document都能给它截下非法字符。不过,对于逻辑上的验证,最好在进行事件操作时另作处理。