《用Java实现日历记事本源代码.doc》由会员分享,可在线阅读,更多相关《用Java实现日历记事本源代码.doc(14页珍藏版)》请在金锄头文库上搜索。
1、CalendarPad类import java.util.Calendar;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.Hashtable;public class CalendarPad extends JFrame implements MouseListener int year,month,day; Hashtable hashtable; File file; JTextField showDay; JLabel title; Cale
2、ndar 日历; int 星期几; NotePad notepad=null; Month 负责改变月; Year 负责改变年; String 星期=星期日,星期一,星期二,星期三,星期四,星期五,星期六; JPanel leftPanel,rightPanel; public CalendarPad(int year,int month,int day) leftPanel=new JPanel(); JPanel leftCenter=new JPanel(); JPanel leftNorth=new JPanel(); leftCenter.setLayout(new GridLayo
3、ut(7,7); rightPanel=new JPanel(); this.year=year; this.month=month; this.day=day; 负责改变年=new Year(this); 负责改变年.setYear(year); 负责改变月=new Month(this); 负责改变月.setMonth(month); title=new JLabel7; showDay=new JTextField42; for(int j=0;j7;j+) titlej=new JLabel(); titlej.setText(星期j); titlej.setBorder(Border
4、Factory.createRaisedBevelBorder(); leftCenter.add(titlej); title0.setForeground(Color.red); title6.setForeground(Color.blue); for(int i=0;i42;i+) showDayi=new JTextField(); showDayi.addMouseListener(this); showDayi.setEditable(false); leftCenter.add(showDayi); 日历=Calendar.getInstance(); Box box=Box.
5、createHorizontalBox(); box.add(负责改变年); box.add(负责改变月); leftNorth.add(box); leftPanel.setLayout(new BorderLayout(); leftPanel.add(leftNorth,BorderLayout.NORTH); leftPanel.add(leftCenter,BorderLayout.CENTER); leftPanel.add(new Label(请在年份输入框输入所查年份(负数表示公元前),并回车确定), 6 / 25 BorderLayout.SOUTH) ; leftPanel
6、.validate(); Container con=getContentPane(); JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel,rightPanel); con.add(split,BorderLayout.CENTER); con.validate(); hashtable=new Hashtable(); file=new File(日历记事本.txt); if(!file.exists() try FileOutputStream out=new FileOutputStream(fi
7、le); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(hashtable); objectOut.close(); out.close(); catch(IOException e) notepad=new NotePad(this); rightPanel.add(notepad); 设置日历牌(year,month); 7 / 25 addWindowListener(new WindowAdapter() public void windowClosing(WindowEv
8、ent e) System.exit(0); ); setVisible(true); setBounds(100,50,524,285); validate(); public void 设置日历牌(int year,int month) 日历.set(year,month-1,1); 星期几=日历.get(Calendar.DAY_OF_WEEK)-1; if(month=1|month=2|month=3|month=5|month=7 |month=8|month=10|month=12) 排列号码(星期几,31); else if(month=4|month=6|month=9|mo
9、nth=11) 排列号码(星期几,30); else if(month=2) if(year%4=0&year%100!=0)|(year%400=0) 排列号码(星期几,29); 8 / 25 else 排列号码(星期几,28); public void 排列号码(int 星期几,int 月天数) for(int i=星期几,n=1;i星期几+月天数;i+) showDayi.setText(+n); if(n=day) showDayi.setForeground(Color.green); showDayi.setFont(new Font(TimesRoman,Font.BOLD,20
10、); else showDayi.setFont(new Font(TimesRoman,Font.BOLD,12); showDayi.setForeground(Color.black); if(i%7=6) showDayi.setForeground(Color.blue); if(i%7=0) showDayi.setForeground(Color.red); 9 / 25 n+; for(int i=0;i星期几;i+) showDayi.setText(); for(int i=星期几+月天数;i42;i+) showDayi.setText(); public int getYear() return year; public void setYear(int y) year=y; notepad.s