public class Sample3 { private Color green = Display.getCurrent().getSystemColor(SWT.COLOR_GREEN); private Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE); private Color gray = Display.getCurrent().getSystemColor(SWT.COLOR_GRAY); private Color cray = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_CYAN); private Color red = Display.getCurrent().getSystemColor(SWT.COLOR_RED); private Vector<CLabel> monthVector = new Vector<CLabel>(45); private GridData gridData; private GregorianCalendar calendar; private int todayInt; private Combo yearCombo, monthCombo; private Combo lunarYearCombo, lunarMonthCombo, lunarDayCombo; private CLabel lunarClabel, weekInYearClabel; private Lunar lunar; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("日历"); new Sample3().createArea(shell); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } private void createArea(Shell shell) { shell.setLayout(new GridLayout()); // shell.setSize(400, 400); Composite container = new Composite(shell, SWT.BORDER); GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 1; gridLayout.marginLeft = 0; gridLayout.marginRight = 0; gridLayout.marginHeight = 0; gridLayout.marginWidth = 0; gridLayout.verticalSpacing=0; container.setLayout(gridLayout); gridData = new GridData(GridData.FILL_BOTH); container.setLayoutData(gridData); Composite c1 = new Composite(container, SWT.BORDER); gridLayout = new GridLayout(); gridLayout.numColumns = 5; gridLayout.marginLeft = 10; gridLayout.marginRight = 10; gridLayout.marginHeight = 10; gridLayout.marginWidth = 10; gridLayout.verticalSpacing=0; gridLayout.horizontalSpacing = 1; c1.setLayout(gridLayout); gridData = new GridData(GridData.FILL_HORIZONTAL); c1.setLayoutData(gridData); createC1(c1); Composite c2 = new Composite(container, SWT.BORDER); gridLayout = new GridLayout(); gridLayout.numColumns = 7; gridLayout.marginLeft = 0; gridLayout.marginRight = 0; gridLayout.marginHeight = 5; gridLayout.marginWidth = 5; gridLayout.verticalSpacing=0; c2.setLayout(gridLayout); gridData = new GridData(GridData.FILL_BOTH); c2.setLayoutData(gridData); createC2forTitle(c2); createC2forMonth(c2); addListen4Month(); Composite c3 = new Composite(container, SWT.BORDER); gridLayout = new GridLayout(); gridLayout.numColumns = 5; gridLayout.marginLeft = 2; gridLayout.marginRight = 2; gridLayout.marginHeight = 2; gridLayout.marginWidth = 2; gridLayout.verticalSpacing=0; gridLayout.horizontalSpacing = 1; c3.setLayout(gridLayout); gridData = new GridData(GridData.FILL_HORIZONTAL); c3.setLayoutData(gridData); createC3(c3); today(); } private void createC3(Composite c3) { //weekInYearClabel weekInYearClabel = new CLabel(c3, SWT.CENTER | SWT.BORDER); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = SWT.LEFT; weekInYearClabel.setLayoutData(gridData); weekInYearClabel.setText("第几周"); weekInYearClabel.setBackground(gray); { // lunar year combo lunarYearCombo = new Combo(c3, SWT.DROP_DOWN | SWT.READ_ONLY ); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = SWT.RIGHT; lunarYearCombo.setLayoutData(gridData); for(int i=1900; i<=2050; i++) { lunarYearCombo.add(""+i); } lunarYearCombo.select(0); // lunar month combo String[] lunarMonthStr = { "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "冬月", "腊月" }; lunarMonthCombo = new Combo(c3, SWT.DROP_DOWN | SWT.READ_ONLY | SWT.RIGHT); gridData = new GridData(); // the restriction for grid. gridData.grabExcessHorizontalSpace = true; // the process for widget in the grid. gridData.horizontalAlignment = SWT.LEFT; lunarMonthCombo.setLayoutData(gridData); lunarMonthCombo.setItems(lunarMonthStr); lunarMonthCombo.select(0); //lunar day combo String[] lunarDayStr = { "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十", "十一","十二","十三","十四","十五","十六","十七","十八","十九","廿十", "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","卅十", }; lunarDayCombo = new Combo(c3, SWT.DROP_DOWN | SWT.READ_ONLY | SWT.RIGHT); gridData = new GridData(); // the restriction for grid. gridData.grabExcessHorizontalSpace = true; // the process for widget in the grid. gridData.horizontalAlignment = SWT.LEFT; lunarDayCombo.setLayoutData(gridData); lunarDayCombo.setItems(lunarDayStr); lunarDayCombo.select(0); Button lunar2solar = new Button(c3, SWT.NONE); gridData = new GridData(); // the restriction for grid. gridData.grabExcessHorizontalSpace = true; // the process for widget in the grid. gridData.horizontalAlignment = SWT.LEFT; lunar2solar.setLayoutData(gridData); lunar2solar.setText("农历转公历"); lunar2solar.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { int lunarYear, lunarMonth, lunarDay; lunarYear = Integer.parseInt(lunarYearCombo.getText()); lunarMonth = lunarMonthCombo.getSelectionIndex() +1; lunarDay = lunarDayCombo.getSelectionIndex() +1; int solarYear, solarMonth, solarDay; String solarStr = ChineseCalendar.sCalendarLundarToSolar(lunarYear, lunarMonth, lunarDay); solarYear = Integer.parseInt(solarStr.substring(0, 4)); solarMonth = Integer.parseInt(solarStr.substring(4, 6)); solarDay = Integer.parseInt(solarStr.substring(6, 8)); calendar = new GregorianCalendar(); calendar.set(Calendar.YEAR, solarYear); calendar.set(Calendar.MONTH, solarMonth-1); calendar.set(Calendar.DAY_OF_MONTH, solarDay); todayInt = solarDay; // refresh yearCombo, monthCombo, lunarClabel, weekInYearClabel; yearCombo.select(solarYear-1900); monthCombo.select(solarMonth-1); // refresh week in year weekInYearClabel.setText("第 "+ calendar.get(Calendar.WEEK_OF_YEAR) +" 周"); // refresh lunarClabel lunar = new Lunar(calendar); lunarClabel.setText(lunar.toString()); calendar.set(Calendar.DAY_OF_MONTH, 1); int firstDayWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); refresh(firstDayWeek, maxDayOfMonth); } }); } } private void createC1(Composite c1) { //years combo yearCombo = new Combo(c1, SWT.DROP_DOWN | SWT.READ_ONLY ); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = SWT.LEFT; yearCombo.setLayoutData(gridData); for(int i=1900; i<=2050; i++) { yearCombo.add(""+i); } yearCombo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { calendar = new GregorianCalendar(); refreshMonth(); } }); //month combo monthCombo = new Combo(c1, SWT.DROP_DOWN | SWT.READ_ONLY | SWT.RIGHT); gridData = new GridData(); // the restriction for grid. gridData.grabExcessHorizontalSpace = true; // the process for widget in the grid. gridData.horizontalAlignment = SWT.LEFT; monthCombo.setLayoutData(gridData); for(int i=1; i<=12; i++) { monthCombo.add(""+i); } monthCombo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { calendar = new GregorianCalendar(); refreshMonth(); } }); //lunar lunarClabel = new CLabel(c1, SWT.CENTER | SWT.BORDER); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalSpan = 2; gridData.horizontalAlignment = SWT.RIGHT; lunarClabel.setLayoutData(gridData); lunarClabel.setText("。。。"); lunarClabel.setBackground(gray); //today Button todayButton = new Button(c1, SWT.NONE | SWT.WRAP); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = SWT.RIGHT; todayButton.setLayoutData(gridData); todayButton.setText("今天"); todayButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { today(); } }); } // text private void createC2forTitle(Composite c2) { final String WEEK[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; for(int i=0; i<7; i++) { Button week = new Button(c2, SWT.NONE); week.setText(WEEK[i]); gridData = new GridData(GridData.FILL_BOTH); week.setLayoutData(gridData); week.setEnabled(false); week.setBackground(cray); } } private void createC2forMonth(Composite c2) { for(int i=0; i<42; i++) { CLabel b = new CLabel(c2, SWT.CENTER | SWT.BORDER); gridData = new GridData(GridData.FILL_BOTH); b.setLayoutData(gridData); b.setText("tst" +i); b.setBackground(white); if ((i+1)%7==0 || i%7==0) { b.setForeground(red); } monthVector.add(b); } } private void addListen4Month() { for(int i=0; i<42; i++) { final CLabel b= monthVector.elementAt(i); b.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { for(int i=0; i<42; i++) { CLabel c = monthVector.elementAt(i); c.setBackground(white); } b.setBackground(green); calendar = new GregorianCalendar(); todayInt = Integer.parseInt(b.getText().substring(0, 2).trim()); calendar.set(Calendar.DAY_OF_MONTH, todayInt); refreshMonth(); } }); } } private void today() { calendar = new GregorianCalendar(); //refresh yearCombo, monthCombo, lunarClabel, weekInYearClabel; int year = calendar.get(Calendar.YEAR); yearCombo.select(year-1900); int month = calendar.get(Calendar.MONTH)+1; monthCombo.select(month-1); weekInYearClabel.setText("第 "+ calendar.get(Calendar.WEEK_OF_YEAR) +" 周"); lunar = new Lunar(calendar); lunarClabel.setText(lunar.toString()); todayInt = calendar.get(Calendar.DAY_OF_MONTH); //refresh lunarYearCombo, lunarMonthCombo, lunarDayCombo; refreshLunarCombo(calendar); int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, 1); int firstDayWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; refresh(firstDayWeek, maxDayOfMonth); } private void refreshLunarCombo(GregorianCalendar calendar2) { int year = calendar2.get(Calendar.YEAR); int month = calendar2.get(Calendar.MONTH)+1; String lunarStr = ChineseCalendar.sCalendarSolarToLundar(year, month, todayInt); int lyear = Integer.parseInt(lunarStr.substring(0, 4)); int lmonth = Integer.parseInt(lunarStr.substring(4, 6)); int lday = Integer.parseInt(lunarStr.substring(6, 8)); lunarYearCombo.select(lyear-1900); lunarMonthCombo.select(lmonth-1); lunarDayCombo.select(lday-1); } private void refreshMonth() { calendar.set(Calendar.YEAR, yearCombo.getSelectionIndex() + 1900); calendar.set(Calendar.MONTH, monthCombo.getSelectionIndex()); // refresh week in year weekInYearClabel.setText("第 "+ calendar.get(Calendar.WEEK_OF_YEAR) +" 周"); // refresh lunarClabel lunar = new Lunar(calendar); lunarClabel.setText(lunar.toString()); // refresh lunarYearCombo, lunarMonthCombo, lunarDayCombo; int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); todayInt = (maxDayOfMonth<todayInt)?maxDayOfMonth:todayInt; refreshLunarCombo(calendar); calendar.set(Calendar.DAY_OF_MONTH, 1); int firstDayWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; refresh(firstDayWeek, maxDayOfMonth); } private void refresh(int firstDayWeek, int maxDayOfMonth) { // clean for(int i=0; i<42; i++) { CLabel b=monthVector.elementAt(i); b.setText(""); b.setBackground(white); b.setEnabled(false); } String appStr; // String lunarStr; // String solarTerms; // String lunarFestival; // for(int i=firstDayWeek,j=1; j<=maxDayOfMonth; i++,j++) { calendar.set(Calendar.DAY_OF_MONTH, j); lunar = new Lunar(calendar); lunarStr = lunar.getChinaDayString(); solarTerms = lunar.getSolarTerms(); lunarFestival =lunar.getLunarFestival(); if(lunarFestival.compareTo("") != 0) { appStr = lunarFestival; } else if (solarTerms.compareTo("") != 0) { appStr = solarTerms; } else { appStr = lunarStr; } CLabel b=monthVector.elementAt(i); b.setText(j +" "+ appStr); if (j==todayInt) { b.setBackground(green); } b.setEnabled(true); } } }
运行上面程序需要附件中的两个类(ChineseCalendar.java 和 Lunar.java)。
效果图: