java实训报告

《Java实训》设计报告

项目名称:万年历记事本

石家庄科技信息职业学院

信息工程系

20##年5月30日

项目名称

一、项目需求

    整合 万年历和记事本的功能,提供重要事件记录功能

(1)提供万年历功能

(2)整合记录事件功能

(3)可以修改设置时间

(4)考虑闰年2月

二、总体设计

本系统共包括1个Java源文件。

Java源文件及其功能如表1所示。

三、详细设计

1.唯一类claendar

(1)成员变量见表2变量表

(2)方法见表3方法表

(3)源代码见文件calendar.java

四.运行效果

1、程序打开时的效果,左边是记事本框,右边是万年历框。颜色和布局都是自己精心调配的,很美观。

2、月份下拉列表的视图,选择谋月很方便的。这也是选择下拉列表,没用输入框的初衷。

3、任选一个日期,在此日期中输入日记的信息。效果如下图:

3、保存日历成功对话框,当写完日记,选择“save”按键后自动弹出此对话框。

4、当想删除所写日记是=时,会弹出此项确认对话框。此确认对话框可有效防止误删。

5、删除成功后会显示此对话框,提示删除成功。

五、项目总结

    本java文件整合记事本和万年历功能,而且可以添加重要事件功能,人性化的防误删功能,总体来讲,是一个比较全面的万年历记事本程序。

六、心得体会

    通过制作这个程序,使我又学到了很多的知识,尤其是运用Java知识去解决实际问题的能力又得到了很大的提高。尽管所设计的程序比较简单,但是程序中的每个功能的实现都经过了自己的一定认真的思考。由于把所学的知识做到很灵活的运用在实际中,对于现在的我还有一定的难度,故而,在设计的每个的环节中,都会多多少少的遇到一些问题,这就促使我去查阅各种资料,或者请教老师和同学,极大的提高我的专业素养,同时对在有些书上的理论知识在实际的运用,以及应该注意的问题有了比较深刻的认识。当然,仍然有些理论问题不是很好的去运用,或者是了解的不深刻。这就使的在实践中的所设计的程序有些功能不是自己所想的那样的完美。不过,既然发现了问题的所在,我就不会放弃的,直至问题得到解决。这些也同时反映出了平时运用知识去实践这方面做的太少。太缺乏经验。对所设计中将所面临的问题认识不够。是Java课程设计让我认识到了这些问题的存在。同时给我提供了去面对这些问题的平台。也使我充分的认识到在以后也应该怎样去实践,怎样去学习一门计算机语言,以及如何去运用这门语言去解决实际问题等等。

总之,在经过这次Java的课程设计,使我认识到了自己很多的缺点,同时,也是我学到新知识和新方法的一个机会。这给我在以后继续学习好这门语言指明了方向。

附录:calendar.java

import java.awt.*;

 import java.awt.event.*;

 import javax.swing.*;

 import javax.swing.event.*;

 import javax.swing.border.*;

 import java.util.Calendar;

 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 key;

   

    int changeyearmessage;

    int changemonthmessage;

   

    int priormonth;

    int prioryear;

   

    boolean ischange=false;

    boolean ischange_priornext=false;

   

    private JPanel LeftPane,RightPane;

   

    //Left sub

    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];

    private JButton ToToday;

    private JButton Query;

    private String week[]={"SUN","MON","TUE","WED","THU","FRI","SAT"};

   

    //right sub

    private JLabel NorthMonthDayYear;

    private JTextArea CenterText;

    private JButton SouthSave,SouthDelete; 

    private JButton PriorMonth;

    private JButton NextMonth;

   

   

   

    public calendar(int year,int month,int day)

    {

        setTitle("My Calendar&Textbook");

       

        //the layout about left of jpane

        LeftPane = new JPanel();

        JPanel LeftCenter = new JPanel();

        JPanel LeftNorth = new JPanel();

        //JPanel LeftSouth = new JPanel();

       

        LeftPane.setLayout(new BorderLayout());

        LeftPane.add(LeftNorth,BorderLayout.NORTH);

        LeftPane.add(LeftCenter,BorderLayout.CENTER);

        LeftPane.add(ToToday = new JButton("Go to today",new ImageIcon("../images/Handle.gif")),BorderLayout.SOUTH);

        ToToday.setBackground(Color.cyan);

        ToToday.addActionListener(this);

        LeftPane.validate();

       

        //the layout of LeftPane

        //LeftPane_North

        LeftNorth.setLayout(new GridLayout(3,1,0,-2));

        LeftNorth.add(Ask = new JLabel("  Plese input the informations which you want query:"));

        JPanel North = new JPanel(new FlowLayout(0,8,0));

        LeftNorth.add(North);

        North.add(YearLabel=new JLabel("Year:"));

        North.add(YearText = new JTextField(4));

        YearText.setBackground(Color.getHSBColor(30,20,50));

        YearText.setForeground(Color.blue);

        YearText.setFont(new Font("TimesRoman",Font.BOLD,17));

        YearText.addActionListener(this);

        YearText.setFocusable(true);

       

        North.add(Blank=new JLabel(" "));

        North.add(MonthLabel = new JLabel("Month:"));

        North.add(MonthCombobox = new JComboBox());

        //add month to monthcombobox

        for(int i=1;i<=12;i++)

        {

            MonthCombobox.addItem(new Integer(i));

        }

        //Switch the month

        MonthCombobox.setForeground(Color.blue);

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

        North.add(Blank=new JLabel(" "));

       

        North.add(Query=new JButton("Query"));

        Query.setForeground(Color.blue);

        Query.addActionListener(this);

       

        JPanel North2=new JPanel(new FlowLayout());

        LeftNorth.add(North2);

        North2.add(PriorMonth=new JButton(new ImageIcon("../images/prior.gif")));

        PriorMonth.addActionListener(this);

        PriorMonth.setActionCommand("prior");

        priormonth=month;

        prioryear=year;

       

        SwitchMonth(month);

        North2.add(ShowDate = new JLabel(SwitchMonth+" "+","+" "+String.valueOf(year),SwingConstants.CENTER));

        ShowDate.setForeground(Color.blue);

        ShowDate.setFont(new Font("TimesRoman",Font.BOLD,14));

        North2.add(NextMonth=new JButton(new ImageIcon("../images/next.gif")));

        NextMonth.addActionListener(this);

        NextMonth.setActionCommand("next");

       

       

        //LeftPane_Center

        LeftCenter.setLayout(new GridLayout(7,7));

        //print title

        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].setBackground(Color.MAGENTA );

            TopBarTitle[i].setBorder(BorderFactory.createRaisedBevelBorder());

            LeftCenter.add(TopBarTitle[i]);

        }

       

        //print screen and add listener

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

        {

            ShowDays[i]=new JTextField();

            ShowDays[i].addMouseListener(this);

            ShowDays[i].setEditable(false);

            LeftCenter.add(ShowDays[i]);

         }

         

         //print the body of month

         PrintMonth(year,month,day);

         

         

         

        //the layout about right of jpane

        RightPane = new JPanel(new BorderLayout());

        JPanel RightCenter = new JPanel();

        JPanel RightNorth = new JPanel();

        JPanel RightSouth = new JPanel(new FlowLayout());

       

        RightPane.add(RightNorth,BorderLayout.NORTH);

        RightPane.add(RightCenter,BorderLayout.CENTER);

        RightPane.add(RightSouth,BorderLayout.SOUTH);

       

        RightNorth.add(NorthMonthDayYear=new JLabel(">>"+year+","+SwitchMonth+","+day+"<<"));

        key=year+"_"+SwitchMonth+"_"+day;

        NorthMonthDayYear.setForeground(Color.blue);

        NorthMonthDayYear.setFont(new Font("TimesRoman",Font.BOLD,17));

       

        RightCenter.add(CenterText=new JTextArea("please write today's things."));

        CenterText.setLineWrap(true);

        CenterText.setSelectedTextColor(Color.blue);

        //CenterText.addActionListener(this);

        RightSouth.add(SouthSave=new JButton("        Save        "));

        SouthSave.setBackground(Color.cyan);

        SouthSave.addActionListener(this);

        SouthSave.setActionCommand("Save");

        RightSouth.add(SouthDelete=new JButton("      Delete      "));

        SouthDelete.setBackground(Color.cyan);

        SouthDelete.addActionListener(this);

        SouthDelete.setActionCommand("Delete");

       

        this.year = year;

        this.month = month;

        this.day = day;

       

       

        ///add container to put LeftPane and RightPane

        Container con=getContentPane();

        JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,RightPane,LeftPane);//gai bian

        con.add(split,BorderLayout.CENTER);

        con.validate();

       

        //add CenterPane to notepad

        //CenterPane initialize

        setFont(new Font("Times New Roman",Font.PLAIN,12));

        JScrollPane scrollpane = new JScrollPane(CenterText);

        scrollpane.setPreferredSize(new Dimension(220,250));

        RightCenter.add(scrollpane);

       

        //init randomaccessfile

       

    }

   

    //switch the month in english

    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;

            }

    }

   

    //print the body of the month

    public void PrintMonth(int year,int month,int day)

    {

        //Get start day of the week for the first date in the month

        int startday = GetStartDay(year,month);

       

        //Get number of days in the month

        int dayinmonth = GetNumOfDaysInMonth(year,month);

       

        //Print header

        //PrintTitleAndScreen();

       

        //Print body

        PrintMonthBody(startday,dayinmonth,day);

    }

   

    //PrintMonth(int year,int month,int day)'s burden

    public void PrintMonth(int year,int month)

    {

        //Get start day of the week for the first date in the month

        int startday = GetStartDay(year,month);

       

        //Get number of days in the month

        int dayinmonth = GetNumOfDaysInMonth(year,month);

       

        //Print header

       

        //Print body

        PrintMonthBody(startday,dayinmonth);

    }

   

    //PrintMonthBody(int startday,int dayinmonth,int day)'s burden

    public void PrintMonthBody(int startday,int dayinmonth)

    {

        for(int i=startday,n=1;i<startday+dayinmonth;i++)

        {

            ShowDays[i].setText(""+n);

            ShowDays[i].setHorizontalAlignment(0);//let center

            if(n==day)

            {

                ShowDays[i].setForeground(Color.green);

                ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,20));

                ShowDays[i].setBackground(Color.DARK_GRAY);

            }

            else

            {

                ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,12));

                ShowDays[i].setForeground(Color.white);

                ShowDays[i].setBackground(Color.DARK_GRAY);

            }

            n++;

        }

       

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

        {

            ShowDays[i].setText("");

            ShowDays[i].setBackground(Color.DARK_GRAY);

        }

       

        for(int i=startday+dayinmonth;i<42;i++)

        {

            ShowDays[i].setText("");

            ShowDays[i].setBackground(Color.DARK_GRAY);

        }

    }

   

    public void PrintMonthBody(int startday,int dayinmonth,int day)

    {

        for(int i=startday,n=1;i<startday+dayinmonth;i++)

        {

            ShowDays[i].setText(""+n);

            ShowDays[i].setHorizontalAlignment(0);//let center

            if(n==day)

            {

                ShowDays[i].setForeground(Color.green);

                ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,20));

                ShowDays[i].setBackground(Color.DARK_GRAY);

            }

            else

            {

                ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,12));

                ShowDays[i].setForeground(Color.white);

                ShowDays[i].setBackground(Color.DARK_GRAY);

            }

            n++;

        }

       

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

        {

            ShowDays[i].setText("");

            ShowDays[i].setBackground(Color.DARK_GRAY);

        }

       

        for(int i=startday+dayinmonth;i<42;i++)

        {

            ShowDays[i].setText("");

            ShowDays[i].setBackground(Color.DARK_GRAY);

        }

    }

   

    //judge leapyear is or not

     public boolean IsLeapYear(int year)

    {

        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(">>"+year+SwitchMonth+","+day+","+"<<");

               

                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)

   {

       

    }

   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);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(560,300);

        frame.setLocation(100,100);

        frame.setVisible(true);

        frame.setResizable(false);

        frame.ReadRecord();

    }

 }

相关推荐