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("");注释掉就可以了