⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 verifyinputexample4.java

📁 一个用java开发界面的程序集(jfc核心编程)
💻 JAVA
字号:
package JFCBook.Chapter9.jdk13;

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

public class VerifyInputExample4 extends JPanel {
	public VerifyInputExample4() {
		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);
		okButton.setVerifyInputWhenFocusTarget(false);
		cancelButton.setVerifyInputWhenFocusTarget(false);

		partNumber.getDocument().addDocumentListener(d);
		quantity.getDocument().addDocumentListener(d);
		
		// OK button is disabled at first
		okButton.setEnabled(false);
	}

	public void okPressed() {
		if (verifier.verify(partNumber) &&
			verifier.verify(quantity)) {
			System.out.println("Fields are OK");
		} else {
			System.out.println("Error converting input fields");
		}
	}

	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 4");
		f.getContentPane().add(new VerifyInputExample4());
		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
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -