关于用InputVerifier进行表单验证的问题(在线等)

zhongzuo 2006-04-24 05:47:53
我想用InputVerifier进行表单验证,下边为主程序代码


import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;
import java.awt.Toolkit;


public class ChatLoginFrame extends JFrame {

/**
*
*/
private static final long serialVersionUID = -2086542134347177135L;
private static JFrame frame = null;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JLabel jLabel1 = null;
private JPasswordField jPasswordField = null;
//private final RegeditFrame frame;
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setInputVerifier(new FormattedTextFieldVerifier());
}
return jTextField;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("登陆");
jButton.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 12));
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String id = jTextField.getText();
if(id.equals("")){
JOptionPane.showMessageDialog(null,"卡号不能为空!");

}
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton;
}

/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("取消");
jButton1.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 12));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
System.exit(0);
}
});
}
return jButton1;
}

/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setText("注册向导");
jButton2.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 12));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
// RegeditDialog dialog= new RegeditDialog(frame,"注册向导",true);
// dialog.setVisible(true);

// dialog.show();
// JFrame.this.validate(false);



}
});
}
return jButton2;
}

/**
* This method initializes jPasswordField
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getJPasswordField() {
if (jPasswordField == null) {
jPasswordField = new JPasswordField();
}
return jPasswordField;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
frame = new ChatLoginFrame();
frame.show();
}

/**
* This is the default constructor
*/
public ChatLoginFrame() {
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setLocale(new java.util.Locale("zh", "CN", ""));
this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setIconImage(Toolkit.getDefaultToolkit().getImage("E:/java\u5b66\u4e60/jexam/ChatLogin/image/title.png"));
this.setFont(new java.awt.Font("幼圆", java.awt.Font.PLAIN, 12));
this.setResizable(false);
this.setEnabled(true);
this.setContentPane(getJContentPane());
this.setTitle("登陆");
}




/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
gridBagConstraints7.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints7.gridwidth = 2;
gridBagConstraints7.gridx = 1;
gridBagConstraints7.gridy = 1;
gridBagConstraints7.ipadx = 111;
gridBagConstraints7.ipady = 4;
gridBagConstraints7.weightx = 1.0;
gridBagConstraints7.insets = new java.awt.Insets(5,27,18,43);
GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
gridBagConstraints6.insets = new java.awt.Insets(5,23,18,3);
gridBagConstraints6.gridy = 1;
gridBagConstraints6.ipadx = 34;
gridBagConstraints6.ipady = 11;
gridBagConstraints6.gridx = 0;
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.insets = new java.awt.Insets(18,5,11,5);
gridBagConstraints5.gridy = 2;
gridBagConstraints5.ipadx = 10;
gridBagConstraints5.gridx = 2;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.insets = new java.awt.Insets(18,2,11,5);
gridBagConstraints4.gridy = 2;
gridBagConstraints4.ipadx = 14;
gridBagConstraints4.gridx = 1;
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.insets = new java.awt.Insets(18,20,11,6);
gridBagConstraints3.gridy = 2;
gridBagConstraints3.ipadx = 14;
gridBagConstraints3.gridx = 0;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints2.gridwidth = 2;
gridBagConstraints2.gridx = 1;
gridBagConstraints2.gridy = 0;
gridBagConstraints2.ipadx = 111;
gridBagConstraints2.ipady = 4;
gridBagConstraints2.weightx = 1.0;
gridBagConstraints2.insets = new java.awt.Insets(7,27,5,43);
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.insets = new java.awt.Insets(5,24,7,2);
gridBagConstraints1.gridy = 0;
gridBagConstraints1.ipadx = 20;
gridBagConstraints1.ipady = 11;
gridBagConstraints1.gridx = 0;
jLabel1 = new JLabel();
jLabel1.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 12));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("密码:");
jLabel = new JLabel();
jLabel.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 12));
jLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel.setText("员工ID:");
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.add(jLabel, gridBagConstraints1);
jContentPane.add(getJTextField(), gridBagConstraints2);
jContentPane.add(getJButton(), gridBagConstraints3);
jContentPane.add(getJButton1(), gridBagConstraints4);
jContentPane.add(getJButton2(), gridBagConstraints5);
jContentPane.add(jLabel1, gridBagConstraints6);
jContentPane.add(getJPasswordField(), gridBagConstraints7);
jContentPane.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "员工登陆", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", java.awt.Font.BOLD, 12), null));
}
return jContentPane;
}

}

...全文
129 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
maloc.jiang 2006-04-26
在WIN2000下试一试
或者升级一下JDK
  • 打赏
  • 举报
回复
zhongzuo 2006-04-24
...............
  • 打赏
  • 举报
回复
zhongzuo 2006-04-24
正在等...
  • 打赏
  • 举报
回复
zhongzuo 2006-04-24
下边是验证代码:
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.*;

class FormattedTextFieldVerifier extends InputVerifier {

public FormattedTextFieldVerifier() {
super();
// TODO 自动生成构造函数存根
}

public boolean shouldYieldFocus(JComponent input) {
// TODO 自动生成方法存根
return super.shouldYieldFocus(input);
}

public boolean verify(JComponent input) {
// TODO 自动生成方法存根
JTextField textField = (JTextField)input;
boolean valid = true;
if(textField.getText().equals(""));
{valid = false;}

if(!valid){
JOptionPane.showMessageDialog(textField,
"请输入您的昵称!", null, JOptionPane.ERROR_MESSAGE);
}
// return false;
return valid;
}

}

编译通过,运行时当文本框失去焦点时,出现错误如下:

java.lang.StackOverflowError


百思不得其解,请高人指点,谢谢!
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2006-04-24 05:47
社区公告
暂无公告