📄 verifyinputexample3.java
字号:
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -