Jframe实现身份证解析器

package idcheck;


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

//import java.io.File;
//import java.io.IOException; 
//import java.io.InputStreamReader; //将字节流转变成字符流
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher; 
import java.util.regex.Pattern;
public class IDFrame extends JFrame implements ActionListener{
 private ImageIcon background;
 static Container imagePanel;
      static JLabel lb1, lb2;
   static JTextArea jta;
         static JTextField tf1;
         static JButton bt1 ,bt2;
         static String id;
         static String year;
         static String month;
         static  String day;
         static int year1, month1, day1;
         static int daysPerMonth[] = {31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
         static int flag = 0;   
            public IDFrame() { 
             setTitle("欢迎来到身份证查询系统");
              JPanel p1 = new JPanel();
             lb1 = new JLabel("请输入您要查询的的身份证号码");
              tf1 = new JTextField(25);
              bt1 = new JButton("确定");
              bt2 = new JButton("清屏");
              lb2 = new JLabel("您查询的结果为:");
              jta = new JTextArea(0,17);
             Container mb = getContentPane();
              mb.add(lb1);
              mb.add(tf1);
              mb.add(bt1);
              mb.add(bt2);
              mb.add(lb2);
              mb.add(jta);
              mb.add(p1);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
              tf1.setBackground(Color.WHITE);
              bt1.addActionListener(this);
              bt2.addActionListener(this);   
             jta.setBackground(Color.WHITE);                    //设置文本区的背景颜色
              jta.setForeground(Color.black);                    //设置文本区内的文字颜色
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setSize (100,40);
               bt1.setBounds(50,20,50,40);
               bt2.setBounds(40,20,50,40);
               lb2.setBounds(0,0,0,0);
           //   setResizable(false);                        //设置成不可更改容器的外形
              setVisible(true);
          BufferedImage image=null;
          try {
           image=ImageIO.read(new File("D:\\Documents\\Pictures\\011.jpg"));
          } catch (IOException e) {
           e.printStackTrace();
          }
              background = new ImageIcon(image);//背景图片
              JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
              // 把标签的大小位置设置为图片刚好填充整个面板
              label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
              //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明      
              JPanel imagePanel = (JPanel)this.getContentPane();
              imagePanel.setOpaque(false);
              jta.setOpaque(false);
              //内容窗格默认的布局管理器为BorderLayout
              imagePanel.setLayout(new FlowLayout());

             this.getLayeredPane().setLayout(null);
              //把背景图片添加到分层窗格的最底层作为背景      
             this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));   
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             this.setSize(background.getIconWidth(),background.getIconHeight());
          }
               
  private static Map<Integer, String> city = new HashMap<Integer, String>();
        public static void st(){
         id= tf1.getText();
         String home = city.get(new Integer(id.substring(0, 2)));// 查询所属城市,如果查不到为null
         jta.append("所属地区:");
   if (home != null)
    jta.append(home+"\n");
   else
    
    jta.append("不存在"+"\n");
        }
        public static void insert() {
   city.put(11, "北京市");
   city.put(12, "天津市");
   city.put(13, "河北省");
   city.put(14, "山西省");
   city.put(15, "内蒙古自治区");
   city.put(21, "辽宁省");
   city.put(22, "吉林省");
   city.put(23, "黑龙江省");
   city.put(31, "上海省");
   city.put(32, "江苏省");
   city.put(33, "浙江省");
   city.put(34, "安徽省");
   city.put(35, "福建省");
   city.put(36, "江西省");
   city.put(37, "山东省");
   city.put(41, "河南省");
   city.put(42, "湖北省");
   city.put(43, "湖南省");
   city.put(44, "广东省");
   city.put(45, "广西壮族自治区");
   city.put(46, "海南省");
   city.put(50, "重庆市");
   city.put(51, "四川省");
   city.put(52, "贵州省");
   city.put(53, "云南省");
   city.put(54, "西藏自治区");
   city.put(61, "陕西省");
   city.put(62, "甘肃省");
   city.put(63, "青海省");
   city.put(64, "宁夏回族自治区");
   city.put(65, "新疆维吾尔自治区");
   city.put(71, "台湾省");
   city.put(81, "香港特别行政区");
   city.put(82, "澳门特别行政区");
  }
       
        public static void input(){ int i=0;
            id = tf1.getText();
           String regex=("(\\d{17}[0-9a-zA-Z])");
            boolean s=id.matches(regex);
           if(s==false){
            javax.swing.JOptionPane.showMessageDialog(null,"请输入正确的身份证号 是十八位的数字哦");
          
           }
           else{
         year=id.substring(6, 10);
           year1 = Integer.parseInt(year);
           if(year1<1900||year1>2015){ i=1;}
           month = id.substring(10, 12);
           month1 = Integer.parseInt(month);
           if(month1 > 12 ||month1 < 1){
           i=2;
           }
           day = id.substring(12, 14);
           day1 = Integer.parseInt(day);
          if(month1 == 2){
               if(day1 > 0 && day1 <29){
                   }
               else {
                i=3;  }  }    
           if(day1>31)
            {i=5;}
           if (i!=1&&i!=2&&i!=3&&i!=5){
            i=7;
           }
           if(i==7){display();insert();st();display1();}
           else{
            javax.swing.JOptionPane.showMessageDialog(null,"输入的身份证号码错误,请重新输入");
          
           }
  } }
public static void display(){
        int midresult = 0;
        int identifynum1 = 0;
        char identifynum2 = '0';
        year = id.substring(6, 10);
        year1 = Integer.parseInt(year);
        month = id.substring(10, 12);
        month1 = Integer.parseInt(month);
        day = id.substring(12, 14);
        day1 = Integer.parseInt(day);
        if( flag == 0){
            for(int i = 2; i <= 18; i++){
            int mi=1;
            for(int j = i-1; j > 0; j--){
            mi = mi * 2;
            }
            int k ;
            k =id.charAt(18 - i)-'0';
            midresult += k*(mi%11);
        }
        identifynum1 = midresult%11;
        switch(identifynum1){
                case 0: identifynum2 = '1';break;
                case 1: identifynum2 = '0';break;
                case 2: identifynum2 = 'x';break;
                case 3: identifynum2 = '9';break;
                case 4: identifynum2 = '8';break;
                case 5: identifynum2 = '7';break;
                case 6: identifynum2 = '6';break;
                case 7: identifynum2 = '5';break;
                case 8: identifynum2 = '4';break;
                case 9: identifynum2 = '3';break;
                case 10: identifynum2 = '2';break;
            }
        if(identifynum2 == (id.toLowerCase().charAt(17))){ //如何使用大小写转换函数,在此解!
            jta.append("\n"+"您输入的身份证号码正确"+"\n");
            if((id.charAt(16)-'0')%2 == 0){
                    jta.append("性别:"+"女"+"\n");
                }
            else if((id.charAt(16)-'0')%2 != 0){
                    jta.append("性别:"+"男"+"\n");
                }
            jta.append("亲出生的日期是:");
     Pattern birthDatePattern= Pattern.compile("\\d{6}(\\d{4})(\\d{2})(\\d{2}).*");
     Matcher birthDateMatcher = birthDatePattern.matcher(id);
     if(birthDateMatcher.find()){
      String Year= birthDateMatcher.group(1);
      String Month=birthDateMatcher.group(2);
      String date=birthDateMatcher.group(3);
      jta.append(Year+"年"+Month+"月"+date+"日"+"\n"); 
           
     }}}
        }
public static String display1(){
 int year1=Integer.parseInt(year);
   String sSX[] = { "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗" }; 
   int end = 3; 
 int x = (year1 - end) % 12; 
   String retValue = ""; 
   retValue = sSX[x]; 
 String sTG[] = { "癸", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "任" }; 
          String sDZ[] = { "亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌" };                
          int i = (year1 - 3) % 10; 
         int j = (year1 - 3) % 12;     
         String retValue1 = ""; 
          retValue1 = sTG[i] + sDZ[j];    
          jta.append("客官您是属"+ retValue+"的"+"\n"+"天干地支:"+ retValue1+"\n" );
  int month1=Integer.parseInt(month);
  int day1=Integer.parseInt(day);
  int iAge; 
      Calendar cal = Calendar.getInstance();
      int iCurrYear = cal.get(Calendar.YEAR); 
      iAge = iCurrYear-year1; 
      jta.append("年龄:"+iAge+"\n");
   String a="";
   if ((month1 == 1 && day1 >=20) || (month1 == 2 && day1<=18)) { 
    jta.append("窝是 水瓶座 哇咔咔") ;   return a="水瓶座";
      } else if ((month1 == 2 && day1 >= 19) || (month1 == 3 && day1 <= 20)) { 
       jta.append("我是 双鱼座 么么哒");  return a="双鱼座";
      } else if ((month1 == 3 && day1 > 20) || (month1 == 4 && day1 <= 19)) { 
       jta.append("白羊座 咩咩咩");  return a="双鱼座";
      } else if ((month1== 4 && day1 >= 20) || (month1 == 5 && day1 <= 20)) { 
       jta.append("金牛座 哞哞哞");  return a="双鱼座";
      } else if ((month1 == 5 && day1 >= 21) || (month1 == 6 && day1 <= 21)) { 
       jta.append("双子座  (*^__^*) 嘻嘻……");  return a="双鱼座";
      } else if ((month1 == 6 && day1 > 21) || (month1 == 7 && day1 <= 22)) { 
       jta.append("巨蟹座 我擦擦");  return a="双鱼座";
      } else if ((month1 == 7 && day1 > 22) || (month1 == 8 && day1 <= 22)) { 
       jta.append("狮子座 咬我呀");  return a="双鱼座";
      } else if ((month1 == 8 && day1 >= 23) || (month1 == 9 && day1 <= 22)) { 
       jta.append("处女座  好羞射");  return a="双鱼座";
      } else if ((month1 == 9 && day1 >= 23) || (month1 == 10 && day1 <= 23)) { 
       jta.append("天秤座 嘿嘿嘿");  return a="双鱼座";
      } else if ((month1 == 10 && day1 > 23) || (month1 == 11 && day1 <= 22)) { 
       jta.append("天蝎座 我有毒");  return a="双鱼座";
      } else if ((month1 == 11 && day1 > 22) || (month1 == 12 && day1 <= 21)) { 
       jta.append ("射手座 最花心");  return a="双鱼座";
      } else if ((month1 == 12 && day1 > 21) || (month1 == 1 && day1 <= 19)) { 
       jta.append("魔羯座  呃呃呃");  return a="双鱼座";
      }return a;
}
        public void actionPerformed(ActionEvent e) {
             if(e.getSource() == bt1||e.getSource() == jta){
                input();
               
                flag = 0;}    
             else if(e.getSource() == bt2){
                jta.setText("");
                tf1.setText("");}
         }  
       /*public static void amount()throws IOException{
        new IDFrame();
        BufferedReader reader=new BufferedReader(new FileReader("C:/temp/studentinfo9.csv"));
     BufferedWriter writer=new BufferedWriter(new FileWriter("C:/temp/studentinfo11.csv"));
     String line=null;
     try{
     while((line=reader.readLine())!=null){
      StringBuilder sb = new StringBuilder ( "" );
      String item[]=line.split(",");
      String name=item[item.length-2];
      String id= item[item.length-1];
      //System.out.println(name+","+id);
      //String idCard = id;
      String xingzuo=IDFrame.display1();
      sb=sb.append(name);
      sb=sb.append(",");
      sb=sb.append(id);
      sb=sb.append(",");
      sb=sb.append(xingzuo);
      sb=sb.append("\n");
      String str1=sb.toString();
      writer.write(str1);
      System.out.println(sb);
      sb=null;
     }
     }
     catch (Exception e){
       e.printStackTrace();
     }
     finally
     {
      reader.close();
      writer.close();
      //sb=null;
     }}*/
 public static void main(String args[]) throws IOException {
 
   new IDFrame();
  //new LoginJFrameTest();
 }
 }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值