?? jdatepicker.java
字號:
package com.sunking.swing;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
import javax.swing.border.*;
import com.sun.java.swing.plaf.motif.*;
import com.sun.java.swing.plaf.windows.*;
//import java.net.*;
/**
* <p>Title:OpenSwing </p>
* <p>Description: JDatePicker 日期選擇框<BR>
* 履歷:<BR>
* 2004/03/26 根據網友caiyj的建議引入了recoon寫的關于JDateDocument的校驗方法<BR>
* 2004/04/02 根據網友caiyj提交的BUG,修正了做為TableCellEditor時日期選擇面板彈不出問題<BR>
* </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>
* @version 1.0
*/
public class JDatePicker
extends JComboBox
implements Serializable {
/**
* 年月日格式
*/
public static final SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd");
/**
* 年月格式
*/
public static final SimpleDateFormat monthFormat
= new SimpleDateFormat("yyyy-MM");
/**
* 日格式
*/
public static final SimpleDateFormat dayFormat
= new SimpleDateFormat("d");
/**
* 構造式
*/
public JDatePicker() {
setEditable(true);
JTextField textField = ( (JTextField)this.getEditor().
getEditorComponent());
textField.setDocument(new JDateDocument(textField));
setSelectedItem(dateFormat.format(new Date()));
}
/**
* 取得當前選擇的日期
* @return Date
*/
public Date getSelectedDate() {
try {
return dateFormat.parse(this.getSelectedItem().toString());
}
catch (Exception ex) {
return new Date();
}
}
/**
* 設置當前選擇的日期
* @return Date
*/
public void setSelectedDate(Date date) {
setSelectedItem(date);
}
/**
* 設置當前選取的日期
* @param item Object
*/
public void setSelectedItem(Object item) {
removeAllItems();
addItem(item);
super.setSelectedItem(item);
}
/**
* 更新UI
*/
public void updateUI() {
ComboBoxUI cui = (ComboBoxUI) UIManager.getUI(this);
if (cui instanceof MetalComboBoxUI) {
cui = new MetalDateComboBoxUI();
}
else if (cui instanceof MotifComboBoxUI) {
cui = new MotifDateComboBoxUI();
}
else if (cui instanceof WindowsComboBoxUI) {
cui = new WindowsDateComboBoxUI();
}
setUI(cui);
}
// UI Inner classes -- one for each supported Look and Feel
/**
* <p>Title: OpenSwing</p>
* <p>Description: MetalDateComboBoxUI</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.0
*/
class MetalDateComboBoxUI
extends MetalComboBoxUI {
protected ComboPopup createPopup() {
return new DatePopup(comboBox);
}
}
/**
*
* <p>Title: OpenSwing</p>
* <p>Description: WindowsDateComboBoxUI</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.0
*/
class WindowsDateComboBoxUI
extends WindowsComboBoxUI {
protected ComboPopup createPopup() {
return new DatePopup(comboBox);
}
}
/**
*
* <p>Title: OpenSwing</p>
* <p>Description: MotifDateComboBoxUI</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.0
*/
class MotifDateComboBoxUI
extends MotifComboBoxUI {
protected ComboPopup createPopup() {
return new DatePopup(comboBox);
}
}
/**
* <p>Title:JDateDocument </p>
* <p>Description: JDateDocument 實現日期的輸入格式限制</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>,recoon
* @version 1.0
*/
public static class JDateDocument
extends PlainDocument {
private JTextComponent textComponent; //日期輸入文本框
private int newOffset; //新的插入位置
/******************************************************************
** 函數名稱:public JDateDocument(JTextComponent tc)
** 功能描述:設置此文本框顯示的默認值和格式限制
** 入口參數:tc : JTextComponent類型,當前操作的文本框
** 返回值:無
** 調用者:類JDateDocument
*******************************************************************/
public JDateDocument(JTextComponent tc) {
//保存操作的文本框
textComponent = tc;
//獲取當前日期
String strCurrentDate = getCurrentDate();
//設置顯示為當前日期,同時完成顯示的格式化
try {
insertString(0, strCurrentDate, null);
}
catch (Exception ex) {
System.out.println(ex);
}
}
/******************************************************************
** 函數名稱:public void insertString(int offset, String s,
** AttributeSet attributeSet) throws BadLocationException
** 功能描述:重載原方法,限制用戶插入格式為日期格式
** 入口參數:offset: int型,插入位置
** s: String型,插入字符串
** attributeSet: AttributeSet型,屬性集
** 返回值:無
** 調用者:類JDateDocument
*******************************************************************/
public void insertString(int offset, String s,
AttributeSet attributeSet) throws
BadLocationException {
String toTest; //用于測試輸入合法性的字符串
//判斷插入字符串長度
if (s.length() == 1) {
//長度為1
try {
//限制輸入為整數
Integer.parseInt(s);
}
catch (Exception ex) {
//錯誤則提示并返回
Toolkit.getDefaultToolkit().beep();
return;
}
//取得原始插入位置
newOffset = offset;
//如果插入位置為"-"符號的前面,則移動到其后面插入(改變newOffset的值)
if (offset == 4 || offset == 7) {
newOffset++;
textComponent.setCaretPosition(newOffset);
}
//如果插入位置為最后,則不插入
if (offset == 10)return;
//取得顯示的時間,處理后得到要顯示的字符串
toTest = textComponent.getText();
toTest = toTest.substring(0, newOffset) + s +
toTest.substring(newOffset + 1, 10);
//如果要顯示的字符串合法,則顯示,否則給出提示并退出
if (!isLegalDate(toTest)) {
Toolkit.getDefaultToolkit().beep();
return;
}
//插入字符串
super.remove(newOffset, 1);
super.insertString(newOffset, s, attributeSet);
}
//如果插入長度10
else if (s.length() == 10) {
//合法則顯示,否則給出提示退出
if (!isLegalDate(s)) {
Toolkit.getDefaultToolkit().beep();
return;
}
//插入字符串
super.remove(0, getLength());
super.insertString(0, s, attributeSet);
}
}
/**********************************************************************************
** 函數名稱:public void remove(int offset, int length) throws BadLocationException
** 功能描述:重載原方法,刪除合適位置的字符串
** 入口參數:offset: int型,插入位置
** length: int型,刪除長度
** 返回值:無
** 調用者:insertString(int, String,AttributeSet)
***********************************************************************************/
public void remove(int offset, int length) throws BadLocationException {
//如果插入位置在"-"前,則回退一個光標位置
if (offset == 4 || offset == 7)
textComponent.setCaretPosition(offset - 1);
else
textComponent.setCaretPosition(offset);
}
/**********************************************************************************
** 函數名稱:public boolean isLegalDate(String strDate)
** 功能描述:判斷插入的長度為10的字符串是否合法
** 入口參數:intY: int型,年的值
** intM: int型,月的值
** intD: int型,日的值
** 返回值:boolean型,真,表示是合法的,假,表示不合法
** 調用者:insertString(int, String,AttributeSet)
***********************************************************************************/
public boolean isLegalDate(String strDate) {
int intY, intM, intD; //年,月,日的值
int iCaretPosition; //光標位置
//獲取字符串
strDate = strDate.trim();
//如果為空,長度不對,則為非法,返回false
if (strDate == null || strDate.trim().equals("") ||
strDate.trim().length() != 10)
return false;
//如果是全角字符,則返回false
for (int i = 0; i < 10; i++)
if ( ( (int) strDate.charAt(i)) > 255)
return false;
//取年,月,日的值
try {
intY = Integer.parseInt(strDate.substring(0, 4));
intM = Integer.parseInt(strDate.substring(5, 7));
intD = Integer.parseInt(strDate.substring(8));
}
catch (Exception e) {
//失敗則返回false
return false;
}
//數值越界,則修改至正確值
//保存光標位置
iCaretPosition = textComponent.getCaretPosition();
//年越界
// if(intY > 2999){
// //修改為合法值
// textComponent.setText("2999" + strDate.substring(5));
// //手動設置光標位置
// textComponent.setCaretPosition(iCaretPosition + 1);
// return false;
// }
// if(intY < 1000){
// textComponent.setText("1000" + strDate.substring(5));
// textComponent.setCaretPosition(iCaretPosition + 1);
// return false;
// }
//月越界
if (intM > 12) {
textComponent.setText(strDate.substring(0, 5) + "12" +
strDate.substring(7));
textComponent.setCaretPosition(iCaretPosition + 1);
return false;
}
if (intM < 1) {
textComponent.setText(strDate.substring(0, 5) + "01" +
strDate.substring(7));
textComponent.setCaretPosition(iCaretPosition + 1);
return false;
}
//根據月份,判斷日期輸入,如越界,則修改
switch (intM) {
//最大天數為31天
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
//如果輸入大于31,則修改為31
if (intD > 31) {
textComponent.setText(strDate.substring(0, 8) + "31");
textComponent.setCaretPosition(iCaretPosition + 1);
}
if (intD < 1) {
textComponent.setText(strDate.substring(0, 8) + "01");
textComponent.setCaretPosition(iCaretPosition + 1);
}
return (intD <= 31 && intD > 0);
//最大天數為30天
case 4:
case 6:
case 9:
case 11:
//如果輸入大于30,則修改為30
if (intD > 30) {
textComponent.setText(strDate.substring(0, 8) + "30");
textComponent.setCaretPosition(iCaretPosition + 1);
}
if (intD < 1) {
textComponent.setText(strDate.substring(0, 8) + "01");
textComponent.setCaretPosition(iCaretPosition + 1);
}
return (intD <= 30 && intD > 0);
//2月份
case 2:
//區別閏年
if ( (intY % 4 == 0 && intY % 100 != 0) || intY % 400 == 0) {
//如果輸入大于29,則修改為29
if (intD > 29) {
textComponent.setText(strDate.substring(0, 8) +
"29");
textComponent.setCaretPosition(iCaretPosition + 1);
}
if (intD < 1) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -