Java 日期选择器Word文档格式.docx
《Java 日期选择器Word文档格式.docx》由会员分享,可在线阅读,更多相关《Java 日期选择器Word文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
![Java 日期选择器Word文档格式.docx](https://file1.bdocx.com/fileroot1/2023-1/29/284b643f-022d-45f6-81cf-468c043fa641/284b643f-022d-45f6-81cf-468c043fa6411.gif)
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;
publicclassDateChooserextendsJPanel{
privateDateinitDate;
privateCalendarnow=Calendar.getInstance();
privateCalendarselect;
privateJPanelmonthPanel;
//月历
privateJP1jp1;
//四块面板,组成
privateJP2jp2;
privateJP3jp3;
privateJP4jp4;
privateFontfont=newFont("
宋体"
Font.PLAIN,12);
privatefinalLabelManagerlm=newLabelManager();
privateJLabelshowDate;
//,toSelect;
privateSimpleDateFormatsdf=newSimpleDateFormat("
yyyy年MM月dd日"
);
privatebooleanisShow=false;
privatePopuppop;
publicDateChooser(){
this(newDate());
}
publicDateChooser(Datedate){
initDate=date;
select=Calendar.getInstance();
select.setTime(initDate);
initPanel();
initLabel();
publicvoidsetEnabled(booleanb){
super.setEnabled(b);
showDate.setEnabled(b);
publicDategetDate(){
returnselect.getTime();
//根据初始化的日期,初始化面板
privatevoidinitPanel(){
monthPanel=newJPanel(newBorderLayout());
monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
JPanelup=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(AncestorEventevent){
publicvoidancestorRemoved(AncestorEventevent){
//只要祖先组件一移动,马上就让popup消失
publicvoidancestorMoved(AncestorEventevent){
hidePanel();
});
//初始化标签
privatevoidinitLabel(){
showDate=newJLabel(sdf.format(initDate));
showDate.setRequestFocusEnabled(true);
showDate.addMouseListener(newMouseAdapter(){
publicvoidmousePressed(MouseEventme){
showDate.requestFocusInWindow();
//
toSelect=newJLabel(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));
publicvoidmouseEntered(MouseEventme){
if(showDate.isEnabled()){
showDate.setCursor(newCursor(Cursor.HAND_CURSOR));
showDate.setForeground(Color.RED);
publicvoidmouseExited(MouseEventme){
showDate.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
showDate.setForeground(Color.BLACK);
showDate.setForeground(Color.CYAN);
if(isShow){
}else{
showPanel(showDate);
publicvoidmouseReleased(MouseEventme){
showDate.addFocusListener(newFocusListener(){
publicvoidfocusLost(FocusEvente){
publicvoidfocusGained(FocusEvente){
//根据新的日期刷新
privatevoidrefresh(){
jp1.updateDate();
jp3.updateDate();
SwingUtilities.updateComponentTreeUI(this);
//提交日期
privatevoidcommit(){
System.out.println("
选中的日期是:
"
+sdf.format(select.getTime()));
showDate.setText(sdf.format(select.getTime()));
privatevoidhidePanel(){
if(pop!
=null){
isShow=false;
pop.hide();
pop=null;
privatevoidshowPanel(Componentowner){
Pointshow=newPoint(0,showDate.getHeight());
SwingUtilities.convertPointToScreen(show,showDate);
Dimensionsize=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
y-=188;
pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);
pop.show();
isShow=true;
privateclassJP1extendsJPanel{
JLabelleft,right,center;
publicJP1(){
super(newBorderLayout());
this.setBackground(newColor(160,185,215));
initJP1();
privatevoidinitJP1(){
left=newJLabel("
<
<
"
JLabel.CENTER);
left.setToolTipText("
上一月"
right=newJLabel("
>
>
right.setToolTipText("
下一月"
left.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
right.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
center=newJLabel("
updateDate();
this.add(left,BorderLayout.WEST);
this.add(center,BorderLayout.CENTER);
this.add(right,BorderLayout.EAST);
this.setPreferredSize(newDimension(295,25));
left.addMouseListener(newMouseAdapter(){
left.setCursor(newCursor(Cursor.HAND_CURSOR));
left.setForeground(Color.RED);
left.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
left.setForeground(Color.BLACK);
select.add(Calendar.MONTH,-1);
left.setForeground(Color.WHITE);
refresh();
right.addMouseListener(newMouseAdapter(){
right.setCursor(newCursor(Cursor.HAND_CURSOR));
right.setForeground(Color.RED);
right.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
right.setForeground(Color.BLACK);
select.add(Calendar.MONTH,1);
right.setForeground(Color.WHITE);
privatevoidupdateDate(){
center.setText(select.get(Calendar.YEAR)+"
年"
+(select.get(Calendar.MONTH)+1)+"
月"
privateclassJP2extendsJPanel{
publicJP2(){
this.setPreferredSize(newDimension(295,20));
protectedvoidpaintComponent(Graphicsg){
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(){
publicvoidupdateDate(){
this.removeAll();
lm.clear();
Datetemp=select.getTime();
Calendarselect=Calendar.getInstance();
select.setTime(temp);
select.set(Calendar.DAY_OF_MONTH,1);
intindex=select.get(Calenda