日期:2014-05-20  浏览次数:20831 次

程序有错 请高手指点
import java.awt.BorderLayout;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class IdCard extends JFrame implements ActionListener {

JTextField jt1 = new JTextField(18);
JButton jb1 = new JButton("确定");
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JLabel jl1 = new JLabel("身份证号码");
JLabel jl2 = new JLabel(" 第二代身份证号码:");
JLabel jl22 = new JLabel();
JLabel jl3 = new JLabel(" 所在省/直辖市:");
JLabel jl33 = new JLabel();
JLabel jl4 = new JLabel(" 所在市/直辖区:");
JLabel jl44 = new JLabel();
JLabel jl5 = new JLabel(" 所在县/区(旗):");
JLabel jl55 = new JLabel();
JLabel jl6 = new JLabel(" 性别:");
JLabel jl66 = new JLabel();
JLabel jl7 = new JLabel(" 出生日期:");
JLabel jl77 = new JLabel();

public IdCard() {
super("身份证验证");
setLocation(500, 500);
jp1.add(jl1);
jp1.add(jt1);
jp1.add(jb1);
jb1.addActionListener(this);
jp2.add(jl2);
setVisible(false);
jp2.add(jl22);
jp2.add(jl3);
jp2.add(jl33);
jp2.add(jl4);
jp2.add(jl44);
jp2.add(jl5);
jp2.add(jl55);
jp2.add(jl6);
jp2.add(jl66);
jp2.add(jl7);
jp2.add(jl77);
add(jp1, BorderLayout.NORTH);
add(jp2);
jp2.setLayout(new GridLayout(6, 6, 5, 15));
pack();
setVisible(true);
}

public static void main(String[] args) {
new IdCard();

}

@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();

if (jb1 == source) {
System.out.println("===========================");
String a = jt1.getText();
List list = new ArrayList();
list.add(a);
if ((a.trim().equals("")) || (a == null)) {
return;
} else {
jl22.setText(a);
}

int d = (Integer) list.get(16);
if (d == 1) {
jl66.setText("男");
} else if (d == 2) {
jl66.setText("女");
} else {
jl66.setText("Cann't found any information");
}

if (a != null) {
for (int i = 6; i < 14; i++) {
jl77.setText((String) list.get(i));
}
} else{
jl77.setText("Cann't found any information");
}
}

}

}









------解决方案--------------------
if ((a == null)||(a.trim().equals(""))) {
return;
} else {
jl22.setText(a);
}
------解决方案--------------------
楼主这程序我看要大改动了。。。
------解决方案--------------------
Java code

import java.awt.BorderLayout;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class IdCard extends JFrame implements ActionListener {

    JTextField jt1 = new JTextField(18);
    JButton jb1 = new JButton("确定");
    JPanel jp1 = new JPanel();
    JPanel jp2 = new JPanel();
    JLabel jl1 = new JLabel("身份证号码");
    JLabel jl2 = new JLabel(" 第二代身份证号码:");
    JLabel jl22 = new JLabel();
    JLabel jl3 = new JLabel(" 所在省/直辖市:");
    JLabel jl33 = new JLabel();
    JLabel jl4 = new JLabel(" 所在市/直辖区:");
    JLabel jl44 = new JLabel();
    JLabel jl5 = new JLabel(" 所在县/区(旗):");
    JLabel jl55 = new JLabel();
    JLabel jl6 = new JLabel(" 性别:");
    JLabel jl66 = new JLabel();
    JLabel jl7 = new JLabel(" 出生日期:");
    JLabel jl77 = new JLabel();

    public IdCard() {
        super("身份证验证");
        setLocation(500, 500);
        jp1.add(jl1);
        jp1.add(jt1);
        jp1.add(jb1);
        jb1.addActionListener(this);
        jp2.add(jl2);
        setVisible(false);
        jp2.add(jl22);
        jp2.add(jl3);
        jp2.add(jl33);
        jp2.add(jl4);
        jp2.add(jl44);
        jp2.add(jl5);
        jp2.add(jl55);
        jp2.add(jl6);
        jp2.add(jl66);
        jp2.add(jl7);
        jp2.add(jl77);
        add(jp1, BorderLayout.NORTH);
        add(jp2);
        jp2.setLayout(new GridLayout(6, 6, 5, 15));
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new IdCard();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (jb1 == e.getSource()){
            System.out.println("===========================");
            String a = jt1.getText();
            List<String> list = new ArrayList<String>();
            list.add(a);
            if (a == null || "".equals(a.trim())){
                return;
            } else {
                if(!a.matches("\\d{17}[0-9X]")){
                    jl22.setText("身份证有误,请重新输入");
                    return;
                }
                jl22.setText(a);
            }

            int d=Integer.parseInt(String.valueOf(list.get(0).charAt(16)));
            if (d == 1) {
                jl66.setText("男");
            } else if (d == 2) {
                jl66.setText("女");
            } else {
                jl66.setText("Cann't found any information");
            }

            if (a != null) {
                for (int i = 6; i < 14; i++) {
                    jl77.setText((String)list.get(0).substring(6,14));
                }
            } else {
                jl77.setText("Cann't found any information");
            }
        }

    }