怎么样判断JTextArea失去了焦点??
即,光标挪开了JTextArea,怎么判断,添加什么事件??

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    public class Testa extends Frame 
    {
    public static void main(String[] args)
    {
    Testa t=new Testa();
    t.show();
    }
    TextField t1;
    TextField t2;
    public Testa()
    {
    setSize(300,400);
    setLayout(new FlowLayout());

    t1=new TextField(10);
    t2=new TextField(10);

    add(t1);
    add(t2);

    actionDemo a=new actionDemo();
    t1.addFocusListener(a);
    } private class actionDemo extends FocusAdapter
    {
    public void focusGained(FocusEvent event)
    {
    t1.setText("获得焦点");
    }

    public void focusLost(FocusEvent event)
    {
    t1.setText("失去焦点");
    }
    }
    }
      

  2.   

    如果只是判断失去焦点,可以使用内部匿名类,比较清楚,如下:
    JTextArea txtArea = new JTextArea(10,10);
    //add listener
    txtArea.addFoucsListener(new FocusAdapter(){
        public void focusLost(FocusEvent event){
            //some processing code
        }
    });