关于日历程序源码

 

类详细代码:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

 

public class CalendarWindow extends JFrame implements ActionListener,

MouseListener,FocusListener{

    int year,month,day;

    CalendarMessage calendarMessage;

    CalendarPad calendarPad;

    NotePad notePad;

    JTextField showYear,showMonth;

    JTextField showDay[];

    CalendarImage calendarImage;

    Clock clock;

    JButton nextYear,previousYear,nextMonth,previousMonth;

    JButton saveDailyRecord,deleteDailyRecord,readDailyRecord;

    File dir;

    Color backColor=Color.white;

    public CalendarWindow(){

       dir= new File("./dailyRecord");

       dir.mkdir();

       showDay = new JTextField[42];

       for(int i=0;i<showDay.length;i++){

           showDay[i]=new JTextField();

           showDay[i].setBackground(backColor);

           showDay[i].setLayout(new GridLayout(3,3));

           showDay[i].addMouseListener(this);

           showDay[i].addFocusListener(this);

       }

       calendarMessage = new CalendarMessage();

       calendarPad = new CalendarPad();

       notePad = new NotePad();

       Calendar calendar = Calendar.getInstance();

       calendar.setTime(new Date());

       year = calendar.get(Calendar.YEAR);

       month = calendar.get(Calendar.MONTH)+1;

       day = calendar.get(Calendar.DAY_OF_MONTH);

       calendarMessage.setYear(year);

       calendarMessage.setMonth(month);

       calendarMessage.setDay(day);

       calendarPad.setCalendarMessage(calendarMessage);

       calendarPad.setShowDayTextField(showDay);

       notePad.setShowMessage(year,month,day);

       calendarPad.showMonthCalendar();

       doMark();

       calendarImage = new CalendarImage();

       calendarImage.setImageFile(new File("image/flower.jpg"));

       clock = new Clock();

       JSplitPane splitV1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);

       JSplitPane splitV2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,notePad,clock);

       JSplitPane splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitV1,splitV2);

       add(splitH,BorderLayout.CENTER);

       showYear = new JTextField(""+year,6);

       showYear.setFont(new Font("TimesRoman",Font.BOLD,12));

       showYear.setHorizontalAlignment(JTextField.CENTER);

       showMonth = new JTextField(""+month,4);

       showMonth.setFont(new Font("TimesRoman",Font.BOLD,12));

       showMonth.setHorizontalAlignment(JTextField.CENTER);

       nextYear = new JButton("下年");

       previousYear = new JButton("上年");

       nextMonth = new JButton("下月");

       previousMonth = new JButton("上月");

       nextYear.addActionListener(this);

       previousYear.addActionListener(this);

       nextMonth.addActionListener(this);

       previousMonth.addActionListener(this);

       showYear.addActionListener(this);

       JPanel north = new JPanel();

       north.add(previousYear);

       north.add(showYear);

       north.add(nextYear);

       north.add(previousMonth);

       north.add(showMonth);

       north.add(nextMonth);

       add(north,BorderLayout.NORTH);

       saveDailyRecord = new JButton("保存日志");

       deleteDailyRecord = new JButton("删除日志");

       readDailyRecord = new JButton("读取日志");

        saveDailyRecord.addActionListener(this);

       deleteDailyRecord.addActionListener(this);

       readDailyRecord.addActionListener(this);

       JPanel pSouth = new JPanel();

       pSouth.add(saveDailyRecord);

       pSouth.add(deleteDailyRecord);

       pSouth.add(readDailyRecord);

       add(pSouth,BorderLayout.SOUTH);

       setVisible(true);

       setBounds(70,70,770,500);

       validate();

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent e){

       if(e.getSource()==nextYear){

           year++;

           showYear.setText(""+year);

           calendarMessage.setYear(year);

           calendarPad.setCalendarMessage(calendarMessage);

           calendarPad.showMonthCalendar();

           notePad.setShowMessage(year,month,day);

           doMark();

       }

       else if (e.getSource()==previousYear){

           year--;

           showYear.setText(""+year);

           calendarMessage.setYear(year);

           calendarPad.setCalendarMessage(calendarMessage);

           calendarPad.showMonthCalendar();

           notePad.setShowMessage(year,month,day);

           doMark();

       }

       else if (e.getSource()==nextMonth){

           month++;

           if(month>12) month=1;

           showMonth.setText(""+month);

           calendarPad.setCalendarMessage(calendarMessage);

           calendarPad.showMonthCalendar();

           notePad.setShowMessage(year,month,day);

           doMark();

       }

       else if (e.getSource()==previousMonth){

           month--;

           if(month<1) month=12;

           showMonth.setText(""+month);

           calendarPad.setCalendarMessage(calendarMessage);

           calendarPad.showMonthCalendar();

           notePad.setShowMessage(year,month,day);

           doMark();

       }

       else if (e.getSource()==showYear){

           String s = showYear.getText().trim();

           char a[] = s.toCharArray();

           boolean boo = false;

           for(int i = 0;i < a.length;i++){

              if(!(Character.isDigit(a[i])))

                  boo = true;

           }

           if(boo==true)

              JOptionPane.showMessageDialog(this,"你输入了非法年份","警告",

                                            JOptionPane.WARNING_MESSAGE);

           else if(boo==false)

              year = Integer.parseInt(s);

           showYear.setText(""+year);

           calendarMessage.setYear(year);

           calendarPad.setCalendarMessage(calendarMessage);

           calendarPad.showMonthCalendar();

           notePad.setShowMessage(year,month,day);

           doMark();

       }

       else if (e.getSource()== saveDailyRecord){

           notePad.save(dir,year,month,day);

           doMark();

       }

       else if(e.getSource()==deleteDailyRecord){

           notePad.delete(dir,year,month,day);

           doMark();

       }

       else if (e.getSource()==readDailyRecord){

           notePad.read(dir,year,month,day);

           doMark();

       }

    }

    public void mousePressed(MouseEvent e){

       JTextField text = (JTextField)e.getSource();

       String str = text.getText().trim();

       try{

           day = Integer.parseInt(str);

       }

       catch (NumberFormatException exp){}

       calendarMessage.setDay(day);

       notePad.setShowMessage(year,month,day);

    }

    public void mouseReleased(MouseEvent e){}

    public void mouseEntered(MouseEvent e){}

    public void mouseExited(MouseEvent e){}

    public void mouseClicked(MouseEvent e){}

    public void focusGained(FocusEvent e){

       Component com = (Component)e.getSource();

       com.setBackground(Color.pink);

    }

    public void focusLost(FocusEvent e){

       Component com = (Component)e.getSource();

       com.setBackground(backColor);

    }

    public void doMark(){

       for(int i=0;i<showDay.length;i++){

           showDay[i].removeAll();

           String str = showDay[i].getText().trim();

           try{

              int n = Integer.parseInt(str);

              if(isHaveDailyRecord(n)==true){

                  JLabel mess = new JLabel("yes");

                  mess.setFont(new Font("TimesRoman",Font.PLAIN,11));

                  mess.setForeground(Color.blue);

                  showDay[i].add(mess);

              }

           }

           catch (Exception exp){}

       }

       calendarPad.repaint();

       calendarPad.validate();

    }

    public boolean isHaveDailyRecord(int n){

       String key = ""+year+""+month+""+n;

       String []dayFile = dir.list();

       boolean boo = false;

       for(int k = 0;k<dayFile.length;k++){

           if(dayFile[k].equals(key+".txt")){

              boo = true;

              break;

           }

       }

       return boo;

    }

    public static void main(String args[]) {

       new CalendarWindow();

    }

}

 

CalendarPad

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class CalendarPad extends JPanel{

    int year,month,day;

    CalendarMessage calendarMessage;

    JTextField []showDay;

    JLabel title [];

    String [] week={"SUN 日","MON 一","TUE 二","WED 三","THU 四","FRI 五","SAT 六"};

    JPanel north,center;

    public CalendarPad(){

       setLayout(new BorderLayout());

       north=new JPanel();

       north.setLayout(new GridLayout(1,7));

       center=new JPanel();

       center.setLayout(new GridLayout(6,7));

       add(center,BorderLayout.CENTER);

       add(north,BorderLayout.NORTH);

       title=new JLabel[7];

       for(int j=0;j<7;j++){

           title[j]=new JLabel();

           title[j].setFont(new Font("TimesRoman",Font.BOLD,12));

           title[j].setText(week[j]);

           title[j].setHorizontalAlignment(JLabel.CENTER);

           title[j].setBorder(BorderFactory.createRaisedBevelBorder());

           north.add(title[j]);

       }

       title[0].setForeground(Color.red);

       title[6].setForeground(Color.blue);

    }

    public void setShowDayTextField(JTextField [] text){

       showDay=text;

       for(int i=0;i<showDay.length;i++){

           showDay[i].setFont(new Font("TimesRoman",Font.BOLD,15));

           showDay[i].setHorizontalAlignment(JTextField.CENTER);

           showDay[i].setEditable(false);

           center.add(showDay[i]);

       }

    }

    public void setCalendarMessage(CalendarMessage calendarMessage){

       this.calendarMessage=calendarMessage;

    }

    public void showMonthCalendar(){

       String [] a=calendarMessage.getMonthCalendar();

       for(int i=0;i<42;i++)

           showDay[i].setText(a[i]);

       validate();

    }

}

 

NotePad

import java.awt.*;

import javax.swing.*;

import java.io.*;

import java.awt.event.*;

public class NotePad extends JPanel implements ActionListener{

    JTextArea text;

    JTextField showMessage;

    JPopupMenu menu;

    JMenuItem itemCopy,itemCut,itemPaste,itemClear;

    public NotePad(){

       showMessage=new JTextField();

       showMessage.setHorizontalAlignment(JTextField.CENTER);

       showMessage.setFont(new Font("TimesRoman",Font.BOLD,16));

       showMessage.setForeground(Color.blue);

       showMessage.setBackground(Color.pink);

       showMessage.setBorder(BorderFactory.createRaisedBevelBorder());

       showMessage.setEditable(false);

       menu =new JPopupMenu();

       itemCopy=new JMenuItem("复制");

       itemCut=new JMenuItem("剪切");

       itemPaste=new JMenuItem("粘贴");

       itemClear=new JMenuItem("清空");

       itemCopy.addActionListener(this);

       itemCut.addActionListener(this);

        itemPaste.addActionListener(this); 

        itemClear.addActionListener(this);

       menu.add(itemCopy);

        menu.add(itemCut);

       menu.add(itemPaste);

       menu.add(itemClear);

       text=new JTextArea(10,10);

       text.addMouseListener(new MouseAdapter(){

           public void mousePressed(MouseEvent e){

              if(e.getModifiers()==InputEvent.BUTTON3_MASK)

                  menu.show(text,e.getX(),e.getY());

           }

       });

       setLayout(new BorderLayout());

       add(showMessage,BorderLayout.NORTH);

       add(new JScrollPane(text),BorderLayout.CENTER);

    }

    public void setShowMessage(int year,int month, int day){

       showMessage.setText(""+year+"年"+month+"月"+day+"日");

    }

    public void save(File dir,int year,int month,int day){

       String dailyContent=text.getText();

       String fileName=""+year+""+month+""+day+""+".txt";

       String key=""+year+""+month+""+day;

       String [] dayFile=dir.list();

       boolean boo=false;

       for(int k=0;k<dayFile.length;k++){

           if(dayFile[k].startsWith(key)){

              boo=true;

              break;

           }

       }

       if(boo){

           String m=""+year+"年"+month+"月"+day+"已有日志,将新内容添加到日志吗?";

           int ok=JOptionPane.showConfirmDialog(this, m, "询问", JOptionPane.YES_NO_OPTION,

                                                JOptionPane.QUESTION_MESSAGE);

           if(ok==JOptionPane.YES_OPTION){

              try{

                  File f=new File(dir,fileName);

                  RandomAccessFile out=new RandomAccessFile(f,"rw");

                  long fileEnd=out.length();

                  byte [] bb=dailyContent.getBytes();

                  out.seek(fileEnd);

                  out.write(bb);

                  out.close();

              }

              catch(IOException exp){}

           }

       }

       else {

           String m=""+year+"年"+month+"月"+day+"还没有日志,保存日志吗?";

           int ok=JOptionPane.showConfirmDialog(this, m, "询问",JOptionPane.YES_NO_OPTION,

                                                JOptionPane.QUESTION_MESSAGE);

           if(ok==JOptionPane.YES_OPTION){

              try{

                  File f=new File(dir,fileName);

                  RandomAccessFile out= new RandomAccessFile(f,"rw");

                  long fileEnd=out.length();

                  byte []bb=dailyContent.getBytes();

                  out.write(bb);

                  out.close();

              }

              catch(IOException exp){}

           }

       }

    }

    public void delete(File dir,int year,int month,int day){

       String key =""+year+""+month+""+day;

       String [] dayFile=dir.list();

       boolean boo=false;

           for(int k=0;k<dayFile.length;k++){

              if(dayFile[k].startsWith(key)){

                  boo=true;

                  break;

              }

           }

           if(boo){

              String m="删除"+year+"年"+month+"月"+day+"日的日志吗?";

              int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

                if(ok==JOptionPane.YES_OPTION){

                  String fileName=""+year+""+month+""+day+""+".txt";

                  File deleteFile=new File(dir,fileName);

                  deleteFile.delete();

              }

           }

           else{

              String m=""+year+"年"+month+"月"+day+"无日志记录";

              JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE);

           }

    }

    public void read(File dir,int year,int month,int day){

       String fileName=""+year+""+month+""+day+""+".txt";

       String key=""+year+""+month+""+day;

       String [] dayFile=dir.list();

       boolean boo=false;

       for(int k=0;k<dayFile.length;k++){

           if(dayFile[k].startsWith(key)){

              boo=true;

              break;

           }

       }

       if(boo){

           String m=""+year+"年"+month+"月"+day+"有日志,显示日志内容吗?";

           int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

           if(ok==JOptionPane.YES_OPTION){

              text.setText(null);

              try{

                  File f=new File(dir,fileName);

                  FileReader inOne=new FileReader(f);

                  BufferedReader inTwo=new BufferedReader(inOne);

                  String s=null;

                  while ((s=inTwo.readLine())!=null)

                     text.append(s+"\n");

                  inOne.close();

                  inTwo.close();

              }

              catch(IOException exp){}

           }

       }

       else{

           String m=""+year+"年"+month+"月"+day+"无日志记录";

              JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE);

       }

    }

    public void actionPerformed(ActionEvent e){

       if(e.getSource()==itemCopy)

         text.copy();

       else if(e.getSource()==itemCut)

           text.cut();

       else if(e.getSource()==itemPaste)

           text.paste();

       else if(e.getSource()==itemClear)

           text.setText(null);

    }

}

 

CalendarImage

 

import javax.swing.*;

import java.io.*;

import java.awt.*;

public class CalendarImage extends JPanel{

    File imageFile;

    Image image;

    Toolkit tool;

    CalendarImage(){

       tool = getToolkit();

    }

    public void setImageFile(File f){

       imageFile = f;

       try{

           image = tool.getImage(imageFile.toURI().toURL());

       }

       catch (Exception e){}

       repaint();

    }

    public void paintComponent(Graphics g){

       super.paintComponent(g);

       int w = getBounds().width;

       int h = getBounds().height;

       g.drawImage(image,0,0,w,h,this);

    }

}

 

Clock类

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

import java.util.*;

public class Clock extends JPanel implements ActionListener{

    Date date;

    javax.swing.Timer secondTime;

    int hour,munite,second;

    Line2D secondLine,muniteLine,hourLine;

    int a,b,c,width,height;

    double pointSX[] = new double[60],

           pointSY[] = new double[60],

           pointMX[] = new double[60],

           pointMY[] = new double[60],

           pointHX[] = new double[60],

           pointHY[] = new double[60];

    Clock(){

       setBackground(Color.green);

       initPoint();

       secondTime = new javax.swing.Timer(1000,this);

       secondLine = new Line2D.Double(0,0,0,0);

       muniteLine = new Line2D.Double(0,0,0,0);

       hourLine = new Line2D.Double(0,0,0,0);

       secondTime.start();

    }

private void initPoint(){

       width = getBounds().width;

       height = getBounds().height;

       pointSX[0] = 0;

       pointSY[0] = -(height/2*5/6);

       pointMX[0] = 0;

       pointMY[0] = -(height/2*4/5);

       pointHX[0] = 0;

       pointHY[0] = -(height/2*2/3);

       double angle = 6*Math.PI/180;

       for(int i = 0;i<59;i++){

           pointSX[i+1] = pointSX[i]*Math.cos(angle) - Math.sin(angle)*pointSY[i];

           pointSY[i+1] = pointSY[i]*Math.cos(angle) + pointSX[i]*Math.sin(angle);

           pointMX[i+1] = pointMX[i]*Math.cos(angle) - Math.sin(angle)*pointMY[i];

           pointMY[i+1] = pointMY[i]*Math.cos(angle) + pointMX[i]*Math.sin(angle);

           pointHX[i+1] = pointHX[i]*Math.cos(angle) - Math.sin(angle)*pointHY[i];

           pointHY[i+1] = pointHY[i]*Math.cos(angle) - pointHX[i]*Math.sin(angle);

       }

       for(int i = 0;i<60;i++){

           pointSX[i] = pointSX[i]+width/2;

           pointSY[i] = pointSY[i]+height/2;

           pointMX[i] = pointMX[i]+width/2;

           pointMY[i] = pointMY[i]+height/2;

           pointHX[i] = pointHX[i]+width/2;

           pointHY[i] = pointHY[i]+height/2;

       }

    }

    public void paintComponent(Graphics g){

       super.paintComponent(g);

       initPoint();

       for(int i = 0;i<60;i++){

           int m = (int)pointSX[i];

           int n = (int)pointSY[i];

           if(i%5 ==0){

              if(i==0||i==15||i==30||i==45){

                  int k = 10;

                  g.setColor(Color.orange);

                  g.fillOval(m-k/2,n-k/2,k,k);

              }

              else{

                  int k = 7;

                  g.setColor(Color.orange);

                  g.fillOval(m-k/2,n-k/2,k,k);

              }

           }

           else{

              int k =2;

              g.setColor(Color.black);

              g.fillOval(m-k/2,n-k/2,k,k);

           }

       }

       g.fillOval(width/2-5,height/2-5,10,10);

       Graphics2D g_2d = (Graphics2D)g;

       g_2d.setColor(Color.red);

       g_2d.draw(secondLine);

       BasicStroke bs = new BasicStroke(2f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);

       g_2d.setStroke(bs);

       g_2d.setColor(Color.blue);

       g_2d.draw(muniteLine);

       bs = new BasicStroke(4f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);

       g_2d.setStroke(bs);

       g_2d.setColor(Color.orange);

       g_2d.draw(hourLine);

       bs = new BasicStroke(8f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);

       g_2d.setStroke(bs);

    }

    public void actionPerformed(ActionEvent e){

       if(e.getSource()== secondTime){

           date = new Date();

           String s = date.toString();

           hour = Integer.parseInt(s.substring(11,13));

           munite = Integer.parseInt(s.substring(14,16));

           second = Integer.parseInt(s.substring(17,19));

           int h = hour%12;

           a = second;

           b = munite;

           c = h*5+munite/12;

           secondLine.setLine(width/2,height/2,(int)pointSX[a],(int)pointSY[a]);

           muniteLine.setLine(width/2,height/2,(int)pointMX[b],(int)pointMY[b]);

           hourLine.setLine(width/2,height/2,(int)pointHX[c],(int)pointHY[c]);

           repaint();

       }

    }

}

posted @ 2015-07-04 22:20  琴韵畔晨曦  阅读(836)  评论(1编辑  收藏  举报