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

📄 test.java

📁 swing 教程,与大家分享一下,哈哈,希望大家多多指教
💻 JAVA
字号:
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JApplet {
	JTextField tf = new JTextField(DateDocument.initString);

	public Test() {
		Container contentPane = getContentPane();
		JLabel label = new JLabel("Date:");
		Font font = new Font("Dialog", Font.PLAIN, 24);

		tf.setFont(font);
		label.setFont(font);

		tf.setDocument(new DateDocument(tf));

		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER,
											 10,10));	
		contentPane.add(label);
		contentPane.add(tf);
	}
}
class DateDocument extends PlainDocument {
	public static String initString = "XX/XX/XXXX"; // Y10K!
	private static int sep1 = 2, sep2 = 5;
	private JTextComponent textComponent;
	private int newOffset;

	public DateDocument(JTextComponent tc) {
		textComponent = tc;
		try {
			insertString(0, initString, null);
		}
		catch(Exception ex) { ex.printStackTrace(); }
	}
	public void insertString(int offset, String s, 
							AttributeSet attributeSet) 
							throws BadLocationException {
		if(s.equals(initString)) {
			super.insertString(offset, s, attributeSet);
		}
		else {
			try {
				Integer.parseInt(s);
			}
			catch(Exception ex) {
				return;  // only allow integer values
			}

			newOffset = offset;

			if(atSeparator(offset)) {
				newOffset++;	
				textComponent.setCaretPosition(newOffset);
			}
			super.remove(newOffset, 1);
			super.insertString(newOffset, s, attributeSet);
		}
	}
	public void remove(int offset, int length) 
							throws BadLocationException {
		if(atSeparator(offset)) 
			textComponent.setCaretPosition(offset-1);
		else
			textComponent.setCaretPosition(offset);
	}
	private boolean atSeparator(int offset) {
		return offset == sep1 || offset == sep2;
	}
}

⌨️ 快捷键说明

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