sorry,由于近来忙,没时间修改后再贴,可能下面的代码需要去掉某些(如MainFrame..)引用才能独立使用,
有空就帮俺试试!
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class UniDTextField extends JTextField implements FocusListener
{
// private MainFrame mMainFrame;
private String mTag="";
//日期分隔符
private char mSparator='-';
/**constructs a date field ,add the sparator is '-'*/
public UniDTextField()
{
super();
setDocument(new DTextDoc(this,mSparator));
setBorder(BorderFactory.createLoweredBevelBorder());
setFont(new Font("宋体",0,13));
addFocusListener(this);
}
/**constructs a date field the specified sparator
* @param main 主窗口对象引用
* @param tag 控制标示
* @param sp 日期分隔符,如'/'、'-'
*/
public UniDTextField( String tag, char sp)
{ super(12);
mTag = tag;
// mMainFrame = main;
mSparator = sp;
setDocument(new DTextDoc(this,mSparator));
setBorder(BorderFactory.createLoweredBevelBorder());
setFont(new Font("宋体",0,13));
addFocusListener(this);
}
/**初始化日期输入框
* @param date 日期格式的字串,如("yyyy-mm-dd"、"yyyymmdd")
* 没有返回值
*/
public void init(String date){
setText(date);
}
/**参考接口ComponentUnit中的方法说明*/
public void setStatus(int pStatus ){
if (mTag.equals("1")) {
setBackground(new Color(255, 255, 173));
setEditable(true);
}
if (mTag.equals("2")) {
setBackground(Color.white);
setEditable(true);
}
if (mTag.equals("3")) {
setBackground(Color.lightGray);
setEditable(false);
}
}
/**参考接口ComponentUnit中的方法说明*/
public void clearField(){}
public void focusGained(FocusEvent e){}
//失去焦点时,对输入的日期进行合法性验证
public void focusLost(FocusEvent e)
{ removeFocusListener(this);
if(!DTextDoc.isLegalDate(getText()))
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(this,"不合法的日期,please input again.");
requestFocus();
}
addFocusListener(this);
}
}
class DTextDoc extends PlainDocument
{ private char sparator = '-';
private JTextComponent textComponent;
private int newOffset;
private AttributeSet mAttributeSet=null;
public DTextDoc(JTextComponent tc,char sp) {
textComponent = tc;
sparator = sp;
String strCurrentDate=getCurrentDate();
try{insertString(0, strCurrentDate, null);}catch(Exception ex){
Toolkit.getDefaultToolkit().beep();
return;
}
}
public void insertString(int offset, String s,AttributeSet attributeSet) throws BadLocationException
{ String strY="",strM="",strD="";
if (mAttributeSet==null) mAttributeSet = attributeSet;
if (s.length()==1)
{
try {Integer.parseInt(s);}catch(Exception ex)
{ Toolkit.getDefaultToolkit().beep();
return;
}
newOffset = offset;
if(offset==4||offset==7){
newOffset++;
textComponent.setCaretPosition(newOffset);
}
super.remove(newOffset, 1);
super.insertString(newOffset, s, attributeSet);
}
else
if(s.length()==10||s.length()==8){
if (!isLegalDate(s))
{
Toolkit.getDefaultToolkit().beep();
return;
}
if (s.length()==10)
{ try{
strY=s.substring(0,4);
strM=s.substring(5,7);
strD=s.substring(8);
}catch(Exception e){
Toolkit.getDefaultToolkit().beep();
return ;
}
}
else
if(s.length()==8)
{ try{
strY=s.substring(0,4);
strM=s.substring(4,6);
strD=s.substring(6);
}catch(Exception e){
Toolkit.getDefaultToolkit().beep();
return ;
}
}
super.remove(0,getLength());
super.insertString(0, strY +sparator + strM + sparator + strD, attributeSet);
} else
{ Toolkit.getDefaultToolkit().beep();
return;
}
}
public void remove(int offset, int length) throws BadLocationException
{
if(offset==4||offset==7)
{ textComponent.setCaretPosition(offset);
return;
}
textComponent.setCaretPosition(offset);
super.remove(offset,1);
super.insertString(offset, "0", mAttributeSet);
textComponent.setCaretPosition(offset);
}
/**判断日期的合法性
* @param strDate 日期格式的字符串,如("yyyy-mm-dd"、"yyyymmdd")
* 返回值 如果合法返回true,否则返回false
*/
public static boolean isLegalDate(String strDate)
{
int intY,intM,intD;
int[]standardDays={31,28,31,30,31,30,31,31,30,31,30,31};
int[]leapyearDays={31,29,31,30,31,30,31,31,30,31,30,31};
if (strDate.trim().length()==10)
{ strDate=strDate.trim();
try{
intY=Integer.parseInt(strDate.substring(0,4));
intM=Integer.parseInt(strDate.substring(5,7));
intD=Integer.parseInt(strDate.substring(8));
}catch(Exception e){return false;}
}
else
if(strDate.trim().length()==8)
{ strDate=strDate.trim();
try{
intY=Integer.parseInt(strDate.substring(0,4));
intM=Integer.parseInt(strDate.substring(4,6));
intD=Integer.parseInt(strDate.substring(6));
}catch(Exception e){return false;}
} else
return false;
if (intM>12||intM<1||intY<1||intD<1)return false;
if ((intY%4==0&&intY%100!=0)||intY%400==0)return (intD<=leapyearDays[intM-1]);
return (intD<=standardDays[intM-1]);
}
/**获取当前系统日期
* 返回值 为带分隔符的日期格式的字符串,如"yyyy-mm-dd"
*/
private String getCurrentDate()
{
GregorianCalendar Day=new GregorianCalendar();
int yyyy,mm,dd;
String MM,DD;
yyyy=Day.get(Calendar.YEAR);
mm=Day.get(Calendar.MONTH)+1;
dd=Day.get(Calendar.DATE);
if (mm<10)MM="0"+mm;else
MM=Integer.toString(mm);
if (dd<10)DD="0"+dd;else
DD=Integer.toString(dd);
return yyyy + ""+sparator + MM + sparator + DD;
}
}