http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/InputVerifier.html
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
// This program demonstrates the use of the Swing InputVerifier class.
// It creates two text fields; the first of the text fields expects the
// string "pass" as input, and will allow focus to advance out of it
// only after that string is typed in by the user. public class VerifierTest extends JFrame {
public VerifierTest() {
JTextField tf1 = new JTextField ("Type \"pass\" here");
getContentPane().add (tf1, BorderLayout.NORTH);
tf1.setInputVerifier(new PassVerifier());
JTextField tf2 = new JTextField ("TextField2");
getContentPane().add (tf2, BorderLayout.SOUTH);
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
}
class PassVerifier extends InputVerifier {
public boolean verify(JComponent input) {
JTextField tf = (JTextField) input;
return "pass".equals(tf.getText());
}
}
public static void main(String[] args) {
Frame f = new VerifierTest();
f.pack();
f.setVisible(true);
}
}
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
// This program demonstrates the use of the Swing InputVerifier class.
// It creates two text fields; the first of the text fields expects the
// string "pass" as input, and will allow focus to advance out of it
// only after that string is typed in by the user. public class VerifierTest extends JFrame {
public VerifierTest() {
JTextField tf1 = new JTextField ("Type \"pass\" here");
getContentPane().add (tf1, BorderLayout.NORTH);
tf1.setInputVerifier(new PassVerifier());
JTextField tf2 = new JTextField ("TextField2");
getContentPane().add (tf2, BorderLayout.SOUTH);
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
}
class PassVerifier extends InputVerifier {
public boolean verify(JComponent input) {
JTextField tf = (JTextField) input;
return "pass".equals(tf.getText());
}
}
public static void main(String[] args) {
Frame f = new VerifierTest();
f.pack();
f.setVisible(true);
}
}
解决方案 »
- 急!!怎么用JAVA把彩票11选5的所有组合打印出来??
- 关于List<Map>的使用
- 求助,求解释!
- A fatal error has been detected by the Java Runtime Environment
- Tomcat中JSP,WebService都封装了对Bean的调用,web.xml配置怎么读?
- 请问在JList上的内容有了变动之后,如何更新显示?
- 用java启动cmd.exe文件后,有无方法读取这个黑窗口的数据
- 大家帮我解释一下是为什么?
- 请教一个小程序的问题(有关finalize的)
- 新手自学Java,在学习集合框架类容时,大牛们有什么建议么?
- 为什么这个程序创建的GUI右边总是有一大块空白
- 一个简单的JAVASCRIPT问题,解决了给80分。
负责监听JTextFiled的焦点
如果焦点离开时,不符合输入条件
让焦点再次回到JTextFiled不就行了吗?
焦点落在JTextField上,如果焦点离开而JTextField中的输入值为空或者不符合格式的话会弹出对话框提示,并且焦点无法离开。