import java.awt.*;
//import java.applet.*;
import java.awt.event.*;
import javax.swing.*;class IdentityCard 
{
public static void main(String args[]) 
{
frmMain f=new frmMain();
f.setSize(400,300);
f.show();
}
}
class frmMain extends JFrame implements ActionListener{

Label lblTitle, lblReIdens, lblSoIdens, lblBirthDay, lblSex;
Label lblMessage, lblIdResult, lblBirResult, lblSexResult;
final int v_List[] = {2,4,8,5,10,9,7,3,6,1,2,4,8,5,10,9,7};
Button workButton, resetButton;
int[] tailList = new int[17];
TextField txtIdentity = null;
Panel disPanel = null;
Label lblReset;
int num;

 frmMain(){
this.setFont(new Font("Georgia", Font.BOLD, 20));
this.setBackground(Color.LIGHT_GRAY);
this.setLayout(new GridLayout(1,1));

lblTitle = new Label("身份证号码15位到18位转换");
lblTitle.setBounds(70, 10, 260, 20);

lblReIdens = new Label("15位身份证号:");
lblReIdens.setBounds(10, 40, 140, 20);
txtIdentity = new TextField("", 100);
txtIdentity.setBounds(165, 38, 210, 22);

lblSoIdens = new Label("18位身份证号:");
lblSoIdens.setBounds(10, 70, 140, 20);
lblIdResult = new Label("");
lblIdResult.setBounds(160, 68, 210, 22);

lblBirthDay = new Label("出生日期:");
lblBirthDay.setBounds(53, 100, 95, 20);
lblBirResult = new Label("");
lblBirResult.setBounds(160, 98, 200, 22);

lblSex = new Label("性别:");
lblSex.setBounds(94, 130, 60, 20);
lblSexResult = new Label("");
lblSexResult.setBounds(160, 128, 50, 22);

lblMessage = new Label("",Label.CENTER);
lblMessage.setBounds(10, 160, 380, 22);
lblMessage.setForeground(Color.MAGENTA);

workButton = new Button("转换");
workButton.addActionListener(this);
workButton.setBounds(140, 190, 50, 22);
workButton.setBackground(Color.LIGHT_GRAY);

resetButton = new Button("重置");
resetButton.addActionListener(this);
resetButton.setBounds(205, 190, 50, 22);
resetButton.setBackground(Color.LIGHT_GRAY);

this.add(lblTitle);  this.add(lblReIdens);
this.add(txtIdentity); this.add(lblSoIdens);
this.add(lblBirthDay); this.add(lblSex);
this.add(lblIdResult); this.add(lblBirResult);
this.add(lblSexResult); this.add(lblMessage);
this.add(workButton); this.add(resetButton);
}

public void actionPerformed(ActionEvent e){
if(e.getSource() == workButton){
checkIdentity();
doConverte();
}else{
clearContent();
txtIdentity.setText("");
lblMessage.setText("请重新输入!");
}
}

//身份证号的验证处理
public void checkIdentity(){
boolean Flag = false;
num = txtIdentity.getText().trim().length();

if(num == 0){
//没有输入值的处理
clearContent();
lblMessage.setText("请输入身份证号!");
}else{
//输入字母的数字位时处理
try{
long i = Long.parseLong(txtIdentity.getText().trim());
Flag = true;
}catch(Exception e){  //NumberFormatException
Flag = false;
clearContent();
txtIdentity.selectAll();
lblMessage.setText("你输入的身份证号无效!");
}

//输入位数不是标准的位数时处理
if(Flag == true){
if(num != 15 && num != 18){
clearContent();
txtIdentity.selectAll();
lblMessage.setText("你输入的身份证位数是"+ num +",请重新输入!");
}
}
}
}

//清除画面中的输入内容
public void clearContent(){
lblIdResult.setText("");
lblBirResult.setText("");
lblSexResult.setText("");
txtIdentity.requestFocus();
}

//实际转换并显示结果
public void doConverte(){
int Sum = 0;
String T = null;

if(num == 15){
//显示18位身份证号
String identity = txtIdentity.getText().trim().substring(0,6) + "19" +
  txtIdentity.getText().trim().substring(6);
for(int i = 0; i < identity.length(); i++){
tailList[i] = Integer.parseInt(identity.substring(i, i + 1));
}

for(int i = 0, j = 16; i < 17; i++, j--){
Sum = Sum + tailList[j] * v_List[i];
}

int R = Sum % 11;
if(R == 0) T = "1"; if(R == 1) T = "0"; if(R == 2) T = "X";
if(R == 3) T = "9"; if(R == 4) T = "8"; if(R == 5) T = "7";
if(R == 6) T = "6"; if(R == 7) T = "5"; if(R == 8) T = "4";
if(R == 9) T = "3"; if(R == 10) T = "2";

String identityN = identity + T;
lblIdResult.setText(identityN);

//显示出生日期
String str = "19" + txtIdentity.getText().trim().substring(6, 8) + "年" +
       txtIdentity.getText().trim().substring(8, 10) + "月" +
       txtIdentity.getText().trim().substring(10, 12) + "日";
lblBirResult.setText(str);

//显示性别
int sexNum = identity.charAt(16);
if(sexNum % 2 == 0){
lblSexResult.setText("女");
}else{
lblSexResult.setText("男");
}
lblMessage.setText("转换成功!");
}

if(num == 18){
//显示出生日期
lblIdResult.setText(txtIdentity.getText());
//显示出生日期
String str = txtIdentity.getText().trim().substring(6, 10) + "年" +
 txtIdentity.getText().trim().substring(10, 12) + "月" +
 txtIdentity.getText().trim().substring(12, 14) + "日";
lblBirResult.setText(str);
//显示性别
int sexNum = txtIdentity.getText().trim().charAt(16);
if(sexNum % 2 == 0){
lblSexResult.setText("女");
}else{
lblSexResult.setText("男");
}
lblMessage.setText("你输入的身份证是18位的!");
}
}
}

解决方案 »

  1.   

    能运行啊..
    就是布局上有点问题.
    我把这行给注了
    this.setLayout(new GridLayout(1,1));
      

  2.   

    编译没有错运行出错下面是出错的地方!
    java.lang.Error: Do not use frmMain.setLayout() use frmMain.getContentPane().setLayout() instead
    at javax.swing.JFrame.createRootPaneException(Unknown Source)
    at javax.swing.JFrame.setLayout(Unknown Source)
    at frmMain.<init>(IdentityCard.java:31)
    at IdentityCard.main(IdentityCard.java:11)
    Exception in thread "main" 
      

  3.   

    系统不是已经明确提示了么?
    没有使用布局管理器,所以会报错。frmMain.getContentPane()使用的是默认的布局管理器