首页 > 代码库 > 2016.9.20小程序--1
2016.9.20小程序--1
在员工管理的GUI练习中加入数据验证。也就是在添加对象进数组之前,先作数据合法性的验证,数据合法再作添加。
姓名:2个以上的字母或汉字
性别:必须是男或女
年龄:必须为数字
电话:13、15、18开始的11位数字 或者 028-99823345
1、员工类
1 public class Staff { 2 private String name ; 3 private int age ; 4 private String sex ; 5 private String tel ; 6 7 8 9 public String getName() {10 return name;11 }12 public void setName(String name) {13 this.name = name;14 }15 public int getAge() {16 return age;17 }18 public void setAge(int age) {19 this.age = age;20 }21 public String getSex() {22 return sex;23 }24 public void setSex(String sex) {25 this.sex = sex;26 }27 public String getTel() {28 return tel;29 }30 public void setTel(String tel) {31 this.tel = tel;32 }33 34 }
2、窗体
1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.JFrame; 5 import javax.swing.JOptionPane; 6 7 import JLabelAndJButton.MyButton; 8 import JLabelAndJButton.MyTxt; 9 10 public class MainJFrame extends JFrame{ 11 private MyTxt workName = new MyTxt("姓名", 60, 40, this); 12 private MyTxt workAge = new MyTxt("年龄", 60, 100, this); 13 private MyTxt workSex = new MyTxt("性别", 60, 160, this); 14 private MyTxt workTel = new MyTxt("电话", 60, 220, this); 15 16 private MyButton addButton = new MyButton("添加", 40, 280, this); 17 18 static int num = 0; 19 public static Staff [] workers = new Staff[2]; 20 21 public MainJFrame(){ 22 this.setTitle("我的窗体"); 23 this.setLayout(null); 24 MyButton showButton = new MyButton("显示", 140, 280, this); 25 MyButton findButton = new MyButton("查找", 240, 280, this); 26 27 /**添加*/ 28 addButton.addActionListener(new ActionListener() { 29 30 @Override 31 public void actionPerformed(ActionEvent arg0) { 32 addnum (); 33 } 34 }); 35 /**显示*/ 36 showButton.addActionListener(new ActionListener() { 37 @Override 38 public void actionPerformed(ActionEvent e) { 39 shownum(); 40 } 41 }); 42 /**查找*/ 43 findButton.addActionListener(new ActionListener() { 44 @Override 45 public void actionPerformed(ActionEvent e) { 46 findnum(); 47 } 48 }); 49 50 this.setSize(400, 400); 51 this.setVisible(true); 52 this.setDefaultCloseOperation(3); 53 this.setLocationRelativeTo(null); 54 55 } 56 /**添加方法*/ 57 public void addnum (){ 58 59 String info = ""; 60 if(workName.getText().matches("[a-zA-Z\\u4e00-\\u9fa5]{2,}") == false){ 61 info += "姓名必须是2位以上的字母或汉字\n"; 62 } 63 if(workAge.getText().matches("[0-9]{1,2}")== false){ 64 info += "年龄必须是数字\n"; 65 } 66 if(workSex.getText().matches("(男|女)")== false){ 67 info += "性别必须是男或女\n"; 68 } 69 if(workTel.getText().matches("1(3|5|8)[0-9]{9}|[0][0-9]{2,3}-[0-9]{8}")== false){ 70 info += "电话必须是13、15、18开始的11位数字 或者 028-99823345\n"; 71 } 72 if (info != ""){ 73 JOptionPane.showMessageDialog(null, info); 74 return; 75 } 76 Staff worker = new Staff(); 77 worker.setName(workName.getText()); 78 worker.setAge(Integer.parseInt(workAge.getText())); 79 worker.setSex(workSex.getText()); 80 worker.setTel(workTel.getText()); 81 workers[num] = worker; 82 num++; 83 JOptionPane.showMessageDialog(null, "添加成功"); 84 if (num == workers.length){ 85 addButton.setEnabled(false); 86 } 87 workName.setText(""); 88 workAge.setText(""); 89 workSex.setText(""); 90 workTel.setText(""); 91 } 92 /**显示方法*/ 93 public void shownum (){ 94 String s = "姓名 "+"年龄 "+"性别 "+"电话\n"; 95 for(int i = 0 ;i <num ;i++){ 96 s+= workers[i].getName()+ " " + workers[i].getAge()+ " " + 97 workers[i].getSex()+ " " + workers[i].getTel()+"\n"; 98 } 99 JOptionPane.showMessageDialog(null, s);100 }101 /**查找方法*/102 public void findnum(){103 int m = -1;104 105 for(int i = 0;i< num;i++){106 if((workName.getText()).equals(workers[i].getName())){107 m=i;108 }109 }110 111 if( m ==-1){112 JOptionPane.showMessageDialog(null, "找不到此人");113 }else {114 workAge.setText(workers[m].getAge()+"");115 workSex.setText(workers[m].getSex());116 workTel.setText(workers[m].getTel());117 }118 119 }120 121 122 123 public static void main(String[] args) {124 MainJFrame meun = new MainJFrame();125 126 }127 128 }
2016.9.20小程序--1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。