请教各位大侠。能否给断小的示例代码。
不胜感激!
你看一下吧,应该符合你的要求的。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class focus extends Frame {
public focus()
{
this.add(new myPanel());
this.setSize(200,100);
this.setVisible(true);
}
public static void main(String args[])
{
new focus();
}
}
class myPanel extends Panel
{
TextField tf = new TextField(10);
Label label = new Label("请输入:");
public myPanel()
{
this.add(label);
this.add(tf);
tf.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
//
System.out.println("focus lost!!"); //这就是lost后触发的一个事件了
}
});
tf.addTextListener(new TextListener()
{
public void textValueChanged(TextEvent e)
{
String inputString = ((TextField)e.getSource()).getText();
if(inputString.length() > 3) //当你输入的字符超过3个是就会弹出一个对话框,使focus失去
{
//jop.showConfirmDialog(, inputString, "hello", JOptionPane.YES_OPTION);
JOptionPane.showConfirmDialog(null, "你输入了:"+inputString);
}
}
});
}
}