首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

TextField与TextArea解决思路

2012-03-17 
TextField与TextArea在TextField中输入内容按回车显示在TextArea中,可是我按回车显示不了,这是怎么回事啊?

TextField与TextArea
在TextField中输入内容按回车显示在TextArea中,可是我按回车显示不了,这是怎么回事啊?求大家指教!!




import java.awt.*;
import java.awt.event.*;

public class ChatClient extends Frame {

TextField tfTxt = new TextField();
TextArea taContent = new TextArea();

public static void main(String[] args) {
new ChatClient().launchFrame();

}

public void launchFrame() {
setLocation(400,300);
this.setSize(300,300);
add(tfTxt,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {


@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
tfTxt.addActionListener(new TFListener());
setVisible(true);

}

private class TFListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
taContent.setText(str );
taContent.setText("");
}

}

}


[解决办法]
把taContent.setText("");去掉试试

[解决办法]
taContent.setText(str );//你在这里已经为taContent设置了值,其值为tfTxt的值
taContent.setText("");//而在这里你又将taContent的设置成了"",这样就导致了taContent的值为""而不是taContent.setText(str )这里设置的值了
1楼说得对,把taContent.setText("");注释掉就可以了

热点排行