/** DateChooser.java
*
* Created on 2007年8月20日, 下午6:07
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.*/importjava.awt.BasicStroke;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Component;importjava.awt.Cursor;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.GridLayout;importjava.awt.Point;importjava.awt.Polygon;importjava.awt.Stroke;importjava.awt.Toolkit;importjava.awt.event.FocusEvent;importjava.awt.event.FocusListener;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.awt.event.MouseMotionListener;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.Comparator;importjava.util.Date;importjava.util.List;importjavax.swing.BorderFactory;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.Popup;importjavax.swing.PopupFactory;importjavax.swing.SwingUtilities;importjavax.swing.event.AncestorEvent;importjavax.swing.event.AncestorListener;/***
*@authorhadeslee*/publicclassDateChooserextendsJPanel{privateDate initDate;privateCalendar now=Calendar.getInstance();privateCalendar select;privateJPanel monthPanel;//月历privateJP1 jp1;//四块面板,组成privateJP2 jp2;privateJP3 jp3;privateJP4 jp4;privateFont font=newFont("宋体",Font.PLAIN,12);privatefinalLabelManager lm=newLabelManager();privateJLabel showDate;//,toSelect;privateSimpleDateFormat sdf=newSimpleDateFormat("yyyy年MM月dd日");privatebooleanisShow=false;privatePopup pop;/*** Creates a new instance of DateChooser*/publicDateChooser() {this(newDate());
}publicDateChooser(Date date){
initDate=date;
select=Calendar.getInstance();
select.setTime(initDate);
initPanel();
initLabel();
}publicvoidsetEnabled(booleanb){super.setEnabled(b);
showDate.setEnabled(b);
}/***得到当前选择框的日期*/publicDate getDate(){returnselect.getTime();
}//根据初始化的日期,初始化面板privatevoidinitPanel(){
monthPanel=newJPanel(newBorderLayout());
monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
JPanel up=newJPanel(newBorderLayout());
up.add(jp1=newJP1(),BorderLayout.NORTH);
up.add(jp2=newJP2(),BorderLayout.CENTER);
monthPanel.add(jp3=newJP3(),BorderLayout.CENTER);
monthPanel.add(up,BorderLayout.NORTH);
monthPanel.add(jp4=newJP4(),BorderLayout.SOUTH);this.addAncestorListener(newAncestorListener(){publicvoidancestorAdded(AncestorEvent event) {
}publicvoidancestorRemoved(AncestorEvent event) {
}//只要祖先组件一移动,马上就让popup消失publicvoidancestorMoved(AncestorEvent event) {
hidePanel();
}
});
}//初始化标签privatevoidinitLabel(){
showDate=newJLabel(sdf.format(initDate));
showDate.setRequestFocusEnabled(true);
showDate.addMouseListener(newMouseAdapter(){publicvoidmousePressed(MouseEvent me){
showDate.requestFocusInWindow();
}
});//toSelect=new JLabel(sdf.format(initDate));//toSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK));//toSelect.setRequestFocusEnabled(true);this.setBackground(Color.WHITE);this.add(showDate,BorderLayout.CENTER);//this.add(toSelect,BorderLayout.EAST);this.setPreferredSize(newDimension(90,25));this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
showDate.addMouseListener(newMouseAdapter(){publicvoidmouseEntered(MouseEvent me){if(showDate.isEnabled()){
showDate.setCursor(newCursor(Cursor.HAND_CURSOR));
showDate.setForeground(Color.RED);
}
}publicvoidmouseExited(MouseEvent me){if(showDate.isEnabled()){
showDate.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
showDate.setForeground(Color.BLACK);
}
}publicvoidmousePressed(MouseEvent me){if(showDate.isEnabled()){
showDate.setForeground(Color.CYAN);if(isShow){
hidePanel();
}else{
showPanel(showDate);
}
}
}publicvoidmouseReleased(MouseEvent me){if(showDate.isEnabled()){
showDate.setForeground(Color.BLACK);
}
}
});
showDate.addFocusListener(newFocusListener(){publicvoidfocusLost(FocusEvent e){
hidePanel();
}publicvoidfocusGained(FocusEvent e){
}
});
}//根据新的日期刷新privatevoidrefresh(){
jp1.updateDate();
jp3.updateDate();
SwingUtilities.updateComponentTreeUI(this);
}//提交日期privatevoidcommit(){
System.out.println("选中的日期是:"+sdf.format(select.getTime()));
showDate.setText(sdf.format(select.getTime()));
hidePanel();
}privatevoidhidePanel(){if(pop!=null){
isShow=false;
pop.hide();
pop=null;
}
}privatevoidshowPanel(Component owner){if(pop!=null){
pop.hide();
}
Point show=newPoint(0,showDate.getHeight());
SwingUtilities.convertPointToScreen(show,showDate);
Dimension size=Toolkit.getDefaultToolkit().getScreenSize();intx=show.x;inty=show.y;if(x<0){
x=0;
}if(x>size.width-295){
x=size.width-295;
}if(y
}else{
y-=188;
}
pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);
pop.show();
isShow=true;
}privateclassJP1extendsJPanel{
JLabel left,right,center;publicJP1(){super(newBorderLayout());this.setBackground(newColor(160,185,215));
initJP1();
}privatevoidinitJP1(){
left=newJLabel("<
left.setToolTipText("上一月");
right=newJLabel(">>",JLabel.CENTER);
right.setToolTipText("下一月");
left.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
right.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
center=newJLabel("",JLabel.CENTER);
updateDate();this.add(left,BorderLayout.WEST);this.add(center,BorderLayout.CENTER);this.add(right,BorderLayout.EAST);this.setPreferredSize(newDimension(295,25));
left.addMouseListener(newMouseAdapter(){publicvoidmouseEntered(MouseEvent me){
left.setCursor(newCursor(Cursor.HAND_CURSOR));
left.setForeground(Color.RED);
}publicvoidmouseExited(MouseEvent me){
left.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
left.setForeground(Color.BLACK);
}publicvoidmousePressed(MouseEvent me){
select.add(Calendar.MONTH,-1);
left.setForeground(Color.WHITE);
refresh();
}publicvoidmouseReleased(MouseEvent me){
left.setForeground(Color.BLACK);
}
});
right.addMouseListener(newMouseAdapter(){publicvoidmouseEntered(MouseEvent me){
right.setCursor(newCursor(Cursor.HAND_CURSOR));
right.setForeground(Color.RED);
}publicvoidmouseExited(MouseEvent me){
right.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
right.setForeground(Color.BLACK);
}publicvoidmousePressed(MouseEvent me){
select.add(Calendar.MONTH,1);
right.setForeground(Color.WHITE);
refresh();
}publicvoidmouseReleased(MouseEvent me){
right.setForeground(Color.BLACK);
}
});
}privatevoidupdateDate(){
center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月");
}
}privateclassJP2extendsJPanel{publicJP2(){this.setPreferredSize(newDimension(295,20));
}protectedvoidpaintComponent(Graphics g){
g.setFont(font);
g.drawString("星期日 星期一 星期二 星期三 星期四 星期五 星期六",5,10);
g.drawLine(0,15,getWidth(),15);
}
}privateclassJP3extendsJPanel{publicJP3(){super(newGridLayout(6,7));this.setPreferredSize(newDimension(295,100));
initJP3();
}privatevoidinitJP3(){
updateDate();
}publicvoidupdateDate(){this.removeAll();
lm.clear();
Date temp=select.getTime();
Calendar select=Calendar.getInstance();
select.setTime(temp);
select.set(Calendar.DAY_OF_MONTH,1);intindex=select.get(Calendar.DAY_OF_WEEK);intsum=(index==1?8:index);
select.add(Calendar.DAY_OF_MONTH,0-sum);for(inti=0;i<42;i++){
select.add(Calendar.DAY_OF_MONTH,1);
lm.addLabel(newMyLabel(select.get(Calendar.YEAR),
select.get(Calendar.MONTH),select.get(Calendar.DAY_OF_MONTH)));
}for(MyLabel my:lm.getLabels()){this.add(my);
}
select.setTime(temp);
}
}privateclassMyLabelextendsJLabelimplementsComparator,
MouseListener,MouseMotionListener{privateintyear,month,day;privatebooleanisSelected;publicMyLabel(intyear,intmonth,intday){super(""+day,JLabel.CENTER);this.year=year;this.day=day;this.month=month;this.addMouseListener(this);this.addMouseMotionListener(this);this.setFont(font);if(month==select.get(Calendar.MONTH)){this.setForeground(Color.BLACK);
}else{this.setForeground(Color.LIGHT_GRAY);
}if(day==select.get(Calendar.DAY_OF_MONTH)){this.setBackground(newColor(160,185,215));
}else{this.setBackground(Color.WHITE);
}
}publicbooleangetIsSelected(){returnisSelected;
}publicvoidsetSelected(booleanb,booleanisDrag){
isSelected=b;if(b&&!isDrag){inttemp=select.get(Calendar.MONTH);
select.set(year,month,day);if(temp==month){
SwingUtilities.updateComponentTreeUI(jp3);
}else{
refresh();
}
}this.repaint();
}protectedvoidpaintComponent(Graphics g){if(day==select.get(Calendar.DAY_OF_MONTH)&&month==select.get(Calendar.MONTH)){//如果当前日期是选择日期,则高亮显示g.setColor(newColor(160,185,215));
g.fillRect(0,0,getWidth(),getHeight());
}if(year==now.get(Calendar.YEAR)&&month==now.get(Calendar.MONTH)&&day==now.get(Calendar.DAY_OF_MONTH)){//如果日期和当前日期一样,则用红框Graphics2D gd=(Graphics2D)g;
gd.setColor(Color.RED);
Polygon p=newPolygon();
p.addPoint(0,0);
p.addPoint(getWidth()-1,0);
p.addPoint(getWidth()-1,getHeight()-1);
p.addPoint(0,getHeight()-1);
gd.drawPolygon(p);
}if(isSelected){//如果被选中了就画出一个虚线框出来Stroke s=newBasicStroke(1.0f,BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_BEVEL,1.0f,newfloat[]{2.0f,2.0f},1.0f);
Graphics2D gd=(Graphics2D)g;
gd.setStroke(s);
gd.setColor(Color.BLACK);
Polygon p=newPolygon();
p.addPoint(0,0);
p.addPoint(getWidth()-1,0);
p.addPoint(getWidth()-1,getHeight()-1);
p.addPoint(0,getHeight()-1);
gd.drawPolygon(p);
}super.paintComponent(g);
}publicbooleancontains(Point p){returnthis.getBounds().contains(p);
}privatevoidupdate(){
repaint();
}publicvoidmouseClicked(MouseEvent e) {
}publicvoidmousePressed(MouseEvent e) {
isSelected=true;
update();
}publicvoidmouseReleased(MouseEvent e) {
Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
lm.setSelect(p,false);
commit();
}publicvoidmouseEntered(MouseEvent e) {
}publicvoidmouseExited(MouseEvent e) {
}publicvoidmouseDragged(MouseEvent e) {
Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
lm.setSelect(p,true);
}publicvoidmouseMoved(MouseEvent e) {
}publicintcompare(MyLabel o1, MyLabel o2) {
Calendar c1=Calendar.getInstance();
c1.set(o1.year,o2.month,o1.day);
Calendar c2=Calendar.getInstance();
c2.set(o2.year,o2.month,o2.day);returnc1.compareTo(c2);
}
}privateclassLabelManager{privateListlist;publicLabelManager(){
list=newArrayList();
}publicListgetLabels(){returnlist;
}publicvoidaddLabel(MyLabel my){
list.add(my);
}publicvoidclear(){
list.clear();
}publicvoidsetSelect(MyLabel my,booleanb){for(MyLabel m:list){if(m.equals(my)){
m.setSelected(true,b);
}else{
m.setSelected(false,b);
}
}
}publicvoidsetSelect(Point p,booleanb){//如果是拖动,则要优化一下,以提高效率if(b){//表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和//将要显示的标签找到了就可以了booleanfindPrevious=false,findNext=false;for(MyLabel m:list){if(m.contains(p)){
findNext=true;if(m.getIsSelected()){
findPrevious=true;
}else{
m.setSelected(true,b);
}
}elseif(m.getIsSelected()){
findPrevious=true;
m.setSelected(false,b);
}if(findPrevious&&findNext){return;
}
}
}else{
MyLabel temp=null;for(MyLabel m:list){if(m.contains(p)){
temp=m;
}elseif(m.getIsSelected()){
m.setSelected(false,b);
}
}if(temp!=null){
temp.setSelected(true,b);
}
}
}
}privateclassJP4extendsJPanel{publicJP4(){super(newBorderLayout());this.setPreferredSize(newDimension(295,20));this.setBackground(newColor(160,185,215));
SimpleDateFormat sdf=newSimpleDateFormat("yyyy年MM月dd日");finalJLabel jl=newJLabel("今天:"+sdf.format(newDate()));
jl.setToolTipText("点击回到今天日期");this.add(jl,BorderLayout.CENTER);
jl.addMouseListener(newMouseAdapter(){publicvoidmouseEntered(MouseEvent me){
jl.setCursor(newCursor(Cursor.HAND_CURSOR));
jl.setForeground(Color.RED);
}publicvoidmouseExited(MouseEvent me){
jl.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
jl.setForeground(Color.BLACK);
}publicvoidmousePressed(MouseEvent me){
jl.setForeground(Color.WHITE);
select.setTime(newDate());
refresh();
commit();
}publicvoidmouseReleased(MouseEvent me){
jl.setForeground(Color.BLACK);
}
});
}
}publicstaticvoidmain(String[] args) {finalDateChooser mp=newDateChooser();
JFrame jf=newJFrame("test");
jf.add(mp,BorderLayout.CENTER);
jf.add(newJButton("测试用的"),BorderLayout.NORTH);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}