www.pudn.com > Hamming.rar > Hamming.java


import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
public class Hamming extends Applet implements TextListener,ActionListener 
{ 
   Label L=new Label("Êý¾Ý£º"); 
   Button B=new Button("º£Ã÷Âë"); 
   int Bit[]={1,0,0,0,1,1,0,0,1,0,0}; 
   TextField t1=new TextField("1",1); 
   TextField t2=new TextField("0",1); 
   TextField t3=new TextField("1",1); 
   TextField t4=new TextField("1",1); 
   TextField t5=new TextField("0",1); 
   TextField t6=new TextField("0",1); 
   TextField t7=new TextField("1",1); 
   TextField T1=new TextField(1); 
   TextField T2=new TextField(1); 
   TextField T3=new TextField(1); 
   TextField T4=new TextField(1); 
   TextField T5=new TextField(1); 
   TextField T6=new TextField(1); 
   TextField T7=new TextField(1); 
   TextField T8=new TextField(1); 
   TextField T9=new TextField(1); 
   TextField T10=new TextField(1); 
   TextField T11=new TextField(1); 
   public void init() 
   { 
     setLayout(new GridLayout(2,12)); 
     add(L);add(t7);add(t6);add(t5);add(t4);add(t3);add(t2);add(t1); 
     add(new Label());add(new Label());add(new Label());add(new Label()); 
     add(B);add(T11);add(T10);add(T9);add(T8);add(T7);add(T6);add(T5);add(T4);add(T3); 
     add(T2);add(T1); 
     B.addActionListener(this); 
     t1.addTextListener(this); 
     t2.addTextListener(this); 
     t3.addTextListener(this); 
     t4.addTextListener(this); 
     t5.addTextListener(this); 
     t6.addTextListener(this); 
     t7.addTextListener(this); 
   } 
   public void textValueChanged(TextEvent e) 
   { 
     if(e.getSource()==t7) Bit[0]=Integer.parseInt(t7.getText()); 
     if(e.getSource()==t6) Bit[1]=Integer.parseInt(t6.getText()); 
     if(e.getSource()==t5) Bit[2]=Integer.parseInt(t5.getText()); 
     if(e.getSource()==t4) Bit[4]=Integer.parseInt(t4.getText()); 
     if(e.getSource()==t3) Bit[5]=Integer.parseInt(t3.getText()); 
     if(e.getSource()==t2) Bit[6]=Integer.parseInt(t2.getText()); 
     if(e.getSource()==t1) Bit[8]=Integer.parseInt(t1.getText()); 
   } 
   public void actionPerformed(ActionEvent e) 
   { 
     if(e.getSource()==B) 
     { 
       T1.setForeground(Color.red);T2.setForeground(Color.red); 
       T3.setForeground(Color.red);T4.setForeground(Color.red); 
       T5.setForeground(Color.red);T6.setForeground(Color.red); 
       T7.setForeground(Color.red);T8.setForeground(Color.red); 
       T9.setForeground(Color.red);T10.setForeground(Color.red); 
       T11.setForeground(Color.red); 
       T3.setText(Integer.toString(Bit[8])); 
       T5.setText(Integer.toString(Bit[6])); 
       T6.setText(Integer.toString(Bit[5])); 
       T7.setText(Integer.toString(Bit[4])); 
       T9.setText(Integer.toString(Bit[2])); 
       T10.setText(Integer.toString(Bit[1])); 
       T11.setText(Integer.toString(Bit[0])); 
       if((Bit[0]+Bit[2]+Bit[4]+Bit[6]+Bit[8])%2==0) 
         T1.setText("0"); 
       else 
         T1.setText("1"); 
       if((Bit[0]+Bit[1]+Bit[4]+Bit[5]+Bit[8])%2==0) 
         T2.setText("0"); 
       else 
         T2.setText("1"); 
       if((Bit[4]+Bit[5]+Bit[6])%2==0) 
         T4.setText("0"); 
       else 
         T4.setText("1"); 
       if((Bit[0]+Bit[1]+Bit[2])%2==0) 
         T8.setText("0"); 
       else 
         T8.setText("1"); 
     } 
   } 
}