大家帮我看看这段JAVA代码

来源:百度知道 编辑:UC知道 时间:2024/05/14 19:34:57
public class ActionDemo extends JFrame {
JTextField name;
JTextArea chat;
JTextField input;
JButton send;
JButton clear;

public ActionDemo()
{

JPanel p=new JPanel();
name=new JTextField(10);
p.add(new JLabel("主题:Write Once,Run Anywhere. "));
p.add(new Label("昵称"));
p.add(name);
chat=new JTextArea();
chat.setLineWrap(true);

JPanel p1=new JPanel();
send=new JButton("确定");
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str;
if(name!=null)
{

str=chat.getText()+name+">>"+input.getText();
chat.setText(str);
}
else
chat.setText(chat.getText()+"kk");
}
});
clear=new JButton("清除");
p1.add(send,BorderL

public void actionPerformed(ActionEvent e)
{
String str;
if(name!=null)
{

str=chat.getText()+name+">>"+input.getText();
chat.setText(str);
}
else
chat.setText(chat.getText()+"kk");
}
});
问题出在这里:
首先,你的if条件的对象不对 你想判断姓名是不是空. 那应该是name.getText(),name只是一个控件对象;
其次就是你的str了 里面加了name,而不是name.getText() 所以会输出name对象的堆栈内容了, str=name.getText()+">>"+input.getText(); 这样就行.chat那个没有必要.
最后就是你的else里 是加了kk 那个kk不作处理的话 如果程序不重新执行那个kk就会越加越多.