/**
* calendar.java
* auther jsp
* update 2006-12-21;
* company sichuanaixin
*/
import java.util.*;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import java.lang.Math;
import java.io.*;
import java.io.BufferedReader.*;
import java.io.FileReader.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.swing.JTextField;
class calendar extends JFrame
{
//变量定义区域
JFrame frame = new JFrame("日记本");
JTextArea view;
JScrollPane Span1;
JScrollPane Span2;
JPanel chooice;
JComboBox jmbyear;
JComboBox jmbmonth;
JButton jbt;
JButton jbtopen;
JButton jbtsave;
JTable table;
mytable model;
JTextField jtext;
String filename;
//calendar类构造方法
calendar()
{
try{
Jbinint();
}catch(Exception e)
{
e.printStackTrace();
}
}
//处世化方法
public void Jbinint()
{
//定义年
String[] yyear = {"1970","1971","1972","1973","1974","1975","1976","1977","1978","1979","1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990","1991","1992","1993","1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014"};
jmbyear = new JComboBox(yyear);
jmbyear.setAutoscrolls(true);
//定义月份
String[] mmonth = {"1","2","3","4","5","6","7","8","9","10","11","12"};
jmbmonth = new JComboBox(mmonth);
jmbmonth.setAutoscrolls(true);
jtext = new JTextField(3);
jtext.setEditable(false);
jbt = new JButton("查看");
jbtsave = new JButton("保存日记");
jbtopen = new JButton("日记");
chooice = new JPanel();
chooice.add(jmbyear);
chooice.add(jmbmonth);
chooice.add(jtext);
chooice.add(jbt);
chooice.add(jbtsave);
chooice.add(jbtopen);
frame.getContentPane().add(chooice,BorderLayout.NORTH);
model = new mytable();
table = new JTable(model);
Span2 = new JScrollPane(table);
frame.getContentPane().add(Span2,BorderLayout.CENTER);
view = new JTextArea(8,20);
view.setLineWrap(true);
Span1 = new JScrollPane(view);
frame.getContentPane().add(Span1,BorderLayout.SOUTH);
Calendar today = Calendar.getInstance();
int month = today.get(Calendar.MONTH)+1;
int year = today.get(Calendar.YEAR);
int day = today.get(Calendar.DATE);
GregorianCalendar todayWeek = new GregorianCalendar(year,month-1,1);
int week = todayWeek.get(Calendar.DAY_OF_WEEK)-1;
int days = count(month,year);
dis (days,week);
jmbyear.setSelectedItem(String.valueOf(new Integer(year)));
jmbmonth.setSelectedItem(String.valueOf(new Integer(month)));
//view.setText("日记文件:"+filename);
jtext.setText(String.valueOf(new Integer(day)));
filename = String.valueOf(new Integer(year))+"-"+String.valueOf(new Integer(month))+"-"+jtext.getText()+".txt";
File f = new File(filename );
if(f.exists())
{
if(f.isFile())
{
openDiary();
view.setEditable(false);
jbtsave.setEnabled(false);
}else{view.setText(String.valueOf(new Integer(year))+"-"+String.valueOf(new Integer(month))+"-"+jtext.getText()+":该日没有日记");
view.setEditable(true);jbtsave.setEnabled(true);
};
}else{view.setText(String.valueOf(new Integer(year))+"-"+String.valueOf(new Integer(month))+"-"+jtext.getText()+":该日没有日记");view.setEditable(true);jbtsave.setEnabled(true);}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,350);
frame.setVisible(true);
jbt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
model.moveall();
table.updateUI();
int years = Integer.parseInt(jmbyear.getSelectedItem().toString());
int months = Integer.parseInt(jmbmonth.getSelectedItem().toString());
GregorianCalendar today = new GregorianCalendar(years ,months-1,1);
int week = today.get(Calendar.DAY_OF_WEEK)-1;
int days = count(months,years);
dis (days,week);
}});
jbtsave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
saveDiary();
}});
jbtopen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(table.getSelectedRow() != -1 && table.getSelectedColumn() != -1){
jtext.setText((String)table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()));}
filename = jmbyear.getSelectedItem().toString()+"-"+jmbmonth.getSelectedItem().toString()+"-"+jtext.getText()+".txt";
File f = new File(filename );
if(f.exists())
{
if(f.isFile())
{
view.setText("");
openDiary();
view.setEditable(false);
jbtsave.setEnabled(false);
}else{view.setText(jmbyear.getSelectedItem().toString()+"-"+jmbmonth.getSelectedItem().toString()+"-"+jtext.getText()+":该日没有日记");view.setEditable(true);jbtsave.setEnabled(true);}
}else{view.setText(jmbyear.getSelectedItem().toString()+"-"+jmbmonth.getSelectedItem().toString()+"-"+jtext.getText()+":该日没有日记");view.setEditable(true);jbtsave.setEnabled(true);}
}});
}
//保存日记
public void saveDiary()
{
try{
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jmbyear.getSelectedItem().toString()+"年"+jmbmonth.getSelectedItem().toString()+"月"+jtext.getText()+"日");
bw.newLine();
bw.write(view.getText());
bw.flush();
bw.close();
fw.close();
}catch(Exception e){e.printStackTrace();}
}
public void openDiary()
{
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while(line != null)
{
view.setText(view.getText()+line+"/n/r");
line = br.readLine();
}
}catch(Exception e){e.printStackTrace();}
}
//判断是否是闰年
public boolean loop(int year)
{
if(((year % 4 ==0)&&(year % 100 !=0)) || (year % 400 ==0))
{
return true;
}
else
{
return false;
}
}
//确定每月的天数
public int count(int month,int year)
{
if(month == 2)
{
if(loop(year))
{
return 29;
}
else
{
return 28;
}
}
else if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
{
return 31;
}
else
{
return 30;
}
}
public void dis(int days,int week)
{
int weekcount = (int)Math.ceil((days +week-1)/7)+1;
int datevalue = 1;
String[][] dt = new String[weekcount][7];
for(int i = 0; i < weekcount;i++)
{
for(int j = 0; j < 7;j++)
{
if(i == 0 && j < week )
{
dt[i][j] ="";
}else
{
if(datevalue <= days)
{
dt[i][j] = String.valueOf(datevalue);
datevalue++;
}else
{
dt[i][j] ="";
}
}
}
}
model.addRow(dt,weekcount);
table.updateUI();
}
public static void main(String[] args)
{
new calendar();
}
}
class mytable extends AbstractTableModel
{
Vector content = null;
String[] title={"七","一","二","三","四","五","六"};
mytable()
{
content = new Vector();
}
public String getColumnName(int col)
{
return title[col];
}
public int getColumnCount()
{
return title.length;
}
public int getRowCount()
{
return content.size();
}
public boolean isCellEditable(int row, int col)
{
return false;
}
public void setValueAt(Object value, int row, int col)
{
((Vector)content.get(row)).remove(col);
((Vector)content.get(row)).add(col,value);
this.fireTableCellUpdated(row,col);
}
public Object getValueAt(int row,int col)
{
return ((Vector)content.get(row)).get(col);
}
public void addRow(String[][] d,int row)
{
for(int i=0 ; i < row; i ++ )
{
Vector c = new Vector(7);
c.add(0,d[i][0]);
c.add(1,d[i][1]);
c.add(2,d[i][2]);
c.add(3,d[i][3]);
c.add(4,d[i][4]);
c.add(5,d[i][5]);
c.add(6,d[i][6]);
content.add(c);
}
}
public void moveall()
{
content.clear();
}
}