📄 jdatepicker.java
字号:
package com.sunking.swing;
import java.io.*;
import java.text.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
import com.sun.java.swing.plaf.motif.*;
import com.sun.java.swing.plaf.windows.*;
import com.sunking.swing.refer.*;
import com.sunking.swing.refer.DatePopup;
import com.sunking.swing.refer.JDateDocument;
/**
* <p>Title:OpenSwing </p>
* <p>Description: JDatePicker 日期选择框<BR>
* 履历:<BR>
* 2004/03/26 根据网友caiyj的建议引入了recoon写的关于JDateDocument的校验方法<BR>
* 2004/04/02 根据网友caiyj提交的BUG,修正了做为TableCellEditor时日期选择面板弹不出问题<BR>
* 2005/04/17 修正了弹出面板不能显示当前输入框中的日期<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_CN
= new SimpleDateFormat("yyyy/MM/dd");
public static final SimpleDateFormat dateFormat_CN1
= new SimpleDateFormat("yyyy-MM-dd");
public static final SimpleDateFormat datetimeFormat_CN
= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static final SimpleDateFormat datetimeFormat_CN1
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final int STYLE_CN_DATE = 0;
public static final int STYLE_CN_DATE1 = 1;
public static final int STYLE_CN_DATETIME = 2;
public static final int STYLE_CN_DATETIME1 = 3;
/**
* 日期格式类型
*/
private int formatStyle = STYLE_CN_DATE;
/**
* 当前设置日期格式
*/
private SimpleDateFormat dateFormat = datetimeFormat_CN;
/**
* 只有一个值的ComboBoxModel
*/
private SingleObjectComboBoxModel model = new SingleObjectComboBoxModel();
JDateDocument dateDocument = null;
/**
* 构造式
*/
public JDatePicker() {
this(STYLE_CN_DATE);
}
public JDatePicker(int formatStyle) {
this(formatStyle, getCurrentDate(
formatStyle == STYLE_CN_DATE ? dateFormat_CN :
(formatStyle == STYLE_CN_DATE1 ? dateFormat_CN1 :
(formatStyle == STYLE_CN_DATETIME ? datetimeFormat_CN :
datetimeFormat_CN1))));
}
public JDatePicker(int formatStyle, Object initialDatetime) {
this.setStyle(formatStyle);
initThis(initialDatetime);
}
public JDatePicker(int formatStyle, Date initialDatetime) {
this.setStyle(formatStyle);
initThis(initialDatetime);
}
protected void initThis(Object initialDatetime) {
this.setModel(model);
this.setSelectedItem(initialDatetime == null?
new Date():initialDatetime);
this.setEditable(true);
JTextField textField = ((JTextField) getEditor().getEditorComponent());
textField.setHorizontalAlignment(SwingConstants.CENTER);
dateDocument = new JDateDocument(textField,this.dateFormat);
textField.setDocument(dateDocument);
}
/**
* 取得当前系统日时
* @param dateFormat SimpleDateFormat
* @return String
*/
public final static String getCurrentDate(SimpleDateFormat smFormat) {
return smFormat.format(new Date());
}
/**
* 设置日期格式
* STYLE_CN_DATE
* STYLE_CN_DATE1
* STYLE_CN_DATETIME
* STYLE_CN_DATETIME1
* @param formatStyle int
*/
public void setStyle(int formatStyle) {
if (formatStyle != STYLE_CN_DATE
&& formatStyle != STYLE_CN_DATE1
&& formatStyle != STYLE_CN_DATETIME
&& formatStyle != STYLE_CN_DATETIME1) {
throw new UnsupportedOperationException(
"invalid formatStyle parameter!");
}
this.formatStyle = formatStyle;
if (formatStyle == STYLE_CN_DATE) {
dateFormat = dateFormat_CN;
} else if (formatStyle == STYLE_CN_DATE1) {
dateFormat = dateFormat_CN1;
} else if (formatStyle == STYLE_CN_DATETIME) {
dateFormat = datetimeFormat_CN;
} else if (formatStyle == STYLE_CN_DATETIME1) {
dateFormat = datetimeFormat_CN1;
}
model.setDateFormat(dateFormat);
if(dateDocument != null){
dateDocument.setDateFormat(dateFormat);
}
}
/**
* 取得日期格式
* STYLE_CN_DATE
* STYLE_CN_DATE1
* STYLE_CN_DATETIME
* STYLE_CN_DATETIME1
* @return int
*/
public int getStyle() {
return formatStyle;
}
/**
* 取得当前选择的日期
* @return Date
*/
public Date getSelectedDate() throws ParseException {
return dateFormat.parse(getSelectedItem().toString());
}
/**
* 设置当前选择的日期
* @return Date
*/
public void setSelectedDate(Date date) throws ParseException {
this.setSelectedItem(dateFormat.format(date));
}
public void setSelectedItem(Object anObject) {
model.setSelectedItem(anObject);
super.setSelectedItem(anObject);
}
/**
* 更新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 {
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);
}
}
/**
* 测试JDatePicker
*/
public static void main(String args[]) {
try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
Font font = new Font("SimSun", Font.PLAIN, 12);
java.util.Enumeration keys = UIManager.getLookAndFeelDefaults().
keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame("JDatePicker Demo");
JPanel c = new JPanel();
c.add(new JLabel("From:"));
JDatePicker datePickerFrom = new JDatePicker(JDatePicker.STYLE_CN_DATETIME);
c.add(datePickerFrom);
c.add(new JLabel("To:"));
JDatePicker datePickerTo = new JDatePicker(JDatePicker.STYLE_CN_DATE);
datePickerTo.setEditable(false);
c.add(datePickerTo);
f.getContentPane().add(c, BorderLayout.NORTH);
f.getContentPane().add(new JDatePicker(), BorderLayout.SOUTH);
final JTable table = new JTable(20, 10);
JComboBox editor = new JDatePicker();
editor.setBorder(null);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setDefaultEditor(Object.class, new DefaultCellEditor(editor));
JScrollPane sp = new JScrollPane(table);
f.getContentPane().add(sp, BorderLayout.CENTER);
f.setSize(600, 400);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((screenSize.width - f.getWidth()) / 2,
(screenSize.height - f.getHeight()) / 2);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -