在员工管理的GUI练习中加入数据验证。也就是在添加对象进数组之前,先作数据合法性的验证,数据合法再作添加。 姓名:2个以上的字母或汉字 性别:必须是男或女 年龄:必须为数字 电话:13、15、18开

在员工管理的GUI练习中加入数据验证。也就是在添加对象进数组之前,先作数据合法性的验证,数据合法再作添加。

 

姓名:2个以上的字母或汉字

性别:必须是男或女

年龄:必须为数字

电话:131518开始的11位数字 或者  028-99823345 (区号是三位和四位的数字。电话为7-8位的数字)


员工验证
package work;

public class Staff {
    private String name;
    private int age;
    private String sex;
    private String tel;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

}

这里我写了一个架包需要鼠标右键build path一下,你就会看到奶牛瓶了

package work;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import component.MyButton;
import component.Mytest;

public class MainJFrame extends JFrame{
    private   Mytest workName = new  Mytest("姓名", 60, 40, this);
    private  Mytest workAge = new  Mytest("年龄", 60, 100, this);
    private  Mytest workSex = new  Mytest("性别", 60, 160, this);
    private  Mytest workTel = new  Mytest("电话", 60, 220, this);
  
    
    private MyButton addButton = new MyButton("添加", 40, 280, this);
    
    static int num = 0;
    public static Staff [] workers = new Staff[2];
    
    public MainJFrame(){
        this.setTitle("我的窗体");
        this.setLayout(null);
         MyButton showButton = new MyButton("显示", 140, 280, this);
         MyButton findButton = new MyButton("查找", 240, 280, this);
         
        /**添加*/
        addButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                addnum ();
            }
        });
        /**显示*/
        showButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shownum();
            }
        });
        /**查找*/
        findButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                findnum();
            }
        });
        
        this.setSize(400, 400);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
    
    }
    /**添加方法*/
    public void addnum (){
        
        String info = "";
        if(workName.getText().matches("[a-zA-Z\\u4e00-\\u9fa5]{2,}") == false){
            info += "姓名必须是2位以上的字母或汉字\n";
        }
        if(workAge.getText().matches("[0-9]{1,2}")== false){
            info += "年龄必须是数字\n";
        }
        if(workSex.getText().matches("(男|女)")== false){
            info += "性别必须是男或女\n";
        }
        if(workTel.getText().matches("1(3|5|8)[0-9]{9}|[0][0-9]{2,3}-[0-9]{8}")== false){
            info += "电话必须是13、15、18开始的11位数字 或者  028-99823345\n";
        }
        if (info != ""){
            JOptionPane.showMessageDialog(null, info);
            return;
        }
        Staff worker = new Staff();
        worker.setName(workName.getText());
        worker.setAge(Integer.parseInt(workAge.getText()));
        worker.setSex(workSex.getText());
        worker.setTel(workTel.getText());
        workers[num] = worker;
        num++;
        JOptionPane.showMessageDialog(null, "添加成功");
        if (num == workers.length){
            addButton.setEnabled(false);
        }
        workName.setText("");
        workAge.setText("");
        workSex.setText("");
        workTel.setText("");
    }
    /**显示方法*/
    public void shownum (){
        String s = "姓名     "+"年龄     "+"性别     "+"电话\n";
        for(int i = 0 ;i <num ;i++){
            s+= workers[i].getName()+ "         " + workers[i].getAge()+ "         " +
                    workers[i].getSex()+ "      " + workers[i].getTel()+"\n";
        }
        JOptionPane.showMessageDialog(null, s);
    }
    /**查找方法*/
    public void findnum(){
        int m = -1;
        
        for(int i = 0;i< num;i++){
            if((workName.getText()).equals(workers[i].getName())){
                    m=i;
            }
        }
        
        if( m ==-1){
            JOptionPane.showMessageDialog(null, "找不到此人");
        }else {
            workAge.setText(workers[m].getAge()+"");
            workSex.setText(workers[m].getSex());
            workTel.setText(workers[m].getTel());
        }

    }
    
    
    
    public static void main(String[] args) {
        MainJFrame meun = new MainJFrame();

    }

}


阅读更多

没有更多推荐了,返回首页