package swing_study.jtextarea;/**
* <p>Title: newman的作品</p>
* <p>Description: neman的版权</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: shu</p>
* @author [email protected]
* @version 1.0
*
* 只可以输入整数的文档
*/
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*; public class JTextArea_DocumentIntOnly extends JFrame {
JTextField tf = new JTextField(30); public JTextArea_DocumentIntOnly() {
Container contentPane = getContentPane();
JLabel label = new JLabel("Enter an Integer:");
//tf.setDocument(new IntegerDocument());
tf.setDocument(new DateDocument(this.tf));
contentPane.setLayout(new FlowLayout());
contentPane.add(label);
contentPane.add(tf);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
public static void main(String[] args) {
JTextArea_DocumentIntOnly JTextArea_DocumentIntOnly1 = new JTextArea_DocumentIntOnly();
JTextArea_DocumentIntOnly1.pack() ;
JTextArea_DocumentIntOnly1.show() ;
}
}class IntegerDocument extends PlainDocument {
public void insertString(int offset, String s, AttributeSet attributeSet)throws BadLocationException {
try {
Integer.parseInt(s);
super.insertString(offset, s, attributeSet);
}
catch(Exception ex) { // only allow integer values
Toolkit.getDefaultToolkit().beep();
System.out.println("Integer Only!");
return;
}
}
}
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(); }
}
/**
* 插入字符
* @param offset 偏移
* @param s 输入的字符
* @param attributeSet
* @throws BadLocationException
*/
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);
// System.out.println("s: "+s);
}
catch(Exception ex) {
return; // only allow integer values
}
newOffset = offset;
if(atSeparator(offset)) {//是“/”
newOffset++;
textComponent.setCaretPosition(newOffset);
}
//效果就是一个原来字符被替换
super.remove(newOffset, 1);//从newOffset开始,删除一个字符
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;
}
}
* <p>Title: newman的作品</p>
* <p>Description: neman的版权</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: shu</p>
* @author [email protected]
* @version 1.0
*
* 只可以输入整数的文档
*/
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*; public class JTextArea_DocumentIntOnly extends JFrame {
JTextField tf = new JTextField(30); public JTextArea_DocumentIntOnly() {
Container contentPane = getContentPane();
JLabel label = new JLabel("Enter an Integer:");
//tf.setDocument(new IntegerDocument());
tf.setDocument(new DateDocument(this.tf));
contentPane.setLayout(new FlowLayout());
contentPane.add(label);
contentPane.add(tf);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
public static void main(String[] args) {
JTextArea_DocumentIntOnly JTextArea_DocumentIntOnly1 = new JTextArea_DocumentIntOnly();
JTextArea_DocumentIntOnly1.pack() ;
JTextArea_DocumentIntOnly1.show() ;
}
}class IntegerDocument extends PlainDocument {
public void insertString(int offset, String s, AttributeSet attributeSet)throws BadLocationException {
try {
Integer.parseInt(s);
super.insertString(offset, s, attributeSet);
}
catch(Exception ex) { // only allow integer values
Toolkit.getDefaultToolkit().beep();
System.out.println("Integer Only!");
return;
}
}
}
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(); }
}
/**
* 插入字符
* @param offset 偏移
* @param s 输入的字符
* @param attributeSet
* @throws BadLocationException
*/
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);
// System.out.println("s: "+s);
}
catch(Exception ex) {
return; // only allow integer values
}
newOffset = offset;
if(atSeparator(offset)) {//是“/”
newOffset++;
textComponent.setCaretPosition(newOffset);
}
//效果就是一个原来字符被替换
super.remove(newOffset, 1);//从newOffset开始,删除一个字符
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;
}
}
try{
Double.pareseDouble(xxx);
xxxx
}Catch(){
弹出一个警告窗口,提示输入数字。
}