⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calwin.java

📁 java编写的带界面的日历程序。可以点击某一个时间
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			{
				//if(this.baseDate.getDate()==31||this.baseDate.getDate()==30||this.baseDate.getDate()==31)
				this.baseDate.setMonth(baseDate.getMonth()-1);
				
			}
			else//对于每年1月份的时候,这里做特殊处理:将年份减少一,月份设置为12
			{
				this.baseDate.setYear(baseDate.getYear()-1);
				this.baseDate.setMonth(12);
			}
			
			this.SetCell(cell, baseDate);//如果点击确定按钮就调用setDay()重新方法绘制按钮  
			updateView();

		} 
		

		//		添加按钮		
		else if (e.getSource() == addButton) 
		{ 
			//System.out.println("changeFlage:"+changeFlag);
			 //对日进行操作
			if(changeFlag == 1)
			{
				//日子超过一个月则作特殊吹
				if(baseDate.getDate()>=baseDate.getDaysOfMonth())
				{
					//日子超过一个月,但是没超过一年
					if(baseDate.getMonth()<12)
					{
						this.baseDate.setMonth(baseDate.getMonth()+1);
						this.baseDate.setDate(1);
					}
					
					//日子超过一年
					else
					{
						this.baseDate.setYear(baseDate.getYear()+1);
						this.baseDate.setMonth(1);
						this.baseDate.setDate(1);
					}
					
				}
				else
				{
				//	System.out.println(baseDate.getDate()+1);
				//	System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
				//	System.out.println(baseDate.getDate()+1);
					baseDate.setDate(baseDate.getDate()+1);
				//	System.out.println("bbbbb"+baseDate.getDate());
				}
			}
			
			//对月份进行处理
			else if(changeFlag == 2)
			{
				//月份超过一年进行特殊处理
				if(baseDate.getMonth()>=12)
				{
					
					this.baseDate.setYear(baseDate.getYear()+1);
					this.baseDate.setMonth(1);
				}
				else
				{
					this.baseDate.setMonth(baseDate.getMonth()+1);
				}
			}
			

			//	处理年份
			else if(changeFlag == 3)
			{
				//年份超过了公元前,则提示错误
				if(baseDate.getYear()>=1)
				{
					this.baseDate.setYear(baseDate.getYear()+1);
					
				}
				else
				{
					
					System.exit(1);
				}
			}
			
			this.SetCell(cell, baseDate);//如果点击确定按钮就调用setDay()重新方法绘制按钮  
			updateView();
			

			//System.out.println("asdfasdfasdfasdfasdf");
			//System.out.println(baseDate.getDate());
		} 
		
		//递减按钮,具体情况见添加按钮,两者类似
		else if (e.getSource() == delButton) 
		{ 
			//System.out.println("changeFlage:"+changeFlag);
			 
			if(changeFlag == 1)
			{
				if(baseDate.getDate()<=1)
				{
					if(baseDate.getMonth()>1)
					{
						this.baseDate.setMonth(baseDate.getMonth()-1);
						this.baseDate.setDate(baseDate.getDaysOfMonth());
					}
					else if(baseDate.getMonth()==1)
					{
						this.baseDate.setYear(baseDate.getYear()-1);
						this.baseDate.setMonth(12);
						this.baseDate.setDate(baseDate.getDaysOfMonth());
					}
					
				}
				else if(baseDate.getDate()>1)
				{
					
					this.baseDate.setDate(baseDate.getDate()-1);
				}
			}
			
			else if(changeFlag == 2)
			{
				if(baseDate.getMonth()<=1)
				{
					this.baseDate.setYear(baseDate.getYear()-1);
					this.baseDate.setMonth(12);
				}
				else
				{
					this.baseDate.setMonth(baseDate.getMonth()-1);
				}
			}
			
			else if(changeFlag == 3)
			{
				if(baseDate.getYear()>=1)
				{
					this.baseDate.setYear(baseDate.getYear()-1);
					
				}
				else
				{
					JOptionPane.showMessageDialog(null,"本日历不能计算公元前!!"); 
				}
			}
			
			this.SetCell(cell, baseDate);//如果点击确定按钮就调用setDay()重新方法绘制按钮  
			updateView();

		} 
		
		//Ok按钮
		else if(e.getSource()==OkButton)
		{
			JOptionPane.showMessageDialog(null,baseDate.toString()); 
		}
		//cancel按钮
		else if(e.getSource()==cancelButton)
		{
			System.exit(1);
		}
	}

	//对所有cell对象进行数据设置
	private viewer.CalWin.Cell[] SetCell(viewer.CalWin.Cell[] cell2, MyDate date) {
		computeDate = new ComputeDate();
		
		//取得每月开始日期星期偏移量
		int firstDate = (int)computeDate.getFirstDay(date);
		
		//该月份总天数
		int daysOfMonth = (int)computeDate.getDaysOfMonth(date);
		
		//System.out.println(daysOfMonth);
		//将发生变化前的的cell数组清理
		for(int i=0;i<42;i++)
		{
			cell[i].setText("");
			cell[i].cellDate = null;
		}
		

//		设置发生变化后的cell数组清理
		for(int i=0;i<daysOfMonth;i++)
		{
			cellDate1 = (MyDate)date.clone();
			cellDate1.setDate(i+1);
			
			cell[i+firstDate].cellDate = cellDate1;
			cell[i+firstDate].setText(""+(i+1));
			
			//如果某个日期单元的日期同当前日期符合,则被选择,被选择的按钮置红
			if(cell[i+firstDate].cellDate.getDate()==baseDate.getDate())
			{
				cell[i+firstDate].selected();
			}
		}
		
		return cell;
	}


	//将TextFeild区间的日期数据更新
	public void updateView()
	{
		dateTextField.setText((new Integer(baseDate.getDate()).toString())); 
		monthTextField.setText((new Integer(baseDate.getMonth()).toString())); 
		yearTextField.setText((new Integer(baseDate.getYear()).toString())); 
			
	}
	


	/********************内部类,用于对单个日期的显示和各种处理等方法**************************/
	class Cell extends JLabel implements MouseListener 
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = -4390509119904759152L;
		MyDate cellDate = new MyDate();//每个单元对应的日期
		MyDate newDate = null;
		boolean isSelected = false;
		
		Cell [] cellGroup = null;
		
		//默认构造方法
		public Cell()
		{

			this.setBorder(null);
					
			if(this.isSelected == true)
			{
				this.setOpaque(false);
				this.setBackground(Color.blue);
			}
			
			this.addMouseListener(this);
		}
		
		
		public Cell(MyDate date,boolean isSelected,Cell[] cellGroup)
		{
			this.setBorder(null);					
			this.cellDate = (MyDate)(date.clone());//关键点,将每个日期的时间进行重置,因此将传送过的日期进行重新克隆	
			//System.out.println(this.cellDate.getDate());
			this.isSelected = isSelected;
			
			//this.cellGroup = cellGroup;
			
			if(this.isSelected == true)
			{
				//this.setOpaque(false);
				this.setForeground(Color.red);
			}
			
			
			this.addMouseListener(this);
		}
		
		public void mouseClicked(MouseEvent e)
		{
			if(e.getClickCount()==2)//处理双击事件
			{
				doSomeing();
			}
			else if(e.getClickCount()==1)//处理单击选择事件
			{
				selected();
			}
			
		}
		
		//处理双击事件
		public void doSomeing()
		{
			if(this.cellDate==null)
			{
				
			}
			else
			{
				JOptionPane.showMessageDialog(null,baseDate.toString()); 
				System.exit(1);
			}
			
		}
		
		//单击选择事件
		public void selected()
		{
			this.isSelected = true;			

			//查看当前被点击的单元节点是否正常的月份之内的节点,不是的话不做任何动作
			if(this.cellDate==null)
			{
				
			}

			//			查看当前被点击的单元节点是否正常的月份之内的节点,是的话做一系列符合节点规则的动作
			else
			{
//				选择之后其他字体颜色回复
				for(int i =0;i<42;i++)
				{
					cell[i].setForeground(Color.black);
					//cell[i].setEnabled(false);
					//System.out.println("aaaa");
				}
				this.setForeground(Color.red);//选择之后字体颜色改变
				
				baseDate = this.cellDate;   //选择之后当前时间改变成该单元对应的时间 
				//System.out.println("dddddddddd"+baseDate);
				updateView();
			}
			//return this.cellDate;
		}

		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		
	}		
}  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -