📄 calendar.java
字号:
if((year%400==0)||(year%4==0&&year%100!=0))
return true;
else
return false;
}
//judge the start day of a month
public int GetStartDay(int year,int month)
{
//get total number of day since1/1/0000
int startday0001=-32768;
long totalnumofdays=GetTotalNumOfDays(year,month);
//return the start day
return (int)((totalnumofdays+startday0001)%7);
}
//judge the days of a year
public long GetTotalNumOfDays(int year,int month)
{
long total=0;
//get the total days from -32767 to year
for(int i=-32767;i<year;i++)
{
if(IsLeapYear(i))
total=total+366;
else
total=total+365;
}
//Add days from jan to the month prior to the calendar month
for(int i=1;i<month;i++)
total=total+GetNumOfDaysInMonth(year,i);
return total;
}
//judge the days of a month
public int GetNumOfDaysInMonth(int year,int month)
{
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||month==12)
return 31;
if(month==4 || month==6 || month==9 || month==11)
return 30;
if(month==2)
{
if(IsLeapYear(year))
return 29;
else
return 28;
}
return 0;
}
public void WriteRecord()
{
String content;
content=CenterText.getText();
int n=content.length();
char[] contentarr=new char[n];
try
{
int i=0;
for(i=0;i<n;i++)
{
contentarr[i]=content.charAt(i);
}
File Diary = new File("Diary");
Diary.mkdir();
File myfile=new File("Diary\\"+key+".txt");
FileWriter Record=new FileWriter(myfile);
for(i=0;i<contentarr.length;i++)
{
Record.write(contentarr[i]);
}
Record.close();
JOptionPane.showMessageDialog(this,"Save success!");
}
catch(IOException ex)
{
}
}
public void ReadRecord()
{
try
{
String content="";
File myfile=new File("Diary\\"+key+".txt");
FileReader Record=new FileReader(myfile);
if(myfile.exists())
{
long b=myfile.length();
//char[] contentarr=new char[b];
int n=JOptionPane.showConfirmDialog(this,"Today has logs,are you read?","Confirm",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION)
{
while((b=Record.read())!=-1)
{
content=content+(char)b;
}
CenterText.setText(content);
}
}
Record.close();
}
catch(IOException ex)
{
CenterText.setText("Today has not logs.");
}
}
public void DeleteFile()
{
String filepath="Diary\\"+key+".txt";
File myfile=new File(filepath);
int n=JOptionPane.showConfirmDialog(this,"Are you sure delete the file?","Confirm",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION)
{
if(myfile.exists())
{
Runtime rt = Runtime.getRuntime();
try
{
rt.exec("cmd /c del "+filepath);
}
catch (IOException e)
{
e.printStackTrace();
}
JOptionPane.showMessageDialog(this,"Delete successed!");
CenterText.setText("Today has not logs.");
}
else
{
JOptionPane.showMessageDialog(this,"The file doesn't exist,delete failured!");
}
}
}
public void AboutActionListenerWay()
{
try
{
prioryear=Integer.parseInt(YearText.getText());
priormonth=MonthCombobox.getSelectedIndex()+1;
String StrYearText=YearText.getText();
changeyearmessage=Integer.parseInt(StrYearText);
changemonthmessage=MonthCombobox.getSelectedIndex()+1;
monthafterquery=changemonthmessage;
yearafterquery=changeyearmessage;
SwitchMonth(changemonthmessage);
ShowDate.setText(SwitchMonth+" "+","+" "+String.valueOf(changeyearmessage));
PrintMonth(changeyearmessage,changemonthmessage);
ischange=true;
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this,"The input format doesn't match","Error",JOptionPane.ERROR_MESSAGE);
}
}
//do actonlistener things
public void actionPerformed(ActionEvent eAction)
{
String ActionCommand=eAction.getActionCommand();
//Handle button events
if(eAction.getSource() instanceof JButton)
{
//Handle the query
if("Query".equals(ActionCommand))
{
try
{
AboutActionListenerWay();
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this,"The input format doesn't match","Error",JOptionPane.ERROR_MESSAGE);
}
}
//Handle prior month
if("prior".equals(ActionCommand))
{
if(priormonth>1)
{
priormonth=priormonth-1;
}
else
{
priormonth=12;
prioryear=prioryear-1;
}
PrintMonth(prioryear,priormonth,day);
SwitchMonth(priormonth);
ShowDate.setText(SwitchMonth+" , "+prioryear);
NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<");
key=prioryear+"_"+SwitchMonth+"_"+day;
ischange_priornext=true;
}
//Handle next month
if("next".equals(ActionCommand))
{
if(priormonth<12)
{
priormonth=priormonth+1;
}
else
{
priormonth=1;
prioryear=prioryear+1;
}
PrintMonth(prioryear,priormonth,day);
SwitchMonth(priormonth);
ShowDate.setText(SwitchMonth+" , "+prioryear);
NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<");
key=prioryear+"_"+SwitchMonth+"_"+day;
ischange_priornext=true;
}
//Handle the "Go to today"
if("Go to today".equals(ActionCommand))
{
PrintMonth(year,month,day);
YearText.setText("");
MonthCombobox.setSelectedIndex(0);
SwitchMonth(month);
ShowDate.setText(SwitchMonth+" "+","+" "+String.valueOf(year));
NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+year+"<<");
key=year+"_"+SwitchMonth+"_"+day;
priormonth=month;
prioryear=year;
ischange=false;
}
//Handle the "Save"
if("Save".equals(ActionCommand))
{
WriteRecord();
}
if("Delete".equals(ActionCommand))
{
DeleteFile();
}
}
//Handle JTextField events
if(eAction.getSource() instanceof JTextField)
{
//Handle the query
AboutActionListenerWay();
}
}
public void mousePressed(MouseEvent eMouse)
{
int day;
try
{
//Handle ShowDays[] events
if(ischange==false)
{
JTextField source=(JTextField)eMouse.getSource();
day=Integer.parseInt(source.getText());
if(ischange_priornext==false)
{
NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+year+"<<");
key=year+"_"+SwitchMonth+"_"+day;
}
else
{
NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<");
key=prioryear+"_"+SwitchMonth+"_"+day;
}
}
else
{
JTextField source=(JTextField)eMouse.getSource();
day=Integer.parseInt(source.getText());
if(ischange_priornext==false)
{
SwitchMonth(changemonthmessage);
NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+changeyearmessage+"<<");
key=changeyearmessage+"_"+SwitchMonth+"_"+day;
}
else
{
SwitchMonth(priormonth);
NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<");
key=prioryear+"_"+SwitchMonth+"_"+day;
}
}
ReadRecord();
}
catch(Exception ee)
{
}
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void SaveLog(int year,int month,int day)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -