txt6.addFocusListener(new FocusAdapter(){
  public void focusGained(FocusEvent e){
  txt6.setText("");}
  public void focusLost(FocusEvent e){
  final String str2=txt6.getText();
  final char ch2[]=str2.toCharArray();
  txt7.setText(ch2[3]);
                                              txt8.setText(ch2[2]);}
  });   txt6和txt7、txt8都是已经定义好的文本框,现在给txt6添加事件。
  txt7.setText(ch2[3]);这里面提示说找不到符号,而txt6.setText("");则正确的。搞不明白啊

解决方案 »

  1.   

    txt7必须是final吧
    你给的描述太模糊了
      

  2.   

    public void setText(String t)
      

  3.   

    不好意思,原完本如下:import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class sum7 {
     static JFrame frm=new JFrame("四位加法器"); static TextField txt1=new TextField("加数Q1",4);
    static TextField txt6=new TextField("加数Q2",4);
    static TextField txt2=new TextField("",1);
    static TextField txt3=new TextField("",1);
    static TextField txt4=new TextField("",1);
    static TextField txt5=new TextField("",1);
    static TextField txt7=new TextField("",1);
    static TextField txt8=new TextField("",1);
    static TextField txt9=new TextField("",1);
    static TextField txt10=new TextField("",1);
    static TextField txt11=new TextField("",1);
    static TextField txt12=new TextField("",1);
    static TextField txt13=new TextField("",1);
    static TextField txt14=new TextField("",1);
    static TextField txt15=new TextField("",1);
    static TextField txt16=new TextField("",1);
    static TextField txt17=new TextField("",1);
    static TextField txt18=new TextField("",1);
    static TextField txt19=new TextField("",1);
    static TextField txt20=new TextField("",1);
    static TextField txt21=new TextField("",1);
    static TextField txt22=new TextField("",1);
    static TextArea txtr=new TextArea(); public static void main(String args[]){
    frm.setSize(700,430);
    frm.setLayout(null);
            //frm.setOpaque(false);
    frm.setBackground(Color.gray);

    //以下设置文本框
    txt1.setBounds(40,40,60,30);
    txt6.setBounds(40,130,60,30);

    txt2.setBounds(200,40,60,30);
       txt2.setEditable(false);
    txt3.setBounds(260,40,60,30);
       txt3.setEditable(false);
    txt4.setBounds(320,40,60,30);
       txt4.setEditable(false);
    txt5.setBounds(380,40,60,30);
       txt5.setEditable(false);

    txt7.setBounds(200,130,60,30);
       txt7.setEditable(false);
    txt8.setBounds(260,130,60,30);
       txt8.setEditable(false);
    txt9.setBounds(320,130,60,30);
       txt9.setEditable(false);
    txt10.setBounds(380,130,60,30);
       txt10.setEditable(false);

    txt11.setBounds(40,220,60,30);
       txt11.setText("进位Ci");
       txt11.setEditable(false);
    txt12.setBounds(150,220,50,30);
       txt12.setEditable(false);
    txt13.setBounds(200,220,60,30);
       txt13.setEditable(false);
    txt14.setBounds(260,220,60,30);
       txt14.setEditable(false);
    txt15.setBounds(320,220,60,30);
       txt15.setEditable(false);
    txt16.setBounds(380,220,60,30);
       txt16.setText("C0=0");
       txt16.setEditable(false);

    txt17.setBounds(40,310,60,30);
       txt17.setEditable(false);
    txt18.setBounds(150,310,50,30);
       txt18.setEditable(false);
    txt19.setBounds(200,310,60,30);
       txt19.setEditable(false);
    txt20.setBounds(260,310,60,30);
       txt20.setEditable(false);
    txt21.setBounds(320,310,60,30);
       txt21.setEditable(false);
    txt22.setBounds(380,310,60,30);
       txt22.setEditable(false);
    txtr.setBounds(460,40,230,310);
       txtr.setEditable(false);

        //以下添加文本框
    frm.add(txt1);
    frm.add(txt6);
    Font font=new Font("楷体",Font.BOLD,16);
      txt1.setFont(font);
      txt6.setFont(font);
      final String str1=txt1.getText();
      final String str2=txt6.getText();
      final char ch1[]=str1.toCharArray();
      final char ch2[]=str2.toCharArray();
      
    frm.add(txt2);
         txt2.setFont(font);
         //txt2.setText(ch[3]);
    frm.add(txt3);
      txt3.setFont(font);
      //txt3.setText(ch[2]);
    frm.add(txt4);
      txt4.setFont(font);
    frm.add(txt5);
      txt5.setFont(font);
    frm.add(txt7);
      txt7.setFont(font);
    frm.add(txt8);
      txt8.setFont(font);
    frm.add(txt9);
      txt9.setFont(font);
    frm.add(txt10);
      txt10.setFont(font);
    frm.add(txt11);
      txt11.setFont(font);
    frm.add(txt12);
      txt12.setFont(font);
    frm.add(txt13);
      txt13.setFont(font);
    frm.add(txt14);
      txt14.setFont(font);
    frm.add(txt15);
      txt15.setFont(font);
    frm.add(txt16);
      txt16.setFont(font);
    frm.add(txt17);
      txt17.setFont(font);
    frm.add(txt18);
      txt18.setFont(font);
    frm.add(txt19);
      txt19.setFont(font);
    frm.add(txt20);
      txt20.setFont(font);
    frm.add(txt21);
      txt21.setFont(font);
    frm.add(txt22);
      txt22.setFont(font);
    frm.add(txtr);
    Font fon=new Font("楷体",Font.BOLD,20);
      txtr.setFont(fon);



    //以下为txt1事件 
    txt1.addFocusListener(new FocusAdapter(){
       public void focusGained(FocusEvent e){
       System.out.println("helloworld");
       txt1.setText("");
       }
       public void focusLost(FocusEvent e){         
       txt2.setText(txt1.getText());
       txt2.setText(ch1[3]);//出错,找不到符号
       txt3.setText(ch1[2]);//出错,找不到符号
       txt4.setText(ch1[1]);//出错,找不到符号
       txt5.setText(ch1[0]); //出错,找不到符号
      
       }
      });
      
     txt6.addFocusListener(new FocusAdapter(){
      public void focusGained(FocusEvent e){
      txt6.setText("");
      }
      public void focusLost(FocusEvent e){
      txt7.setText(ch2[3]);}  //出错,找不到符号
      });
      
      //文本域换行尝试  成功
    txtr.setText("计算过程如下:");
    txtr.append("\n");
    txtr.append("3㈩32∧12∨45");

    System.out.println(txt1.getText());
    frm.setResizable(false);
        frm.setVisible(true);
    }
       
     
    }