只使用了最简的组件,JLabel和JPanel.所以有点丑陋。但是使用方便,在程序的任何部分只要使用String ss = new datePicker().getTheDay();当关闭窗口,或者在日期上
DoubleClick,就会得到日期。
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class datePicker extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleDateFormat mft=new SimpleDateFormat("yyyy-MM-dd");
private int year=0,month=0,day=0;
private final JLabel currentday;
private String today = mft.format(new Date());
private final JPanel body = new JPanel();
private final JLabel bott = new JLabel();
private final ArrayList<JLabel> lm = new ArrayList<JLabel>();
private Calendar cal = Calendar.getInstance();
private Calendar daycal = Calendar.getInstance();
private int currLabel=0,tday = 0,sday = 0;
private final int tyear,tmonth,thisday;
private final int[] dayofmonth = {31,28,31,30,31,30,31,31,30,31,30,31};
private boolean notStop = true;
public datePicker(JTextField jt){
setSize(240,240);
setVisible(true);
final JPanel contp = new JPanel();
setContentPane(contp);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
contp.setLayout(null);
final JPanel header = new JPanel();
body.setLayout(null);
header.setBounds(0, 0, 240, 25);
header.setBackground(Color.LIGHT_GRAY);
header.setLayout(null);
contp.add(header);
try{
year = Integer.parseInt(today.substring(0, 4));
month = Integer.parseInt(today.substring(5,7));
day = Integer.parseInt(today.substring(8,10));
}catch(Exception e){
}
tyear = year;
tmonth = month;
thisday = day;
final JLabel pyear = new JLabel("<<",JLabel.CENTER);
final JLabel pmonth = new JLabel("<",JLabel.CENTER);
final JLabel nmonth = new JLabel(">",JLabel.CENTER);
final JLabel nyear = new JLabel(">>",JLabel.CENTER);
currentday = new JLabel(year + " - " + (month < 10 ? 0 : "") + month,JLabel.CENTER);
final JLabel weektitle = new JLabel("Sun Mon Tue Wed Thr Fri Sat",JLabel.CENTER);
contp.add(weektitle);
contp.add(body);
contp.add(bott);
bott.setBounds(10, 175, 150, 20);
body.setBounds(0, 50, 221, 120);
for (int i = 0;i<42;i++){
lm.add(new JLabel(i + "",JLabel.CENTER));
body.add(lm.get(lm.size() - 1));
lm.get(lm.size() -1).setBounds((i % 7) * 31 + 5 , (i / 7) * 20, 31, 15);
}
header.add(pyear);
header.add(pmonth);
header.add(nyear);
header.add(nmonth);
header.add(currentday);
pyear.setBounds(0, 0, 40, 20);
pmonth.setBounds(40, 0, 15, 20);
currentday.setBounds(70, 0, 80, 20);
nmonth.setBounds(170, 0, 15, 20);
nyear.setBounds(185, 0, 40, 20);
weektitle.setBounds(0, 30, 220, 20);
pyear.addMouseListener(new myMouseListener(pyear,-12));
pmonth.addMouseListener(new myMouseListener(pmonth,-1));
nyear.addMouseListener(new myMouseListener(nyear,12));
nmonth.addMouseListener(new myMouseListener(nmonth,1));
body.addMouseMotionListener(new MouseMotionListener(){
@Override
public void mouseDragged(MouseEvent arg0) {}
@Override
public void mouseMoved(MouseEvent arg0) {
if (arg0.getX() < 5 || arg0.getY() > 120) return;
if (currLabel != tday && currLabel != sday) lm.get(currLabel).setBorder(null);
if ((arg0.getY() / 20) * 7 + ((arg0.getX() -5) / 31) == tday) return;
currLabel = (arg0.getY() / 20) * 7 + ((arg0.getX() -5) / 31);
if (currLabel != sday) lm.get((arg0.getY() / 20) * 7 + ((arg0.getX() -5) / 31)).setBorder(BorderFactory.createLineBorder(Color.red));
}
});
body.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getX() < 5 || arg0.getY() > 120) return;
lm.get(sday).setBorder(null);
lm.get(sday).setOpaque(false);
lm.get(sday).setBackground(null);
lm.get((arg0.getY() / 20) * 7 + ((arg0.getX() -5) / 31)).setBorder(BorderFactory.createLineBorder(Color.green));
lm.get((arg0.getY() / 20) * 7 + ((arg0.getX() -5) / 31)).setBackground(Color.gray.brighter());
lm.get((arg0.getY() / 20) * 7 + ((arg0.getX() -5) / 31)).setOpaque(true);
sday = ((arg0.getY() / 20) * 7 + ((arg0.getX() -5) / 31));
if (sday < 7 && lm.get(sday).getForeground().equals(Color.gray.brighter())){
if (month == 1){
bott.setText((year - 1) + " - 12 - " + lm.get(sday).getText() );
}else
bott.setText(year + " - " + ((month - 1) < 10 ? 0 : "") + (month - 1) +" - " + lm.get(sday).getText() );
}
if (sday > 24 && lm.get(sday).getForeground().equals(Color.gray.brighter())){
if (month == 12){
bott.setText((year + 1) + " - 01 - " + lm.get(sday).getText() );
}else
bott.setText(year + " - " + ((month + 1) < 10 ? 0 : "") + (month + 1) +" - " + (lm.get(sday).getText().length() == 1 ? 0 : "") + lm.get(sday).getText());
}
if (lm.get(sday).getForeground().equals(Color.black))
bott.setText(year + " - " + ((month) < 10 ? 0 : "") + (month) +" - " + (lm.get(sday).getText().length() == 1 ? 0 : "") + lm.get(sday).getText());
notStop = false;
if (arg0.getClickCount() == 2) setVisible(false);
}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {
if (currLabel == tday || currLabel == sday) return;
lm.get(currLabel).setBorder(null);
}
@Override
public void mousePressed(MouseEvent arg0) {}
@Override
public void mouseReleased(MouseEvent arg0) {}
});
updateDate();
lm.get((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0))).setBackground(Color.gray);
lm.get((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0))).setOpaque(true);
lm.get((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0))).setBorder(new EdgeBorder(true,true,true,true,1,Color.BLUE.brighter(),2,true));
tday = ((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0)));
bott.setText(year + " - " + ((month) < 10 ? 0 : "") + (month) +" - " + ((day) < 10 ? 0 : "") + day);
this.addWindowListener(new WindowListener(){
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
jt.setText(bott.getText());
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
jt.setText(bott.getText());
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
});
}
private String getTheDay(){
while (notStop){try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
return bott.getText();
}
private void updateDate(){
try {
daycal.setTime(mft.parse(year + "-" + (month < 10 ? 0 : "") + month + "-01"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int day = getDayOfMonth(month > 1 ? year : year -1,month > 1 ? month - 1: 12);
int cday = getDayOfMonth(year,month);
int fday = daycal.get(Calendar.DAY_OF_WEEK);
int d1 = 1;
for (int i = 0;i < 42;i++){
if (((i < fday -1) && fday > 1) || (fday == 1) && i < 7){
((JLabel)(lm.get(i))).setForeground(Color.gray.brighter());
if (fday > 1) ((JLabel)(lm.get(i))).setText((day - fday + i + 2) + "");
if (fday == 1) ((JLabel)(lm.get(i))).setText((day - 7 + i + 1) + "");
}else
if (d1 < cday + 1)
{
((JLabel)(lm.get(i))).setText((d1 ++) + "");
((JLabel)(lm.get(i))).setForeground(Color.black);
}else
{
((JLabel)(lm.get(i))).setForeground(Color.gray.brighter());
((JLabel)(lm.get(i))).setText((d1++ - cday) + "");
}
}
}
private boolean isLeapYear(int year){
return (year%4==0&&year%100!=0)||year%400==0;
}
private int getDayOfMonth(int year,int month){
return isLeapYear(year) && month == 1 ? 29 : dayofmonth[month - 1];
}
final class myMouseListener extends MouseAdapter{
JLabel label;
int amonth;
public myMouseListener(final JLabel label,int month){
this.label = label;
this.amonth = month;
}
public void mouseEntered(java.awt.event.MouseEvent me) {
label.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
label.setForeground(java.awt.Color.RED);
}
public void mouseExited(java.awt.event.MouseEvent me) {
label.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
label.setForeground(java.awt.Color.BLACK);
}
public void mousePressed(java.awt.event.MouseEvent me) {
label.setForeground(java.awt.Color.WHITE);
lm.get(day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1).setOpaque(false);
year = month + this.amonth <= 0 ? year-1 : (month + this.amonth > 12 ? year+1 : year);
month = (month + this.amonth == 0 ? 12 : (month + this.amonth == 13) ? 1 : month + (this.amonth == -1 || this.amonth == 1 ? this.amonth : 0));
currentday.setText(year + " - " + (month < 10 ? 0 : "") + month);
if (year == tyear && month == tmonth)
try {
day = thisday;
cal.setTime(mft.parse(year + "-" + (month < 10 ? 0 : "") + month + "-" + (day < 10 ? 0 :"") + day));
} catch (ParseException e) {
//
e.printStackTrace();
}
else
try {
day = 1;
cal.setTime(mft.parse(year + "-" + (month < 10 ? 0 : "") + month + "-01"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lm.get(tday).setOpaque(false);
lm.get(tday).setBorder(null);
lm.get(sday).setBorder(null);
lm.get(sday).setOpaque(false);
lm.get(sday).setBackground(null);
if (year == tyear && month == tmonth){
lm.get((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0))).setBackground(Color.gray);
lm.get((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0))).setOpaque(true);
lm.get((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0))).setBorder(new EdgeBorder(true,true,true,true,1,Color.BLUE.brighter(),2,true));
}else{
lm.get((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0))).setBackground(Color.gray);
lm.get((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0))).setOpaque(true);
}
tday = ((day -1 + cal.get(Calendar.DAY_OF_WEEK) - 1 + (cal.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : 0)));
updateDate();
}
public void mouseReleased(java.awt.event.MouseEvent me) {
label.setForeground(java.awt.Color.BLACK);
}
}
public static void main(String[] args){
String ss = new datePicker().getTheDay();
System.out.print(ss);
}
}