import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.Date;
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 SwitchNLMonth;
String key; //保存的文件名
String strtext="";
int changeyearmessage; //按钮转换的年
int changemonthmessage; //按钮转换的月
int priormonth; //自己想查看的年
int prioryear; //自己想查看的月
boolean ischange=false; //是否改变
boolean ischange_priornext=false; //是否改变当前
private JPanel LeftPane,RightPane; //定义左右两个面板
//左
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]; //7个星期的标签
private JLabel ToToday; //按钮(转换到今天)
private JButton Query; //按钮(转换)
private String week[]={"SUN","MON","TUE","WED","THU","FRI","SAT"}; //星期
//右
private JLabel NorthMonthDayYear; //显示年月日的标签
private JLabel NorthNLMonthDayYear; //显示农历的标签
private JTextArea CenterText; //多行文本框
private JButton SouthSave,SouthDelete; //按扭 (保存,删除)
private JButton PriorMonth; //前一个月
private JButton NextMonth; //下一个月
JPopupMenu pop=new JPopupMenu("弹出"); //定义右键弹出菜单
JMenuItem pundo=new JMenuItem("撤销");
JMenuItem pcut=new JMenuItem("剪切");
JMenuItem pcopy=new JMenuItem("复制");
JMenuItem ppaste=new JMenuItem("粘贴");
JMenuItem pdelete=new JMenuItem("删除");
JMenuItem pselectAll=new JMenuItem("全选");
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); //初始化calendar
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭按扭时退出程序
frame.setSize(560,350); //设置大小
frame.setLocation(200,200); //打开程序时窗口出现的位置
frame.setVisible(true); //设置可见
frame.setResizable(false); //设置窗口大小不能改变
frame.ReadRecord(); //读取当天保存的日记
}
public calendar(int year,int month,int day)
{
setTitle("欢迎使用我忘了日历记事本"); //设置标签
//添加文本框的右键的选项
pundo.setActionCommand("pundo");
pcut.setActionCommand("pcut");
pcopy.setActionCommand("pcopy");
ppaste.setActionCommand("ppaste");
pdelete.setActionCommand("pdelete");
pselectAll.setActionCommand("pselectAll");
pundo.addActionListener(this);
pcut.addActionListener(this);
pcopy.addActionListener(this);
ppaste.addActionListener(this);
pdelete.addActionListener(this);
pselectAll.addActionListener(this);
pop.add(pundo);
pop.addSeparator();
pop.add(pcut);
pop.add(pcopy);
pop.add(ppaste);
pop.add(pdelete);
pop.addSeparator();
pop.add(pselectAll);
pcut.setEnabled( false );
pcopy.setEnabled( false );
pdelete.setEnabled( false );
//左
LeftPane = new JPanel(); //实例化
//将左边的面板分成北和中两个小面板
JPanel LeftCenter = new JPanel(); //初始化左 中
JPanel LeftNorth = new JPanel(); //初始化左 北
SwitchMonth(month);//把月份转换成相对应的英文
LeftPane.setLayout(new BorderLayout()); //设置布局管理器 (默认)
LeftPane.add(LeftNorth,BorderLayout.NORTH); //添加左 北面板
LeftPane.add(LeftCenter,BorderLayout.CENTER); //添加左 中面板
ToToday=new JLabel(" 您好,今天是 "+SwitchMonth+","+day+","+year);
LeftPane.add(ToToday ,BorderLayout.SOUTH);//添加
LeftPane.validate(); //确保组件具有有效的布局
//设置布局 (左 北)
LeftNorth.setLayout(new GridLayout(3,1,0,-2)); //设置网个布局
LeftNorth.add(Ask = new JLabel(" 请输入你要查看的日期:")); //设置提示标签
JPanel North = new JPanel(new FlowLayout(0,8,0)); //一个接一个放入容器 间距为8
LeftNorth.add(North); //添加到面板中去
North.add(YearLabel=new JLabel("年:")); //添加年的标签
North.add(YearText = new JTextField(4)); //添加一个文本框
YearText.setForeground(Color.darkGray);
YearText.setFont(new Font("TimesRoman",Font.BOLD,17));//设置字体
YearText.addActionListener(this); //兼听
YearText.setFocusable(true); //是指使YearText能够被聚焦 光标放置的位置
North.add(Blank=new JLabel(" ")); //添加空白
North.add(MonthLabel = new JLabel("月:")); //添加月
North.add(MonthCombobox = new JComboBox());//添加月份的组合框
//在组合框里添加月份
for(int i=1;i<=12;i++)
{
MonthCombobox.addItem(new Integer(i));
}
//转换月份
MonthCombobox.setForeground(Color.darkGray); //设置颜色
MonthCombobox.setFont(new Font("TimesRoman",Font.BOLD,12)); //设置字体
North.add(Blank=new JLabel(" ")); //添加空白
North.add(Query=new JButton("转至"));
Query.addActionListener(this); //兼听
JPanel North2=new JPanel(new FlowLayout());//定义下一个棉板
LeftNorth.add(North2); //添加到总面板里去
North2.add(PriorMonth=new JButton("<<"));
PriorMonth.addActionListener(this);
PriorMonth.setActionCommand("prior");
priormonth=month;
prioryear=year;
North2.add(ShowDate = new JLabel(SwitchMonth+" "+","+" "+String.valueOf(year),SwingConstants.CENTER));
ShowDate.setFont(new Font("TimesRoman",Font.BOLD,14));
North2.add(NextMonth=new JButton(">>"));
NextMonth.addActionListener(this);
NextMonth.setActionCommand("next");
//左边中间面板
LeftCenter.setLayout(new GridLayout(7,7));
//输出标题
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].setBorder(BorderFactory.createRaisedBevelBorder()); //设置边界
LeftCenter.add(TopBarTitle[i]);
}
//添加日期框
for(int i=0;i<42;i++)
{
ShowDays[i]=new JTextField();
ShowDays[i].addMouseListener(this); //鼠标兼听
ShowDays[i].setEditable(false);//设置不可以编辑
LeftCenter.add(ShowDays[i]); // 添加这个
}
//输出月的部分
PrintMonthBody(year,month,day);
//右边布局
RightPane = new JPanel(new BorderLayout());
JPanel RightCenter = new JPanel(); //右中
JPanel RightNorth = new JPanel(new BorderLayout()); //右 北
JPanel RightSouth = new JPanel(new FlowLayout()); //右 南
JPanel RightNorthNorth= new JPanel();
JPanel RightCenterCenter= new JPanel();
//添加面板
RightPane.add(RightNorth,BorderLayout.NORTH);
RightPane.add(RightCenter,BorderLayout.CENTER);
RightPane.add(RightSouth,BorderLayout.SOUTH);
RightNorth.add(RightNorthNorth,BorderLayout.NORTH);
RightNorth.add(RightCenterCenter,BorderLayout.CENTER);
RightNorthNorth.add(NorthMonthDayYear=new JLabel("你查看的日期的阳历为:"+SwitchMonth+","+day+","+year));//添加标签
key=year+"_"+SwitchMonth+"_"+day;
//设置显示农历
int[] rNong = Change(year, month, day);
SwitchNLMonth(rNong[1]);
String NLmoth=SwitchNLMonth;
if(rNong[2]<11)
{
SwitchNLMonth(rNong[2]);
String NLday=SwitchNLMonth;
RightCenterCenter.add(NorthNLMonthDayYear=new JLabel("阴历为: "+rNong[0]+"年"+NLmoth+"月"+"初"+NLday));
}
else if(11<=rNong[2]&&rNong[2]<20)
{
SwitchNLMonth(rNong[2]-10);
String NLday=SwitchNLMonth;
RightCenterCenter.add(NorthNLMonthDayYear=new JLabel("阴历为: "+rNong[0]+"年"+NLmoth+"月"+"十"+NLday));
}
else if(20<=rNong[2]&&rNong[2]<30)
{
SwitchNLMonth(rNong[2]-20);
String NLday=SwitchNLMonth;
RightCenterCenter.add(NorthNLMonthDayYear=new JLabel("阴历为: "+rNong[0]+"年"+NLmoth+"月"+"二"+NLday));
}
else
{
SwitchNLMonth(rNong[2]-30);
String NLday=SwitchNLMonth;
RightCenterCenter.add(NorthNLMonthDayYear=new JLabel("阴历为: "+rNong[0]+"年"+NLmoth+"月"+"三"+NLday));
}
NorthMonthDayYear.setForeground(Color.darkGray);//设置出现日期的颜色
NorthMonthDayYear.setFont(new Font("TimesRoman",Font.BOLD,12)); //字体大小
NorthNLMonthDayYear.setFont(new Font("TimesRoman",Font.BOLD,12));
RightCenter.add(CenterText=new JTextArea("今天还没有写日记.")); //添加文本框
CenterText.setLineWrap(true); //是否自动换行
CenterText.addFocusListener( new Filechange());//注册 兼听鼠标显示右键
CenterText.addMouseListener( new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
{
pop.show(e.getComponent(),e.getX(),e.getY());
}
}
} );
//添加删除和保存
RightSouth.add(SouthSave=new JButton(" 保存 "));
SouthSave.addActionListener(this);
SouthSave.setActionCommand("保存");
RightSouth.add(SouthDelete=new JButton(" 删除 "));
SouthDelete.addActionListener(this);
SouthDelete.setActionCommand("删除");
//传递值
this.year = year;
this.month = month;
this.day = day;
//在容易器把左右面板都加进去
Container container=getContentPane(); //返回引发事件的容器
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,LeftPane,RightPane); //分割两个面板
container.add(split,BorderLayout.CENTER);
container.validate(); //确保有效性
setFont(new Font("Times New Roman",Font.PLAIN,12));
JScrollPane scrollpane = new JScrollPane(CenterText);//设置可以滚动的窗格
scrollpane.setPreferredSize(new Dimension(220,220)); //设置大小
RightCenter.add(scrollpane); //添加进去
}
//把月份转换成相对应的英文******************************************************
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;
}
}
/***********************************************************/
//数字转换成中文 为了表示农历
public void SwitchNLMonth(int month)
{
switch(month)
{
case 0:
SwitchNLMonth="十";break;
case 1:
SwitchNLMonth="一";break;
case 2:
SwitchNLMonth="二";break;
case 3:
SwitchNLMonth="三";break;
case 4:
SwitchNLMonth="四";break;
case 5:
SwitchNLMonth="五";break;
case 6:
SwitchNLMonth="六";break;
case 7:
SwitchNLMonth="七";break;
case 8:
SwitchNLMonth="八";break;
&n




下载