下面是我用SWT写的一个日历控件,主要是用于让用记选择日期,可以通过按钮 上一年、下一年、上一月 和 下一月 调整到要切换的月份,也可以通过改 年 和 月 的下拉框中的选择项来切换到指定年月,还可以直接在 年 和 月 的下拉框中输入要切换到的年月后按 回车键 切换到指定年月。运行后的效果如下图所示:
源代码如下:
-
packagecom.hmw.tools;
-
importjava.util.Calendar;
-
importorg.eclipse.swt.SWT;
-
importorg.eclipse.swt.custom.CLabel;
-
importorg.eclipse.swt.events.KeyAdapter;
-
importorg.eclipse.swt.events.KeyEvent;
-
importorg.eclipse.swt.events.KeyListener;
-
importorg.eclipse.swt.events.MouseAdapter;
-
importorg.eclipse.swt.events.MouseEvent;
-
importorg.eclipse.swt.events.MouseListener;
-
importorg.eclipse.swt.events.SelectionAdapter;
-
importorg.eclipse.swt.events.SelectionEvent;
-
importorg.eclipse.swt.events.SelectionListener;
-
importorg.eclipse.swt.graphics.Color;
-
importorg.eclipse.swt.graphics.Rectangle;
-
importorg.eclipse.swt.layout.GridData;
-
importorg.eclipse.swt.layout.GridLayout;
-
importorg.eclipse.swt.widgets.Button;
-
importorg.eclipse.swt.widgets.Combo;
-
importorg.eclipse.swt.widgets.Display;
-
importorg.eclipse.swt.widgets.Shell;
-
-
-
publicclassCalendarWidget{
-
-
privateDisplaydisplay=null;
-
-
privateShellshell=null;
-
-
privatestaticfinalintSHELLWIDTH=380;
-
-
privatestaticfinalintSHELLHEIGHT=280;
-
-
privatestaticfinalintGRIDDATAHINT=20;
-
-
privatestaticfinalStringTOOPTIPTEXT_PREVIOUSYEAR="上一年";
-
-
privatestaticfinalStringTOOPTIPTEXT_NEXTYEAR="下一年";
-
-
privatestaticfinalStringTOOPTIPTEXT_PREVIOUSMONTH="上一月";
-
-
privatestaticfinalStringTOOPTIPTEXT_NEXTMONTH="下一月";
-
-
privatestaticfinalString[]weekdays={"星期日","星期一","星期二","星期三",
-
"星期四","星期五","星期六"};
-
-
privatestaticfinalinthalfYearItemCount=20;
-
-
privatestaticfinalString[]years=newString[halfYearItemCount*2+1];
-
-
privatestaticfinalString[]months={"1","2","3","4","5","6","7","8","9","10","11","12",};
-
-
privateStringselectedDate="";
-
- Combocbo_year;
-
- Combocbo_month;
-
-
privatefinalCLabel[]clbl_days=newCLabel[42];
-
-
privateColorCOLOR_SHELL_BACKGROUND=null;
-
-
privateColorCOLOR_CLBLWEEKDAY_BACKGROUND=null;
-
-
privateColorCOLOR_SYSTEM_WHITE=null;
-
-
privateColorCOLOR_SYSTEM_BLUE=null;
-
publicCalendarWidget(Shellparent){
- RectangleparentRec=parent.getBounds();
- display=parent.getDisplay();
-
COLOR_SHELL_BACKGROUND=newColor(display,219,235,250);
-
COLOR_CLBLWEEKDAY_BACKGROUND=newColor(display,64,128,128);
- COLOR_SYSTEM_WHITE=display.getSystemColor(SWT.COLOR_WHITE);
- COLOR_SYSTEM_BLUE=display.getSystemColor(SWT.COLOR_BLUE);
-
shell=newShell(parent,SWT.CLOSE|SWT.APPLICATION_MODAL);
-
shell.setText("选择日期");
-
-
shell.setBounds(parentRec.x+(parentRec.width-SHELLWIDTH)/2,
-
parentRec.y+(parentRec.height-SHELLHEIGHT)/2,SHELLWIDTH,
- SHELLHEIGHT);
-
GridLayoutgridLayout=newGridLayout();
-
gridLayout.numColumns=7;
-
gridLayout.makeColumnsEqualWidth=true;
- shell.setLayout(gridLayout);
- shell.setBackground(COLOR_SHELL_BACKGROUND);
-
-
Buttonbtn_yearPrevious=newButton(shell,SWT.ARROW|SWT.UP);
-
btn_yearPrevious.setLayoutData(newGridData(SWT.FILL,SWT.CENTER,true,false));
- btn_yearPrevious.setToolTipText(TOOPTIPTEXT_PREVIOUSYEAR);
- btn_yearPrevious.addSelectionListener(btnSelectionListener);
-
-
Buttonbtn_monthPrevious=newButton(shell,SWT.ARROW|SWT.LEFT);
-
btn_monthPrevious.setLayoutData(newGridData(SWT.FILL,SWT.CENTER,true,false));
- btn_monthPrevious.setToolTipText(TOOPTIPTEXT_PREVIOUSMONTH);
- btn_monthPrevious.addSelectionListener(btnSelectionListener);
-
cbo_year=newCombo(shell,SWT.DROP_DOWN);
-
cbo_year.setLayoutData(newGridData(SWT.FILL,SWT.CENTER,true,false));
- cbo_year.addSelectionListener(cboSelectionListener);
- cbo_year.addKeyListener(cboKeyListener);
-
CLabelclabel=newCLabel(shell,SWT.CENTER|SWT.SHADOW_OUT);
- clabel.setBackground(COLOR_SYSTEM_WHITE);
-
clabel.setLayoutData(newGridData(SWT.FILL,SWT.CENTER,true,false));
-
clabel.setText("-");
-
cbo_month=newCombo(shell,SWT.DROP_DOWN);
- cbo_month.setItems(months);
- cbo_month.select(Calendar.getInstance().get(Calendar.MONTH));
-
cbo_month.setLayoutData(newGridData(SWT.FILL,SWT.CENTER,true,false));
- cbo_month.addSelectionListener(cboSelectionListener);
- cbo_month.addKeyListener(cboKeyListener);
-
-
Buttonbtn_monthNext=newButton(shell,SWT.ARROW|SWT.RIGHT);
-
btn_monthNext.setLayoutData(newGridData(SWT.FILL,SWT.CENTER,true,false));
- btn_monthNext.setToolTipText(TOOPTIPTEXT_NEXTMONTH);
- btn_monthNext.addSelectionListener(btnSelectionListener);
-
-
Buttonbtn_yearNext=newButton(shell,SWT.ARROW|SWT.DOWN);
-
btn_yearNext.setLayoutData(newGridData(SWT.FILL,SWT.CENTER,true,false));
- btn_yearNext.setToolTipText(TOOPTIPTEXT_NEXTYEAR);
- btn_yearNext.addSelectionListener(btnSelectionListener);
-
GridDatagridData_1=null;
-
-
for(inti=0;i<weekdays.length;i++){
-
CLabelclbl_weekDay=newCLabel(shell,SWT.CENTER|SWT.SHADOW_OUT);
- clbl_weekDay.setForeground(COLOR_SYSTEM_WHITE);
- clbl_weekDay.setBackground(COLOR_CLBLWEEKDAY_BACKGROUND);
-
gridData_1=newGridData(SWT.FILL,SWT.CENTER,true,false);
- gridData_1.widthHint=GRIDDATAHINT;
- gridData_1.heightHint=GRIDDATAHINT;
- clbl_weekDay.setLayoutData(gridData_1);
- clbl_weekDay.setText(weekdays[i]);
- }
-
-
for(inti=0;i<clbl_days.length;i++){
-
clbl_days[i]=newCLabel(shell,SWT.FLAT|SWT.CENTER);
-
clbl_days[i].setLayoutData(newGridData(GridData.FILL_HORIZONTAL
- |GridData.FILL_VERTICAL));
- clbl_days[i].setCursor(display.getSystemCursor(SWT.CURSOR_HAND));
- clbl_days[i].addMouseListener(clblMouseListener);
- }
- }
-
-
-
privatevoidsetCboYearItems(intmiddleYear){
-
intselectIndex=halfYearItemCount;
-
-
if(middleYear<halfYearItemCount){
- selectIndex=middleYear;
- middleYear=halfYearItemCount;
- }
-
intindex=0;
-
for(inti=middleYear-halfYearItemCount;i<=middleYear+halfYearItemCount;i++){
-
years[index++]=""+i;
- }
- cbo_year.setItems(years);
- cbo_year.select(selectIndex);
- }
-
-
-
privateintgetDayCountOfMonth(intyear,intmonth){
- Calendarcal=Calendar.getInstance();
-
cal.set(year,month-1,1);
- Calendarcal2=(Calendar)cal.clone();
-
cal2.add(Calendar.MONTH,1);
-
cal2.add(Calendar.DAY_OF_MONTH,-1);
-
returncal2.get(Calendar.DAY_OF_MONTH);
- }
-
-
-
privateint[]getYearAndMonth(){
- Calendarnow=Calendar.getInstance();
-
intyear=now.get(Calendar.YEAR);
-
intmonth=now.get(Calendar.MONTH)+1;
-
if("".equals(cbo_year.getText().trim()))
-
cbo_year.setText(year+"");
-
else{
-
try{
- year=Integer.parseInt(cbo_year.getText().trim());
-
}catch(NumberFormatExceptione){
-
-
cbo_year.setText(year+"");
- }
- }
-
if("".equals(cbo_month.getText().trim()))
-
cbo_month.setText(month+"");
-
else{
-
try{
- month=Integer.parseInt(cbo_month.getText().trim());
-
if(month<1){
-
month=1;
-
cbo_month.setText("1");
-
}elseif(month>12){
-
month=12;
-
cbo_month.setText("12");
- }
-
}catch(NumberFormatExceptione){
-
-
cbo_month.setText(month+"");
- }
- }
-
returnnewint[]{year,month};
- }
-
-
-
privatevoiddisplayClblDays(booleanreflushCboYearItems){
- Calendarcalendar=Calendar.getInstance();
-
intcurrentDate=calendar.get(Calendar.DATE);
-
int[]yearAndMonth=getYearAndMonth();
-
intyear=yearAndMonth[0];
-
intmonth=yearAndMonth[1];
-
calendar.set(year,month-1,1);
-
intbeginIndex=calendar.get(Calendar.DAY_OF_WEEK)-1;
-
intendIndex=beginIndex+getDayCountOfMonth(year,month)-1;
-
if(reflushCboYearItems)
- setCboYearItems(year);
-
intday=1;
-
for(inti=0;i<clbl_days.length;i++){
-
if(i>=beginIndex&&i<=endIndex){
-
clbl_days[i].setText(day+"");
-
clbl_days[i].setToolTipText(year+"-"+month+"-"+day);
-
if(day==currentDate)
- clbl_days[i].setBackground(COLOR_SYSTEM_BLUE);
-
else
- clbl_days[i].setBackground(COLOR_SYSTEM_WHITE);
- day++;
-
}else{
-
clbl_days[i].setText("");
-
clbl_days[i].setToolTipText("");
- clbl_days[i].setBackground(COLOR_SYSTEM_WHITE);
- }
- }
- }
-
publicStringopen(){
-
displayClblDays(true);
- shell.open();
-
while(!shell.isDisposed()){
-
if(!display.readAndDispatch()){
- display.sleep();
- }
- }
- COLOR_SHELL_BACKGROUND.dispose();
- COLOR_CLBLWEEKDAY_BACKGROUND.dispose();
-
returnselectedDate;
- }
-
privateMouseListenerclblMouseListener=newMouseAdapter(){
-
-
-
@Override
-
publicvoidmouseDown(MouseEvente){
- selectedDate=((CLabel)e.widget).getToolTipText();
-
if(!"".equals(selectedDate))
- shell.close();
- }
- };
-
privateSelectionListenerbtnSelectionListener=newSelectionAdapter(){
-
publicvoidwidgetSelected(SelectionEvente){
- Stringtooptip=((Button)e.widget).getToolTipText();
-
int[]yearAndMonth=getYearAndMonth();
-
intyear=yearAndMonth[0];
-
intmonth=yearAndMonth[1];
-
booleanreflushCboyearItems=true;
-
-
if(TOOPTIPTEXT_PREVIOUSYEAR.equals(tooptip))
-
cbo_year.setText((year-1)+"");
-
-
elseif(TOOPTIPTEXT_NEXTYEAR.equals(tooptip))
-
cbo_year.setText((year+1)+"");
-
-
elseif(TOOPTIPTEXT_PREVIOUSMONTH.equals(tooptip)){
-
if(month==1){
-
setCboYearItems(year-1);
-
cbo_month.select(11);
-
}else{
-
cbo_month.select(month-2);
-
reflushCboyearItems=false;
- }
-
-
}elseif(TOOPTIPTEXT_NEXTMONTH.equals(tooptip)){
-
if(month==12){
-
setCboYearItems(year+1);
-
cbo_month.select(0);
-
}else{
- cbo_month.select(month);
-
reflushCboyearItems=false;
- }
- }
- displayClblDays(reflushCboyearItems);
- }
- };
-
privateSelectionListenercboSelectionListener=newSelectionAdapter(){
-
publicvoidwidgetSelected(SelectionEvente){
-
booleanreflushCboyearItems=e.widget==cbo_year?true:false;
- displayClblDays(reflushCboyearItems);
- }
- };
-
privateKeyListenercboKeyListener=newKeyAdapter(){
-
-
-
@Override
-
publicvoidkeyPressed(KeyEventevent){
-
-
if(event.keyCode==16777296||event.keyCode==13){
-
booleanreflushCboyearItems=event.widget==cbo_year?true:false;
- displayClblDays(reflushCboyearItems);
-
}else{
-
-
if((event.keyCode<'0'||event.keyCode>'9')&&!(event.keyCode==SWT.BS||event.keyCode==SWT.ARROW_UP||event.keyCode==SWT.ARROW_DOWN))
-
event.doit=false;
- }
- }
- };
-
publicstaticvoidmain(Stringargs[]){
-
try{
- Displaydisplay=Display.getDefault();
-
Shellshell=newShell(display,SWT.SHELL_TRIM);
-
shell.setSize(500,375);
- shell.open();
- shell.layout();
-
CalendarWidgetca=newCalendarWidget(shell);
- System.out.println(ca.open());
-
while(!shell.isDisposed()){
-
if(!display.readAndDispatch())
- display.sleep();
- }
-
}catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
分享到:
相关推荐
因此自己写一个日历控件用着方便,功能大致如下: 1、日历控件可自定义设置常见的几种日期格式; 2、可以绑定注册到其他控件如:文本框、按钮、标签上使用; 3、可以绑定注册控件后给指定其他控件赋值。 使用者可以...
用 SWT 来显示 日历控件 源码 很实用
在使用SWT 的控件CalendarCombo时,点击None之后再选择其他时间,会出现空指针异常,且会导致程序崩溃,改包下的几个类通过改写CalendarCombo解决了这个小bug ,使用方法与CalendarCombo类似,MyCalendarCombo mc=...
Java swing日历控件,界面精美,使用方便,能用于个人作品 首先,声明一点,这个控件是我从一套据说价值九百多美元的swing类包中提取出来, 并加以修改的,所以: 一, 你不能把该控件用于任何商业产品中。 二,...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...
5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...