哪里有错啊,题目要求是在文本域(tf)输入并按下回车键以后.将输入的内容附加到文本区(ta)
import java.awt.*;
import java.awt.event.*;class MyFrame extends Frame implements ActionListener
{
    TextField tf;
    TextArea ta;
    MyFrame()
    {
        TextField tf = new TextField(20);
        TextArea ta = new TextArea();
        add(tf,BorderLayout.NORTH);
        add(ta);        setTitle("响应事件");
        setSize(300,400);
       
        tf.addActionListener(this);
        addWindowListener(new Mywindow());
    }    public void actionPerformed(ActionEvent e)
    {
        String s=tf.getText();
        ta.setText(s);
    }
    
    private class Mywindow extends WindowAdapter
   {
       public void windowClosing(WindowEvent wevent)
       {
           System.exit(0);
       }
   }
}public class Test3
{
    public static void main(String[] args) {
        MyFrame s1 = new MyFrame();
        s1.setVisible(true);
    }
}

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;public class text8 extends Frame implements ActionListener
    {
    TextField f = new TextField(8);
    TextArea a = new TextArea(8,8);
    Panel p = new Panel();
    boolean w;
    public text8()
    {
    w = false;
    add(p);
    p.add(f);
    p.add(a);
    f.addActionListener(this);
    this.addWindowListener(new test());

    }
    public void actionPerformed(ActionEvent e)
    {
    a.setText(f.getText());

    }
    public class test extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
    {
    if(w)
    dispose();
    else
    System.exit(0);
    }
    }
    public static void main(String args[])
    {
    text8 t = new text8();
    t.setSize(300,200);
    t.show();
    }
    }
      

  2.   

    修改后的代码如下:import java.awt.*;
    import java.awt.event.*;class MyFrame extends Frame implements ActionListener
    {
        TextField tf = new TextField(20);
        TextArea ta = new TextArea();
        MyFrame()
        {
           // TextField tf = new TextField(20);
          //  TextArea ta = new TextArea();
            add(tf,BorderLayout.NORTH);
            add(ta);        setTitle("响应事件");
            setSize(300,400);
           
            tf.addActionListener(this);
            addWindowListener(new Mywindow());
        }    
        public void actionPerformed(ActionEvent e)
        {
            String s=tf.getText();        
            ta.setText(s);
        }
        
        private class Mywindow extends WindowAdapter
       {
           public void windowClosing(WindowEvent wevent)
           {
               System.exit(0);
           }
       }
    }public class Test3
    {
        public static void main(String[] args) {
            MyFrame s1 = new MyFrame();
            s1.setVisible(true);
        }
    }
      

  3.   

    这段代码会报空指针错误,
    import java.awt.*;
    import java.awt.event.*;class MyFrame extends Frame implements ActionListener
    {
        TextField tf;//这是你开始声名的两个对像
        TextArea ta;
        MyFrame()
        {
            TextField tf = new TextField(20);//这是给对像分配空间
            TextArea ta = new TextArea();
            add(tf,BorderLayout.NORTH);
            add(ta);        setTitle("响应事件");
            setSize(300,400);
           
            tf.addActionListener(this);
            addWindowListener(new Mywindow());
        }    public void actionPerformed(ActionEvent e)
        {
            String s=tf.getText();//----->tf.getText()为null 原因在上面
            ta.setText(s);
        }
        
        private class Mywindow extends WindowAdapter
       {
           public void windowClosing(WindowEvent wevent)
           {
               System.exit(0);
           }
       }
    }public class Test3
    {
        public static void main(String[] args) {
            MyFrame s1 = new MyFrame();
            s1.setVisible(true);
        }
    }
    TextField tf;
    TextArea ta;
    你先这样写了,下面又
    TextField tf = new TextField(20);//这是给对像分配空间
    TextArea ta = new TextArea();
    这就等于从新定义了两个对像tf和ta,与你开始定义的那两个没任何关系,上面两个对像还是NULLTextField tf = new TextField(20);//这是给对像分配空间
    TextArea ta = new TextArea();
    而你所填入的值保存在TextField tf = new TextField(20);这里了,
    你最后用的又是最开始定义的
    TextField tf;
    TextArea ta;
    所以会报空指针错误
    改正:
    TextField tf = new TextField(20);
    TextArea ta = new TextArea();
    把前面的类型去掉就OK了
    就是这个
    tf = new TextField(20);
    ta = new TextArea();不知道楼主看明白没有!!!
      

  4.   

    以下是报的错误:
    java.lang.NullPointerException
    at com.leeyufeng.MyFrame.actionPerformed(Test.java:26)
    at java.awt.TextField.processActionEvent(TextField.java:573)
    at java.awt.TextField.processEvent(TextField.java:541)
    at java.awt.Component.dispatchEventImpl(Component.java:3615)
    at java.awt.Component.dispatchEvent(Component.java:3477)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
    Java2D Direct3D usage disabled by J2D_D3D env以下是修改的内容:
    import java.awt.*;
    import java.awt.event.*;class MyFrame extends Frame implements ActionListener
    {
        TextField tf = new TextField(20);
        TextArea ta = new TextArea();
        MyFrame()
        {
            add(tf,BorderLayout.NORTH);
            add(ta);        setTitle("ACTION");
            setSize(300,400);
           
            tf.addActionListener(this);
            addWindowListener(new Mywindow());
        }    
        public void actionPerformed(ActionEvent e)
        {
            String s=tf.getText();        
            ta.setText(s);
        }
        
        private class Mywindow extends WindowAdapter
       {
           public void windowClosing(WindowEvent wevent)
           {
               System.exit(0);
           }
       }
    }public class Test
    {
        public static void main(String[] args) {
            MyFrame s1 = new MyFrame();
            s1.setVisible(true);
        }
    }
      

  5.   

    你先是这样定义了tf,ta
    TextField tf;
    TextArea ta;
    然后又
    TextField tf = new TextField(20);
    TextArea ta = new TextArea();
    执行到这里的时候的tf,ta将覆盖外层的全局变量,tf,ta只是在构造方法内才有值,在构造函数外的全局变量tf,ta的值还是null,所以在执行到actionPerformed(e)时会抛出nullException;
    你可以采用这两种方式:
    1。在定义全局变量的同时初始化;2。在构造方法里不要在定义变量了(去掉构造函数中tf,ta前的TextField,和TextArea),而是直接引用定义好的全局变量。