import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* 日期时间选择对话框,模仿Windows日期和时间属性对话框
* @see #showDialog(java.awt.Component, String, java.util.Calendar)
*/
public class JDateChooser extends JPanel implements ChangeListener, ItemListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private static boolean showTime=false;
private JComboBox month = new JComboBox(new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"});
private JSpinner year = new JSpinner(new SpinnerNumberModel(1, 1, 9999, 1));
private DateGrid dateGrid = new DateGrid();
private JSpinner time = new JSpinner(new SpinnerDateModel());
private static Timer timer;
public JDateChooser(Calendar initialDate) {
timer=new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e) {
time.setValue(new Date());
}});
this.setBackground(Color.white);
this.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
this.setLayout(new GridLayout(1, 1, 5, 10));
JPanel p1 = new JPanel();
p1.setBackground(Color.white);
p1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("日期"), BorderFactory.createEmptyBorder(5, 5, 10, 5)));
p1.setLayout(new BorderLayout(10, 10));
JPanel p11 = new JPanel(new GridLayout(1, 2, 10, 10));
p11.setBackground(Color.white);
p11.add(month);
month.addItemListener(this);
month.setPreferredSize(new Dimension(0, 20));
p11.add(year);
year.setEditor(new JSpinner.NumberEditor(year, "0"));
year.addChangeListener(this);
year.setPreferredSize(new Dimension(0, 20));
p1.add(p11, BorderLayout.NORTH);
p1.add(dateGrid);
this.add(p1);
if(showTime){
JPanel p2 = new JPanel(new BorderLayout(10, 10));
p2.setBackground(Color.white);
p2.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("时间"), BorderFactory.createEmptyBorder(0, 10, 10, 10)));
time.setPreferredSize(new Dimension(0, 20));
JSpinner.DateEditor editor = new JSpinner.DateEditor(time, "HH:mm:ss");
editor.getTextField().setHorizontalAlignment(JTextField.RIGHT);
time.setEditor(editor);
JPanel p = new JPanel();
p.setBackground(Color.white);
time.setPreferredSize(new Dimension(100, 20));
p.add(time);
p2.add(p, BorderLayout.SOUTH);
p2.add(new Clock(time));
// timer.start();
this.add(p2);
setPreferredSize(new Dimension(369, 200));
}else{
setPreferredSize(new Dimension(185, 200));
}
setCalendar(initialDate);
}
/**
* 设置日期和时间
* @param calendar 日期和时间
*/
public void setCalendar(Calendar calendar) {
if(calendar==null) {
calendar = new GregorianCalendar();
}
month.setSelectedIndex(calendar.get(Calendar.MONTH));
year.setValue(calendar.get(Calendar.YEAR));
int year = (Integer)this.year.getValue();
int month = this.month.getSelectedIndex()+1;
dateGrid.set(year, month);
dateGrid.setDay(calendar.get(Calendar.DAY_OF_MONTH));
if(showTime==false){
calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
calendar.set(GregorianCalendar.MINUTE, 0);
calendar.set(GregorianCalendar.MILLISECOND, 0);
}
time.setValue(calendar.getTime());
}
/**
* 获得日期和时间
* @return 日期和时间
*/
public Calendar getCalendar() {
int year = (Integer)this.year.getValue();
int month = this.month.getSelectedIndex();
int day = dateGrid.getDay();
Calendar c = Calendar.getInstance();
Date date = (Date) time.getValue();
c.setTime(date);
c.set(year, month, day);
return c;
}
/**
* 显示对话框以选择日期,使用方法同JColorChooser.showDialog
* @param c 父级组件
* @param title 对话框标题
* @param initialDate 初始化日期和时间
* @param showTime 是否显示钟表
* @return 选择的日期,null为取消选择
*/
public static Calendar showDialog(Component c, String title,
Calendar initialDate, boolean showTime) {
JDateChooser.showTime=showTime;
Window window = c == null ? JOptionPane.getRootFrame() : SwingUtilities
.windowForComponent(c);
JDialog dlg;
if (window instanceof Frame)
dlg = new JDialog((Frame) window, title, true);
else
dlg = new JDialog((Dialog) window, title, true);
JDateChooser dc = new JDateChooser(initialDate);
ActionHandler actionHandler = new ActionHandler(dc);
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createEmptyBorder(6, 6, 0, 6), BorderFactory
.createEtchedBorder()));
p.add(dc);
dlg.add(p);
p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
dlg.add(p, BorderLayout.SOUTH);
JButton btOk = new JButton("确定");
btOk.addActionListener(actionHandler);
dlg.getRootPane().setDefaultButton(btOk);
p.add(btOk);
JButton btCancel = new JButton("取消");
btCancel.addActionListener(actionHandler);
p.add(btCancel);
dlg.pack();
dlg.setLocationRelativeTo(window);
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.addWindowListener(actionHandler);
dlg.setVisible(true);
return actionHandler.getCalendar();
}
public void stateChanged(ChangeEvent e) {
dateGrid.set((Integer)year.getValue(), month.getSelectedIndex()+1);
}
public void itemStateChanged(ItemEvent e) {
dateGrid.set((Integer)year.getValue(), month.getSelectedIndex()+1);
}
private static class ActionHandler extends WindowAdapter implements ActionListener {
private JDateChooser dc;
public ActionHandler(JDateChooser dc) {
this.dc = dc;
}
public void windowClosing(WindowEvent e) {
actionPerformed(new ActionEvent(this, 0, "取消"));
}
public void actionPerformed(ActionEvent e) {
timer.stop();
JDialog dlg = (JDialog) SwingUtilities.windowForComponent(dc);
if("取消".equals(e.getActionCommand())) {
dc = null;
}
dlg.dispose();
}
public Calendar getCalendar() {
return dc==null?null:dc.getCalendar();
}
}
private static class DateGrid extends JPanel implements MouseListener {
JLabel[][] lbs = new JLabel[7][7];
int year = 1, month = 1, day = 1;
public DateGrid() {
super(new GridLayout(7, 7));
this.setBackground(Color.white);
Color blue = new Color(140, 150, 255);
Color white = new Color(220, 220, 255);
for(int i=0; i<7; i++) {
for(int j=0; j<7; j++) {
this.add(lbs[i][j] = new JLabel((String)null));
lbs[i][j].setOpaque(true);
lbs[i][j].setHorizontalAlignment(JLabel.CENTER);
lbs[i][j].setBackground(Color.white);
if(i>0) {
lbs[i][j].addMouseListener(this);
} else {
lbs[0][j].setBackground(blue);
lbs[0][j].setForeground(white);
}
}
}
lbs[0][0].setText("日");
lbs[0][1].setText("一");
lbs[0][2].setText("二");
lbs[0][3].setText("三");
lbs[0][4].setText("四");