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

📄 calendar1.java

📁 里面写的是 一些J2SE 的日历和 计算机的代码 还有时间钟表的代码 供初学者参考
💻 JAVA
字号:
//1900年-2019年 日历

import  java.awt.*;
import  java.awt.event.*;
import  java.util.*;

public class Calendar1 extends Frame implements ActionListener,ItemListener{
   
    boolean bool;
    static int years=1900; 
    static int months=1;
    int dates;
    int days;
    
    Choice ch1=new Choice();
    Choice ch2=new Choice();
    
    String[] year=new String[30];
    Label[] label=new Label[42];           //控制days
    Label[]   lab=new Label[7];            //控制星期几
    Panel p=new Panel(new GridLayout(6,7));//星期几及日期
    
   public Calendar1(String title){
    	super("1900-2019月历");
    	setLayout(null);
        setBackground(new Color(255,210,210));
       
        Label n1=new Label("年");          //年份下拉列表框
        n1.setBounds(120,45,25,20); add(n1);
        
          for(int i=0;i<30;i++){
          	 year[i]=String.valueOf(i+1990);
          	 ch1.add(year[i]);
          }
          
        ch1.setLocation(60,45);add(ch1);    //添加
        ch1.addItemListener(this);          //注册
        
        Label n2=new Label("月");           //月份下拉列表框
         n2.setBounds(215,45,25,20);add(n2);
         
          String[] month=new String[13];
          for(int i=1;i<=12;i++){
          	month[i]=String.valueOf(i);
          	 ch2.add(month[i]);
          }
        ch2.setLocation(170,45);add(ch2);    //添加
        ch2.addItemListener(this);           //注册 
       
        Panel p1=new Panel();
        p1.setBounds(9,75,290,25);
        //p1.setBackground(new Color(125,125,125));   //测试p1的大小
        p1.setLayout(new FlowLayout(FlowLayout.LEFT,14,2));
        add(p1);
        for(int i=0;i<7;i++){
        	lab[i]=new Label("",Label.CENTER);
        	p1.add(lab[i]);
        }
    	String log[]={"日","一","二","三","四","五","六"};
    	for(int i=0;i<7;i++){
    		lab[i].setText(log[i]);
    	}
    	lab[0].setForeground(Color.red);
    	lab[6].setForeground(Color.green);
    	
        for(int i=0;i<42;i++){
        	label[i]=new Label("",Label.CENTER);//日期居中显示
        	p.add(label[i]);
        }
        p.setBounds(16,100,285,135);add(p);
        
        p.setVisible(true);
		
   	   addWindowListener(new WindowAdapter (){
	    	public void windowClosing(WindowEvent e){
	    		Calendar1.this.dispose();    //关闭窗口
	    	}
	    });	
    }
    public void Init(){                     //设置月历的颜色

    	for(int i=0;i<42;i=i+7){
    		label[i].setForeground(Color.red);//星期日设置为红色
    	}
    	for(int i=6;i<42;i=i+7){
    		label[i].setForeground(Color.green);//星期六设置为绿色
    	}
    }
    
    public void HshuCalendar(int years,int months){
		if(years%4==0&&!(years%100==0)||years%400==0){
		bool=true;                              //闰年
		}
		else{
		bool=false;                             //平年
		}
		/* 判断闰年或平年,并进行天数累加 */
		for( int i=1900;i<years;i++){
			if(i%4==0&&!(i%100==0)||i%400==0){
				dates=dates+366;                //闰年天数
			}
			else{
				dates=dates+365;                //平年天数
			}
		}
		/* 计算输入月份之前的天数 */
		int beforeDays = 0;              
		for(int i=1;i<=months;i++){
		   switch(i){
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
			    days=31;
			break;
			case 2:
			if(bool){
				days=29;                         //闰年的2月
			}
			else{
				days=28;                         //一般的2月
			}
			break;
			default: days=30;
			break;
		   }
		   if(i<months)	{
		   	 beforeDays=beforeDays+days;
		   }
		}
		     dates=dates+beforeDays;
		
	   int firstDayOfMonth=0;     // 存储当月第一天是星期几:星期日为0,星期一/星期六为1/6
       int temp = 1 + dates % 7;  // 从1900年1月1日推算
       if (temp == 7) {           // 当月第一天
        firstDayOfMonth = 0;      // 周日
        }
       else {
        firstDayOfMonth = temp;
       }
	   
	   for(int i=0;i<firstDayOfMonth;i++)
           label[i].setText("");   
       for(int i=0;i<days;i++)
       	label[i+firstDayOfMonth].setText(String.valueOf(i+1));
       for(int i=41;i>=days+firstDayOfMonth;i--)
           label[i].setText("");
       dates=0;   beforeDays = 0;
	}
	
	//覆盖
    public void actionPerformed(ActionEvent e){
    	
    	} 
    //覆盖
    public void itemStateChanged(ItemEvent e){
		if(e.getSource()==ch2) {
		      months=Integer.parseInt(ch2.getSelectedItem());
		      HshuCalendar(years,months);
		      //System.out.println(months);//测试语句
		}
		
		if(e.getSource()==ch1){
		     years=Integer.parseInt(ch1.getSelectedItem());
		     HshuCalendar(years,months);
		     //System.out.println(years); //测试语句
		}
	}
    //主方法	
	public static void main(String[] args){
		  Calendar1 f=new Calendar1("");
		  f.setSize(300,250);
		  f.setVisible(true);
		  f.setLocation(250,200);
		  f.setResizable(false);
		  f.HshuCalendar(years,months);
		  f.Init();
	}
} 

⌨️ 快捷键说明

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