JAVA实现日期选择控件Word格式.docx
《JAVA实现日期选择控件Word格式.docx》由会员分享,可在线阅读,更多相关《JAVA实现日期选择控件Word格式.docx(9页珍藏版)》请在冰点文库上搜索。
![JAVA实现日期选择控件Word格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/11/b08b1085-2539-4635-ba9f-ed0a93d60c4b/b08b1085-2539-4635-ba9f-ed0a93d60c4b1.gif)
importjavax.swing.JSpinner;
importjavax.swing.JSpinner.NumberEditor;
importjavax.swing.SpinnerNumberModel;
importjavax.swing.SwingUtilities;
importjavax.swing.SwingConstants;
importjavax.swing.event.ChangeListener;
importjavax.swing.event.ChangeEvent;
importjavax.swing.border.LineBorder;
publicclassDateChooserJButtonextendsJButton{
privateDateChooserdateChooser=null;
privateStringpreLabel="
"
;
publicDateChooserJButton(){
this(getNowDate());
}
publicDateChooserJButton(SimpleDateFormatdf,StringdateString){
this();
setText(df,dateString);
publicDateChooserJButton(Datedate){
this("
date);
publicDateChooserJButton(StringpreLabel,Datedate){
if(preLabel!
=null)
this.preLabel=preLabel;
setDate(date);
setBorder(null);
setCursor(newCursor(Cursor.HAND_CURSOR));
super.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(dateChooser==null)
dateChooser=newDateChooser();
Pointp=getLocationOnScreen();
p.y=p.y+30;
dateChooser.showDateChooser(p);
});
privatestaticDategetNowDate(){
returnCalendar.getInstance().getTime();
privatestaticSimpleDateFormatgetDefaultDateFormat(){
returnnewSimpleDateFormat("
yyyy-MM-ddHH:
MM:
ss"
);
//按钮显示的日期格式
//覆盖父类的方法
publicvoidsetText(Strings){
Datedate;
try{
date=getDefaultDateFormat().parse(s);
}catch(ParseExceptione){
date=getNowDate();
publicvoidsetText(SimpleDateFormatdf,Strings){
date=df.parse(s);
publicvoidsetDate(Datedate){
super.setText(preLabel+getDefaultDateFormat().format(date));
publicDategetDate(){
StringdateString=getText().substring(preLabel.length());
returngetDefaultDateFormat().parse(dateString);
returngetNowDate();
//覆盖父类的方法使之无效
publicvoidaddActionListener(ActionListenerlistener){
privateclassDateChooserextendsJPanelimplementsActionListener,
ChangeListener{
intstartYear=1980;
//默认【最小】显示年份
intlastYear=2050;
//默认【最大】显示年份
intwidth=200;
//界面宽度
intheight=200;
//界面高度
ColorbackGroundColor=Color.gray;
//底色
//月历表格配色----------------//
ColorpalletTableColor=Color.white;
//日历表底色
ColortodayBackColor=Color.orange;
//今天背景色
ColorweekFontColor=Color.blue;
//星期文字色
ColordateFontColor=Color.black;
//日期文字色
ColorweekendFontColor=Color.red;
//周末文字色
//控制条配色------------------//
ColorcontrolLineColor=Color.pink;
//控制条底色
ColorcontrolTextColor=Color.white;
//控制条标签文字色
ColorrbFontColor=Color.white;
//RoundBox文字色
ColorrbBorderColor=Color.red;
//RoundBox边框色
ColorrbButtonColor=Color.pink;
//RoundBox按钮色
ColorrbBtFontColor=Color.red;
//RoundBox按钮文字色
JDialogdialog;
JSpinneryearSpin;
JSpinnermonthSpin;
JSpinnerhourSpin;
JButton[][]daysButton=newJButton[6][7];
DateChooser(){
setLayout(newBorderLayout());
setBorder(newLineBorder(backGroundColor,2));
setBackground(backGroundColor);
JPaneltopYearAndMonth=createYearAndMonthPanal();
add(topYearAndMonth,BorderLayout.NORTH);
JPanelcenterWeekAndDay=createWeekAndDayPanal();
add(centerWeekAndDay,BorderLayout.CENTER);
privateJPanelcreateYearAndMonthPanal(){
Calendarc=getCalendar();
intcurrentYear=c.get(Calendar.YEAR);
intcurrentMonth=c.get(Calendar.MONTH)+1;
intcurrentHour=c.get(Calendar.HOUR_OF_DAY);
JPanelresult=newJPanel();
result.setLayout(newFlowLayout());
result.setBackground(controlLineColor);
yearSpin=newJSpinner(newSpinnerNumberModel(currentYear,
startYear,lastYear,1));
yearSpin.setPreferredSize(newDimension(48,20));
yearSpin.setName("
Year"
yearSpin.setEditor(newJSpinner.NumberEditor(yearSpin,"
####"
));
yearSpin.addChangeListener(this);
result.add(yearSpin);
JLabelyearLabel=newJLabel("
年"
yearLabel.setForeground(controlTextColor);
result.add(yearLabel);
monthSpin=newJSpinner(newSpinnerNumberModel(currentMonth,1,
12,1));
monthSpin.setPreferredSize(newDimension(35,20));
monthSpin.setName("
Month"
monthSpin.addChangeListener(this);
result.add(monthSpin);
JLabelmonthLabel=newJLabel("
月"
monthLabel.setForeground(controlTextColor);
result.add(monthLabel);
hourSpin=newJSpinner(newSpinnerNumberModel(currentHour,0,23,
1));
hourSpin.setPreferredSize(newDimension(35,20));
hourSpin.setName("
Hour"
hourSpin.addChangeListener(this);
result.add(hourSpin);
JLabelhourLabel=newJLabel("
时"
hourLabel.setForeground(controlTextColor);
result.add(hourLabel);
returnresult;
privateJPanelcreateWeekAndDayPanal(){
Stringcolname[]={"
日"
"
一"
二"
三"
四"
五"
六"
};
//设置固定字体,以免调用环境改变影响界面美观
result.setFont(newFont("
宋体"
Font.PLAIN,12));
result.setLayout(newGridLayout(7,7));
result.setBackground(Color.white);
JLabelcell;
for(inti=0;
i<
7;
i++){
cell=newJLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.RIGHT);
if(i==0||i==6)
cell.setForeground(weekendFontColor);
else
cell.setForeground(weekFontColor);
result.add(cell);
intactionCommandId=0;
6;
i++)
for(intj=0;
j<
j++){
JButtonnumberButton=newJButton();
numberButton.setBorder(null);
numberButton.setHorizontalAlignment(SwingConstants.RIGHT);
numberButton.setActionCommand(String
.valueOf(actionCommandId));
numberButton.addActionListener(this);
numberButton.setBackground(palletTableColor);
numberButton.setForeground(dateFontColor);
if(j==0||j==6)
numberButton.setForeground(weekendFontColor);
daysButton[i][j]=numberButton;
result.add(numberButton);
actionCommandId++;
privateJDialogcreateDialog(Frameowner){
JDialogresult=newJDialog(owner,"
日期时间选择"
true);
result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
result.getContentPane().add(this,BorderLayout.CENTER);
result.pack();
result.setSize(width,height);
voidshowDateChooser(Pointposition){
Frameowner=(Frame)SwingUtilities
.getWindowAncestor(DateChooserJButton.this);
if(dialog==null||dialog.getOwner()!
=owner)
dialog=createDialog(owner);
dialog.setLocation(getAppropriateLocation(owner,position));
flushWeekAndDay();
dialog.show();
PointgetAppropriateLocation(Frameowner,Pointposition){
Pointresult=newPoint(position);
Pointp=owner.getLocation();
intoffsetX=(position.x+width)-(p.x+owner.getWidth());
intoffsetY=(position.y+height)-(p.y+owner.getHeight());
if(offsetX>
0){
result.x-=offsetX;
if(offsetY>
result.y-=offsetY;
privateCalendargetCalendar(){
Calendarresult=Calendar.getInstance();
result.setTime(getDate());
privateintgetSelectedYear(){
return((Integer)yearSpin.getValue()).intValue();
privateintgetSelectedMonth(){
return((Integer)monthSpin.getValue()).intValue();
privateintgetSelectedHour(){
return((Integer)hourSpin.getValue()).intValue();
privatevoiddayColorUpdate(booleanisOldDay){
intday=c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH,1);
intactionCommandId=day-2+c.get(Calendar.DAY_OF_WEEK);
inti=actionCommandId/7;
intj=actionCommandId%7;
if(isOldDay)
daysButton[i][j].setForeground(dateFontColor);
daysButton[i][j].setForeground(todayBackColor);
privatevoidflushWeekAndDay(){
intmaxDayNo=c.getActualMaximum(Calendar.DAY_OF_MONTH);
intdayNo=2-c.get(Calendar.DAY_OF_WEEK);
Strings="
if(dayNo>
=1&
&
dayNo<
=maxDayNo)
s=String.valueOf(dayNo);
daysButton[i][j].setText(s);
dayNo++;
dayColorUpdate(false);
publicvoidstateChanged(ChangeEvente){
JSpinnersource=(JSpinner)e.getSource();
if(source.getName().equals("
)){
c.set(Calendar.HOUR_OF_DAY,getSelectedHour());
setDate(c.getTime());
return;
dayColorUpdate(true);
))
c.set(Calendar.YEAR,getSelectedYea