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

📄 calendar.java

📁 java GUI使用实现的日历
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*  @Calendar
 *  @star 2007.1.11 
 *  @copyright (x)
 */
 
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.event.*;
 import javax.swing.border.*;
 import java.util.Calendar;
 import java.util.*;
 import java.io.*;
 import java.io.IOException;

 
 public class calendar extends JFrame 
 	implements ActionListener,MouseListener 
 {
 	int year,month,day;
 	int yearafterquery,monthafterquery;
 	int startday;
 	String SwitchMonth;
 	String key;
 	
 	int changeyearmessage;
 	int changemonthmessage;
 	
 	int priormonth;
 	int prioryear;
 	
 	boolean ischange=false;
 	boolean ischange_priornext=false;
 	
 	private JPanel LeftPane,RightPane;
 	
 	//Left sub
 	private JLabel YearLabel;
 	private JLabel MonthLabel;
 	private JComboBox MonthCombobox;
 	private JTextField ShowDays[]= new JTextField[42];
 	private JTextField YearText;
 	private JLabel Ask;
 	private JLabel ShowDate;
 	private JLabel Blank;
 	private JLabel TopBarTitle[]=new JLabel[7];
 	private JButton ToToday;
 	private JButton Query;
 	private String week[]={"SUN","MON","TUE","WED","THU","FRI","SAT"};
 	
 	//right sub
 	private JLabel NorthMonthDayYear;
 	private JTextArea CenterText;
 	private JButton SouthSave,SouthDelete;  
 	private JButton PriorMonth;
 	private JButton NextMonth;
 	
 	public static void main(String[] args) throws Exception
 	{
 		Calendar calendar=Calendar.getInstance();    
        int y=calendar.get(Calendar.YEAR);           
        int m=calendar.get(Calendar.MONTH)+1;        
        int d=calendar.get(Calendar.DAY_OF_MONTH);  
 		
 		calendar frame = new calendar(y,m,d);
 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 		//frame.pack();
 		frame.setSize(560,300);
 		frame.setLocation(100,100);
 		frame.setVisible(true);
 		frame.setResizable(false);
 		
 		frame.ReadRecord();
 	}
 	
 	public calendar(int year,int month,int day)
 	{
 		setTitle("Calendar");
 		
 		//the layout about left of jpane 
 		LeftPane = new JPanel();
 		JPanel LeftCenter = new JPanel();
 		JPanel LeftNorth = new JPanel();
 		//JPanel LeftSouth = new JPanel();
 		
 		LeftPane.setLayout(new BorderLayout());
 		LeftPane.add(LeftNorth,BorderLayout.NORTH);
 		LeftPane.add(LeftCenter,BorderLayout.CENTER);
 		LeftPane.add(ToToday = new JButton("Go to today",new ImageIcon("../images/Handle.gif")),BorderLayout.SOUTH);
 		ToToday.setBackground(Color.cyan);
 		ToToday.addActionListener(this);
 		LeftPane.validate();
 		
 		//the layout of LeftPane
 		//LeftPane_North
 		LeftNorth.setLayout(new GridLayout(3,1,0,-2));
 		LeftNorth.add(Ask = new JLabel("  Plese input the informations which you want query:"));
 		JPanel North = new JPanel(new FlowLayout(0,8,0));
 		LeftNorth.add(North);
 		North.add(YearLabel=new JLabel("Year:"));
 		North.add(YearText = new JTextField(4));
 		YearText.setBackground(Color.getHSBColor(30,20,50));
 		YearText.setForeground(Color.blue);
 		YearText.setFont(new Font("TimesRoman",Font.BOLD,17));
 		YearText.addActionListener(this);
 		YearText.setFocusable(true);
 		
 		North.add(Blank=new JLabel(" "));
 		North.add(MonthLabel = new JLabel("Month:"));
 		North.add(MonthCombobox = new JComboBox());
 		//add month to monthcombobox
 		for(int i=1;i<=12;i++)
 		{
 			MonthCombobox.addItem(new Integer(i));
 		}
 		//Switch the month 
 		MonthCombobox.setForeground(Color.blue);
 		MonthCombobox.setFont(new Font("TimesRoman",Font.BOLD,12));
 		North.add(Blank=new JLabel(" "));
 		
 		North.add(Query=new JButton("Query"));
 		Query.setForeground(Color.blue);
 		Query.addActionListener(this);
 		
 		JPanel North2=new JPanel(new FlowLayout());
 		LeftNorth.add(North2);
 		North2.add(PriorMonth=new JButton(new ImageIcon("../images/prior.gif")));
 		PriorMonth.addActionListener(this);
 		PriorMonth.setActionCommand("prior");
 		priormonth=month;
 		prioryear=year;
 		
 		SwitchMonth(month);
 		North2.add(ShowDate = new JLabel(SwitchMonth+" "+","+" "+String.valueOf(year),SwingConstants.CENTER));
 		ShowDate.setForeground(Color.blue);
 		ShowDate.setFont(new Font("TimesRoman",Font.BOLD,14));
 		North2.add(NextMonth=new JButton(new ImageIcon("../images/next.gif")));
 		NextMonth.addActionListener(this);
 		NextMonth.setActionCommand("next");
 		//ShowDate.setIcon(new ImageIcon("images/Handle.gif"));
 		
 		
 		//LeftPane_Center
 		LeftCenter.setLayout(new GridLayout(7,7));
 		//print title
 		for(int i=0;i<7;i++)
 		{
 			TopBarTitle[i]=new JLabel();
 			TopBarTitle[i].setText(week[i]);
 			TopBarTitle[i].setForeground(Color.darkGray);
 			TopBarTitle[i].setHorizontalAlignment(0);
 			TopBarTitle[i].setBackground(Color.orange);
 			TopBarTitle[i].setBorder(BorderFactory.createRaisedBevelBorder());
 			LeftCenter.add(TopBarTitle[i]);
 		}
 		
 		//print screen and add listener
 		for(int i=0;i<42;i++)
 		{
 			ShowDays[i]=new JTextField();
 			ShowDays[i].addMouseListener(this);
 			ShowDays[i].setEditable(false);
 			LeftCenter.add(ShowDays[i]);
 		 }
 		 
 		 //print the body of month
 		 PrintMonth(year,month,day);
 		 
 		 
 		 
 		//the layout about right of jpane
 		RightPane = new JPanel(new BorderLayout());
 		JPanel RightCenter = new JPanel();
 		JPanel RightNorth = new JPanel();
		JPanel RightSouth = new JPanel(new FlowLayout());
		
		RightPane.add(RightNorth,BorderLayout.NORTH);
		RightPane.add(RightCenter,BorderLayout.CENTER);
		RightPane.add(RightSouth,BorderLayout.SOUTH);
		
		RightNorth.add(NorthMonthDayYear=new JLabel(">>"+SwitchMonth+","+day+","+year+"<<"));
		key=year+"_"+SwitchMonth+"_"+day;
		NorthMonthDayYear.setForeground(Color.blue);
 		NorthMonthDayYear.setFont(new Font("TimesRoman",Font.BOLD,17));
 		
		RightCenter.add(CenterText=new JTextArea("Today has not logs."));
		CenterText.setLineWrap(true);
		CenterText.setSelectedTextColor(Color.blue);
		//CenterText.addActionListener(this);
		RightSouth.add(SouthSave=new JButton("        Save        "));
		SouthSave.setBackground(Color.cyan);
		SouthSave.addActionListener(this);
		SouthSave.setActionCommand("Save");
		RightSouth.add(SouthDelete=new JButton("      Delete      "));
		SouthDelete.setBackground(Color.cyan);
		SouthDelete.addActionListener(this);
		SouthDelete.setActionCommand("Delete");
		
 		this.year = year;
 		this.month = month;
 		this.day = day;
 		
 		
 		///add container to put LeftPane and RightPane
 		Container con=getContentPane();
        JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,LeftPane,RightPane);
        con.add(split,BorderLayout.CENTER);
        con.validate();
 		
 		//add CenterPane to notepad
		//CenterPane initialize
		setFont(new Font("Times New Roman",Font.PLAIN,12));
		JScrollPane scrollpane = new JScrollPane(CenterText);
		scrollpane.setPreferredSize(new Dimension(220,250));
      	RightCenter.add(scrollpane);
      	
      	//init randomaccessfile
      	
 	}
 	
 	//switch the month in english
 	public void SwitchMonth(int month)
 	{
 		switch(month)
 		{
 			case 1:
 				SwitchMonth="Jan";break;
 			case 2:
 				SwitchMonth="Feb";break;
 			case 3:
 				SwitchMonth="Mar";break;
 			case 4:
 				SwitchMonth="Apr";break;
 			case 5:
 				SwitchMonth="May";break;
 			case 6:
 				SwitchMonth="Jun";break;
 			case 7:
 				SwitchMonth="Jul";break;
 			case 8:
 				SwitchMonth="Aug";break;
 			case 9:
 				SwitchMonth="Sep";break;
 			case 10:
 				SwitchMonth="Qct";break;
 			case 11:
 				SwitchMonth="Nov";break;
 			case 12:
 				SwitchMonth="Dec";break;
 			}
 	}
 	
 	//print the body of the month
 	public void PrintMonth(int year,int month,int day)
 	{
 		//Get start day of the week for the first date in the month
 		int startday = GetStartDay(year,month);
 		
 		//Get number of days in the month
 		int dayinmonth = GetNumOfDaysInMonth(year,month);
 		
 		//Print header
 		//PrintTitleAndScreen();
 		
 		//Print body
 		PrintMonthBody(startday,dayinmonth,day);
 	}
 	
 	//PrintMonth(int year,int month,int day)'s burden
 	public void PrintMonth(int year,int month)
 	{
 		//Get start day of the week for the first date in the month
 		int startday = GetStartDay(year,month);
 		
 		//Get number of days in the month
 		int dayinmonth = GetNumOfDaysInMonth(year,month);
 		
 		//Print header
 		//PrintTitleAndScreen();
 		
 		//Print body
 		PrintMonthBody(startday,dayinmonth);
 	}
 	
 	//PrintMonthBody(int startday,int dayinmonth,int day)'s burden
 	public void PrintMonthBody(int startday,int dayinmonth) 
 	{
 		for(int i=startday,n=1;i<startday+dayinmonth;i++)
 		{
 			ShowDays[i].setText(""+n);
 			ShowDays[i].setHorizontalAlignment(0);//let center
 			if(n==day)
            { 
                ShowDays[i].setForeground(Color.green); 
                ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,20));
                ShowDays[i].setBackground(Color.DARK_GRAY);
            }
            else
            { 
              	ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,12));
                ShowDays[i].setForeground(Color.white);
                ShowDays[i].setBackground(Color.DARK_GRAY);
            }
            n++;
 		}
 		
 		for(int i=0;i<startday;i++)
 		{
 			ShowDays[i].setText("");
 			ShowDays[i].setBackground(Color.DARK_GRAY);
 		}
 		
 		for(int i=startday+dayinmonth;i<42;i++)
 		{
 			ShowDays[i].setText("");
 			ShowDays[i].setBackground(Color.DARK_GRAY);
 		}
 	}
 	
 	public void PrintMonthBody(int startday,int dayinmonth,int day) 
 	{
 		for(int i=startday,n=1;i<startday+dayinmonth;i++)
 		{
 			ShowDays[i].setText(""+n);
 			ShowDays[i].setHorizontalAlignment(0);//let center
 			if(n==day)
            { 
                ShowDays[i].setForeground(Color.green); 
                ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,20));
                ShowDays[i].setBackground(Color.DARK_GRAY);
            }
            else
            { 
              	ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,12));
                ShowDays[i].setForeground(Color.white);
                ShowDays[i].setBackground(Color.DARK_GRAY);
            }
            n++;
 		}
 		
 		for(int i=0;i<startday;i++)
 		{
 			ShowDays[i].setText("");
 			ShowDays[i].setBackground(Color.DARK_GRAY);
 		}
 		
 		for(int i=startday+dayinmonth;i<42;i++)
 		{
 			ShowDays[i].setText("");
 			ShowDays[i].setBackground(Color.DARK_GRAY);
 		}
 	}
 	
 	//judge leapyear is or not
 	 public boolean IsLeapYear(int year)
 	{

⌨️ 快捷键说明

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