这个样子吧
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class TestTextField {
/**
* @param args
*/
JFrame jf;
JTextField jt;
final String name="please input your name";
public TestTextField()
{
jt=new JTextField(name);
jt.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
if(jt.getText()==name||name.equals(jt.getText()))
jt.setText("");
}
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
if(jt.getText()==""||jt.getText().equals(""))
jt.setText(name);
}});
jf=new JFrame();
jf.setLayout(new FlowLayout());
jf.add(new JTextField(8));
jf.add(jt);
jf.setBounds(200,200,100,100);
jf.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestTextField();
}
}
触发输入框的MouseClicked事件,里面写 jtextField.setText("");这就就可以了
使用得到焦点和失去焦点的方法