关于inputVerifier抛出java.lang.StackOverflowError异常现象 我在文本框中进行验证,程序没有问题,但是当失去焦点时 命令显示java.lang.StackOverflowError,不知道怎么回事,请高人指点 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是程序代码: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; }} 在WIN2000下试一试或者升级一下JDK 问一个小问题,谢谢! 问:用java开发大型网络游戏有哪些优势? 找女朋友....... java如何连接局域网内的另一台机器上access数据库 getConnection()方法如何写? 我想做个系统 一个关于同步概念的问题 哪位高手能解释一下下面语句的意思?谢谢 看看这道题! 请问,JBuilder3有JDK运行环境么? 紧急求助!!JXL往excel模板中写数据,excel模板的数据透视表失效。 关于用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