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

📄 mycalendar.java

📁 一个java写的简单的日历程序
💻 JAVA
字号:
/*
 *@一个日历程序  2005.01.30
 *@author: Renyao(ailingfor)
 *@version: 1.0.0
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class MyCalendar extends JFrame implements ItemListener,ChangeListener,MouseListener{
	private JLabel lbyear,lbmonth;
	private JSpinner spyear;
	private JComboBox cbmonth;
	private JLabel lbweek[];
	private JTextField days[];
	private JPanel p1,p2,weekpanel,daypanel;
	private Calendar calend;
	int year,month,day;
	int week;
	String []weeks={"Sun.","Mon.","Tues.","Wed.","Thurs.","Friday","Sat."};
	String []months={"January","February","March","April","May","June","July","August","September","October","November","December"};
	public MyCalendar(int year,int month,int day){
		super("Calendar");
		setBounds(300,200,360,220);
		calend = Calendar.getInstance();
		Container cp=getContentPane();
		cp.setLayout(new BorderLayout());
		p1=new JPanel();
		p2=new JPanel();
		weekpanel=new JPanel();
		daypanel=new JPanel();
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(UnsupportedLookAndFeelException exc){
			System.err.println(exc.getMessage());	
		}
		catch(Exception exc){
			System.err.println(exc.getMessage());	
		}
		this.year=year;
		this.month=month;
		this.day=day;
		lbyear=new JLabel("Year");
		lbmonth=new JLabel("Month");
		spyear=new JSpinner();
		spyear.setEditor(new JSpinner.NumberEditor(spyear,"0000"));
		spyear.setValue(new Integer(year));
		cbmonth=new JComboBox();
		for(int i=0;i<12;i++){
			cbmonth.addItem(months[i]);
		}
		p1.add(lbyear);
		p1.add(spyear);
		p1.add(lbmonth);
		p1.add(cbmonth);
		spyear.addChangeListener(this);
		cbmonth.addItemListener(this);
		lbweek=new JLabel[7];
		weekpanel.setLayout(new GridLayout(1,7));
		for(int i=0;i<7;i++){
			lbweek[i]=new JLabel();
			lbweek[i].setText(weeks[i]);
			lbweek[i].setBorder(BorderFactory.createRaisedBevelBorder());
			lbweek[i].setHorizontalAlignment(JLabel.CENTER);
			weekpanel.add(lbweek[i]);	
		}
		lbweek[0].setForeground(Color.red);
		lbweek[6].setForeground(Color.blue);
		days=new JTextField[42];
		daypanel.setLayout(new GridLayout(6,7));
		for(int i=0;i<42;i++){
			days[i]=new JTextField();
			days[i].setEditable(false);
			days[i].setHorizontalAlignment(JTextField.RIGHT);
			days[i].addMouseListener(this);
			daypanel.add(days[i]);	
		}
		p2.setLayout(new BorderLayout());
		p2.add(weekpanel,BorderLayout.NORTH);
		p2.add(daypanel,BorderLayout.CENTER);
		cp.add(p1,BorderLayout.NORTH);
		cp.add(p2,BorderLayout.CENTER);
		setCalendar(year,month);
		addWindowListener(new WindowAdapter()
                    { public void windowClosing(WindowEvent e)
                       {
                         System.exit(0);
                       }
                    });
		setVisible(true);
		setResizable(false);
		validate();
	}
	public void setCalendar(int year,int month){
  		calend.set(year,month-1,1);                 
     	week=calend.get(Calendar.DAY_OF_WEEK)-1;
     	if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
        	sortCalendar(week,31);         
        }
     	else if(month==4||month==6||month==9||month==11){
            sortCalendar(week,30);
        }
     	else if(month==2){
        	if((year%4==0&&year%100!=0)||(year%400==0)){
            	sortCalendar(week,29);
           	}
        else{
            sortCalendar(week,28);
        }
        }
   } 
	public void sortCalendar(int week,int monthdays){
		for(int i=week,n=1;i<week+monthdays;i++){
			days[i].setText(""+n);
			if(n==day){
				days[i].setForeground(Color.green);
				days[i].setFont(new Font("仿宋体",Font.BOLD,13));	
			}	
			else {
				days[i].setForeground(Color.black);				
				days[i].setFont(new Font("仿宋体",Font.PLAIN,10));
			}
			if(i%7==6){
				days[i].setForeground(Color.blue);	
			}
			if(i%7==0){
				days[i].setForeground(Color.red);	
			}
			n++;
		}
		for(int i=0;i<week;i++){
			days[i].setText("");	
		}
		for(int i=week+monthdays;i<42;i++){
			days[i].setText("");	
		}
		
	}
	public void itemStateChanged(ItemEvent e){
		month=cbmonth.getSelectedIndex();	
		setCalendar(year,month+1);
	}
	public void stateChanged(ChangeEvent e){
		year=((Integer)spyear.getValue()).intValue();
		setCalendar(year,cbmonth.getSelectedIndex()+1);	
	}
	public void mouseClicked(MouseEvent e){
		JTextField source=(JTextField)e.getSource();
		try{
			String s=source.getText();
			if(s==""){
				return;	
			}
			else {
				day=Integer.parseInt(s);
				year=((Integer)spyear.getValue()).intValue();
				month=cbmonth.getSelectedIndex();
				setCalendar(year,month+1);	
			}
		}
		catch(NumberFormatException ee){}			
	}
	public void mouseReleased(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public static void main(String[] args){
		Calendar calendar=Calendar.getInstance();
		int year=calendar.get(Calendar.YEAR);
		int month=calendar.get(Calendar.MONTH)+1;
		int day=calendar.get(Calendar.DAY_OF_MONTH);
		new MyCalendar(year,month,day);
	}
}

⌨️ 快捷键说明

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