在Applet中加入1个文本框,1个文本区,每次在文本框中输入文本,回车后将文本添加到文本区的最后一行。我的程序为什么加不到最后一行,用什么方法可以
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Ex7_01 extends Applet implements ActionListener 
{
TextField txt1;
TextArea txt2;
public void init() 
{
txt1 = new TextField("",50);
txt2 = new TextArea(50,50);
add(txt1);
add(txt2);
txt1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if ((e.getSource()==txt1))
{
txt2.setText(txt1.getText());
}
}
}

解决方案 »

  1.   

    贴全了,而且能用,在TextField中输入数据,在TextArea 中也能显示,就是不知道怎么在TextArea 最后一行输出
      

  2.   

    使用txt2.append(String text)方法!
    setText(String text)方法会重新设置字符串。
      

  3.   

    public void actionPerformed(ActionEvent e)

        if (e.getSource() == txt1)
        {
            if (txt1.getText().trim().length != 0)
            {
                txt2.append(txt1.getText());
            }
        }
    }
      

  4.   

    你不应该用textfield
    用JTextPane就可以实现你的效果,而且可以分不同的颜色来显示文字http://bbs.bc-cn.net/dispbbs.asp?boardid=12&replyid=241077&id=127125&page=1&skin=0&star=1
    帮你解决一切问题,要看哦.
      

  5.   

    txt2.append(txt1.getText() + "\n");