我在文本框中进行验证,程序没有问题,但是当失去焦点时 命令显示java.lang.StackOverflowError,不知道怎么回事,请高人指点 谢谢
解决方案 »
- 求windowbuilder pro插件
- Log4j疑问
- 请教 轩辕互动 面试经验
- (没准你也有此疑惑)对 "equals"的疑问?
- 多线程怎样传递参数啊?
- 请问哪位高手有免费的java混淆器呀?
- 有没有关于:JAVA应用程序内嵌图像资源的方法的文章
- ~急~####高分请教,不知道在哪问了,怎么在oracle中编写存储进程存入BLOB类型数据,最好是要有源程序!!
- 一个头疼的问题,请对java多态性有深入了解的高手给予关注
- 请问,怎样对一个文件进行各种操作,比如(拷贝到某一目录,换名)等..
- 关于用InputVerifier进行表单验证的问题(在线等)
- 如何在一个类里引用另一个类里的已经存在的实例?
package JFCBook.Chapter9.jdk13;import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;public class VerifyInputExample3 extends JPanel {
public VerifyInputExample3() {
JLabel l;
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIVE;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(2, 2, 2, 2);
c.anchor = GridBagConstraints.EAST; add(l = new JLabel("Part Number:", SwingConstants.RIGHT), c);
l.setDisplayedMnemonic('p');
l.setLabelFor(partNumber); add(l = new JLabel("Quantity:", SwingConstants.RIGHT), c);
l.setDisplayedMnemonic('q');
l.setLabelFor(quantity); c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
add(partNumber, c);
c.gridx = 1;
c.gridy = GridBagConstraints.RELATIVE;
add(quantity, c); // Add the buttons on their own panel
c.gridx = 0;
c.weighty = 0.0;
c.weightx = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
JPanel p = new JPanel();
add(p, c);
p.add(okButton = new JButton("OK"), c);
okButton.setMnemonic('o');
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
okPressed();
}
}); p.add(cancelButton = new JButton("Cancel"), c);
cancelButton.setMnemonic('c');
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFrame frame = (JFrame)SwingUtilities.getAncestorOfClass(
JFrame.class, cancelButton);
frame.dispose();
}
}); partNumber.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// Pass focus to the quantity field
quantity.requestFocus();
}
}); quantity.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (partNumber.getText().equals("")) {
partNumber.requestFocus();
} else if (!quantity.getText().equals("")) {
if (verifier.verify(partNumber) &&
verifier.verify(quantity)) {
okPressed();
}
}
}
}); DocumentListener d = new DocumentListener() {
public void changedUpdate(DocumentEvent evt) {
// Nothing to do
} public void insertUpdate(DocumentEvent evt) {
checkButton();
} public void removeUpdate(DocumentEvent evt) {
checkButton();
}
}; partNumber.setName("part number");
quantity.setName("quantity"); verifier = new IntegerVerifier();
partNumber.setInputVerifier(verifier);
quantity.setInputVerifier(verifier);
partNumber.getDocument().addDocumentListener(d);
quantity.getDocument().addDocumentListener(d);
// OK button is disabled at first
okButton.setEnabled(false);
} public void okPressed() {
System.out.println("OK button pressed");
} public void checkButton() {
// Check whether the OK button should be enabled or not
boolean newOkState;
boolean partNumberClear = partNumber.getText().equals("");
boolean quantityClear = quantity.getText().equals(""); newOkState = partNumberClear == false && quantityClear == false; if (newOkState != okState) {
okButton.setEnabled(newOkState);
okState = newOkState;
}
} public static void main(String[] args) {
JFrame f = new JFrame("Input Verification 3");
f.getContentPane().add(new VerifyInputExample3());
f.pack();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
public void windowClosed(WindowEvent evt) {
System.exit(0);
}
});
f.setVisible(true);
}
JTextField partNumber = new JTextField(15);
JTextField quantity = new JTextField(15);
JButton okButton;
JButton cancelButton;
InputVerifier verifier; // Button state
boolean okState = false; // Disabled
}class IntegerVerifier extends InputVerifier {
public boolean verify(JComponent source) {
JTextField textField = (JTextField)source;
boolean valid = true;
try {
if (Integer.parseInt(textField.getText()) <= 0) {
valid = false;
}
} catch (Throwable t) {
valid = false;
} if (!valid) {
JOptionPane.showMessageDialog(textField,
"The value in the " + textField.getName() +
"\nfield is invalid.\nPlease correct it.\n",
"Invalid Input", JOptionPane.ERROR_MESSAGE);
}
return valid;
}
}
或者升级一下JDK