首页 > 编程语言 >一个日历程序 可农历到公历 公历到农历 第几周 显示节气

一个日历程序 可农历到公历 公历到农历 第几周 显示节气

时间:2023-10-20 11:04:24浏览次数:33  
标签:公历 gridData GridData 节气 农历 private gridLayout new SWT


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)。


标签:公历,gridData,GridData,节气,农历,private,gridLayout,new,SWT
From: https://blog.51cto.com/u_16298170/7947893

相关文章

  • 阴历、阳历、农历,自己悟
    金刚经上说破4相;我相、人相、众生相、寿者相;其中的寿者相就是指的时间,时间是人类的定义,人类的自我感觉;时间是不存在的;所以佛经再次重申,破寿者相破3心,过去心不可得、现在心不可得、未来心不可得;应无所住而生其心;----------------时间是人类的自我定义,要想定义时间,首先物理定......
  • Python 农历公历相互转换
    Python农历公历相互转换stackOverflowsh关注IP属地:江苏0.2252019.02.1918:23:48字数2,054阅读6,862背景日常用python处理各种数据分析工作,最近需要对历年春节期间的数据做一些对比工作,本来只是用了一个简单的日期数组来进行,但后来发现一些数据在农历日期进行对比......
  • .net core 24节气计算器 by valu C#
    调用方法:solar_terms_utiljq=newsolar_terms_util();vartarget_date=DateTime.Now;jq.calc_jieqi(target_date);stringjq_text=jq.this_jq;//当前节气vardate1=jq.this_date;//当前节气开始时间vardate2=jq.jq.next_date;//下个节气开始时间代码如下:......
  • C#农历日期,亲测可用
     usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Globalization;namespaceDTcms.Common{   ///<summary>   ///转为农历类   ///</summary>   publicclasscalendar   {       staticChi......
  • chf 农历日期 干支日期
    1、html<script>vardate=newDate();xmltext=newXMLHttpRequest;xmltext.open("GET","2.xml",false);xmltext.send();a=xmltext.responseXML;//document.getElementById("xmlid").innerHTM......
  • 查找自己农历生日与公历生日在同一天的年份
    #请先使用命令pipinstallsxtwl安装依赖库后,再执行以下脚本importsxtwlymc=["正","二","三","四","五","六","七","八","九","十","冬","腊"]rmc=[&quo......
  • 农历为什么会有“闰月”,今年为啥要“闰二月”?一文读懂
    2023年2月21日是农历二月初二。“二月二”起码从元朝开始就是一个很重要的日子。老话讲:“二月二,龙抬头。”过了三十天,到了2023年3月23日,你猜怎么着?请看月份牌:......
  • GDI+带农历的万年历(周历)之制作
    今天在网上无意中搜索到“Vista风格日历控件”。下载之后发现,略有BUG,于是进行改进。无意中,制作出来带农历的万年历(周历版)。运行如下图:​​​​​​主要的改进在://frmDemo.c......
  • 带农历的中国万年历制作
    最近一个小项目需要制作带农历的中国万年历,苦闷了几天,终于大功告成,可以保存为图片,可以更换风格等,全用C#和 GDI+完成,为提高效率,全自行绘制,没有使用Label等控件。上面的代码......
  • 代码段——C#获取完整农历日期
    代码段——C#获取完整农历日期1.测试privatestaticvoidMain(string[]args){CalendarHelpercCalendar=newCalendarHelper();stringstrDateInfo......