主题:急~!有关JAVA中的问题,
冰过晓寒轻
[专家分:0] 发布于 2008-07-04 07:57:00
在JAVA的JTextField中,
我已经输入a.setText("请输入用户名");
如果我要清空里面的文字,该怎么办
回复列表 (共3个回复)
沙发
happyboy2007 [专家分:3900] 发布于 2008-07-04 08:44:00
a.setText("");
板凳
冰过晓寒轻 [专家分:0] 发布于 2008-07-04 20:07:00
试过了,这样子不行呀,运行后还是和原来一样
3 楼
happyboy2007 [专家分:3900] 发布于 2008-07-10 13:10:00
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class FocusDemo extends JFrame implements FocusListener
{
private JTextField textField;
private JTextField tf_name = new JTextField();
public FocusDemo()
{
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
getContentPane().setLayout(null);
tf_name.setText("请输入用户名");
tf_name.addFocusListener(this);
tf_name.setBounds(32, 38, 204, 21);
getContentPane().add(tf_name);
textField = new JTextField();
textField.setBounds(32, 78, 204, 21);
getContentPane().add(textField);
}
public void focusGained(FocusEvent e)
{
tf_name.setText("");
}
public void focusLost(FocusEvent e)
{
if(tf_name.getText().trim().equals(""))
{
tf_name.setText("请输入用户名");
}
}
public static void main(String[] args)
{
new FocusDemo();
}
}
我来回复