Java日歷記事本課程設計報告_第1頁
Java日歷記事本課程設計報告_第2頁
Java日歷記事本課程設計報告_第3頁
Java日歷記事本課程設計報告_第4頁
Java日歷記事本課程設計報告_第5頁
已閱讀5頁,還剩18頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Java 日歷記事本課程設計報告在設計日歷記事本時,需要編寫 6 個 JAVA 源文件: CalendarWindow.java、 CalendarPad.java 、 NotePad.java 、 CalendarImage.java 、 Clock.java 和 CalendarMesssage.java效果圖如下. CalendarWindow 類import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;public class CalendarWi

2、ndow extends JFrame implements ActionListener,MouseListener,FocusListenerint year,month,day;CalendarMessage calendarMessage;CalendarPad calendarPad;NotePad notePad;JTextField showYear,showMonth;JTextField showDay;CalendarImage calendarImage;String picturename;Clock clock;JButton nextYear,previousYea

3、r,nextMonth,previousMonth;JButton saveDailyRecord,deleteDailyRecord,readDailyRecord;JButton getPicture;File dir;Color backColor=Color.white ;public CalendarWindow()dir=new File(./dailyRecord);dir.mkdir();showDay=new JTextField42;for(int i=0;ishowDay.length;i+)showDayi=new JTextField();showDayi.setBa

4、ckground(backColor);showDayi.setLayout(new GridLayout(3,3);showDayi.addMouseListener(this);showDayi.addFocusListener(this);calendarMessage=new CalendarMessage();calendarPad=new CalendarPad();notePad=new NotePad();Calendar calendar=Calendar.getInstance();calendar.setTime(new Date();year=calendar.get(

5、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,m

6、onth,day);calendarPad.showMonthCalendar();doMark();calendarImage=new CalendarImage();calendarImage.setImageFile(new File(flower.jpg);clock=new Clock();JSplitPanesplitV1=newJSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);JSplitPane splitV2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,note

7、Pad,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); show

8、Month.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.addA

9、ctionListener(this);previousMonth.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=ne

10、w 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,Bo

11、rderLayout.SOUTH);getPicture=new JButton(選擇日歷圖像 );getPicture.addActionListener(this);pSouth.add(getPicture);add(pSouth,BorderLayout.SOUTH);setVisible(true);setBounds(60,60,660,480);validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e)if(e.getSource()=ne

12、xtYear)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);calendarPa

13、d.setCalendarMessage(calendarMessage);calendarPad.showMonthCalendar();notePad.setShowMessage(year,month,day);doMark();else if(e.getSource()=nextMonth)month+;if(month1) month=12;showMonth.setText(+month);calendarMessage.setMonth(month);calendarPad.setCalendarMessage(calendarMessage);calendarPad.showM

14、onthCalendar();notePad.setShowMessage(year,month,day);doMark();else if(e.getSource()=previousMonth)month-;if(month1) month=12;showMonth.setText(+month);calendarMessage.setMonth(month); calendarPad.setCalendarMessage(calendarMessage);calendarPad.showMonthCalendar();notePad.setShowMessage(year,month,d

15、ay);doMark();else if(e.getSource()=showYear)String s=showYear.getText().trim();char a=s.toCharArray();boolean boo=false;for(int i=0;ia.length;i+)if(!(Character.isDigit(ai)boo=true;if(boo=true)JOptionPane.showMessageDialog(this, 您輸入了非法年份 , 警告,JOptionPane.WARNING_MESSAGE);else if(boo=false)year=Intege

16、r.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()=de

17、leteDailyRecord)notePad.delete(dir,year,month,day);doMark();else if(e.getSource()=readDailyRecord)notePad.read(dir,year,month,day);else if (e.getSource() =getPicture ) FileDialog fd=new FileDialog(this, 打開文件對話框 ); fd.setVisible(true);String fileopen = null, filename = null;fileopen = fd.getDirectory

18、();filename = fd.getFile();calendarImage.setImageFile(new File(fileopen,filename);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.setShow

19、Message(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 focusL

20、ost(FocusEvent e)Component com=(Component)e.getSource();com.setBackground(backColor);public void doMark()for(int i=0;ishowDay.length;i+)showDayi.removeAll();String str=showDayi.getText().trim();tryint n=Integer.parseInt(str);if(isHaveDailyRecord(n)=true)JLabel mess=new JLabel(有 );mess.setFont(new Fo

21、nt(TimesRoman,Font.PLAIN,11); mess.setForeground(Color.blue); showDayi.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;kdayFile.length;k+)if(dayFilek.equ

22、als(key+.txt)boo=true;break;return boo;public String getPicture_address() String address = null;try InputStream outOne = new FileInputStream(picture_address.txt); ObjectInputStream outTwo = new ObjectInputStream(outOne); try address = (String) outTwo.readObject(); catch (Exception ex) outTwo.close()

23、; catch (IOException eee) if (address != null) return address; else return picture.jpg;public void actionPerformed1(ActionEvent e) if (e.getActionCommand().equals(更改圖片背景 ) FileDialog dia = new FileDialog(this, 選 擇 圖 片 , FileDialog.LOAD);dia.setModal(true);dia.setVisible(true);if (dia.getDirectory()

24、!= null) & (dia.getFile() != null) try FileOutputStream inOne = new FileOutputStream(picture_address.txt);ObjectOutputStream inTwo = new ObjectOutputStream(inOne);inTwo.writeObject(dia.getDirectory() + dia.getFile(); inTwo.close(); catch (IOException ee) String picturename = getPicture_address(); ca

25、lendarImage.setImageFile(new File(picturename);public static void main(String args)new CalendarWindow();CalendarPad類import javax.swing.*;import java.awt .*;import java.awt.event.*;import java.util.*;import javax.swing.JPanel;public class CalendarPad extends JPanelint year,month,day;CalendarMessage c

26、alendarMessage;JTextField showDay;JLabel title;String 星期 =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 ,BorderL

27、ayout.CENTER );add(north,BorderLayout.NORTH );title=new JLabel7;for(int j=0;j7;j+)titlej=new JLabel();titlej.setFont(new Font(TimesRoman,Font.BOLD ,12); titlej.setText( 星期 j);titlej.setHorizontalAlignment(JLabel.CENTER); titlej.setBorder(BorderFactory.createRaisedBevelBorder();north.add(titlej);titl

28、e0.setForeground(Color.red);title6.setForeground(Color.blue);public void setShowDayTextField(JTextFieldtext)showDay=text;for(int i=0;ishowDay.length;i+)showDayi.setFont(new Font(TimesRoman,Font.BOLD ,15);showDayi.setHorizontalAlignment(JTextField.CENTER);showDayi.setEditable(false);center.add(showDa

29、yi);public void setCalendarMessage (CalendarMessage calendarMessage) this.calendarMessage=calendarMessage;public void showMonthCalendar()String a=calendarMessage.getMonthCalendar();for(int i=0;i42;i+)showDayi.setText(ai);validate();CalendarMesssage類import java.util.Calendar;public class CalendarMess

30、age int year=-1,month=-1,day=-1;public int getYear()return year;public void setMonth(int month)if(month=1)this.month=month;elsethis.month =1;public int getMonth()return month;public void setDay(int day)this.day =day;public int getDay()return day ;public String getMonthCalendar()String day=new String

31、42;Calendar rili =Calendar.getInstance();rili.set(year, month-1,1);int 星期幾 =rili.get(Calendar.DAY_OF_WEEK )-1;int dayAmount=0;if(month=1|month=3|month=5|month=7|month=8|month=10|month=12)dayAmount=31;if(month=4|month=6|month=9|month=11)dayAmount=30;if(month=2)if(year%4=0)&(year%100!=0)|year%400=0)da

32、yAmount=29;elsedayAmount=28;for(int i=0;i 星期幾 ;i+)dayi=;for(int i= 星期幾 ,n=1;i 星期幾 +dayAmount;i+)dayi=String.valueOf(n);n+;for(int i= 星期幾 +dayAmount;i42;i+)dayi=;return day;public void setYear(int year) this.year = year;NotePad類import java.awt.*;import javax.swing.*;import javax.swing.border.Border;i

33、mport javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;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, bt

34、n;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();sh

35、owMessage.setEditable(false);menu = new JPopupMenu();itemCopy = new JMenuItem(復制 );itemCut = new JMenuItem(剪切 );itemPaste = new JMenuItem(粘貼 );itemClear = new JMenuItem(清空 );btn = new JMenuItem(字體 );itemCopy.addActionListener(this);itemCut.addActionListener(this);itemPaste.addActionListener(this);it

36、emClear.addActionListener(this);btn.addActionListener(this);menu.add(itemCopy);menu.add(itemCut);menu.add(itemPaste);menu.add(itemClear);menu.add(btn);text = new JTextArea(10, 10);text.addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent e) if (e.getModifiers() = InputEvent.BUTTON

37、3_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 + 日); showMessage.setForeground(C

38、olor.blue); showMessage.setFont(new Font(宋體 , Font.BOLD, 15);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

39、 k = 0; k dayFile.length; k+) if (dayFilek.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

40、 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.showConfirmDia

41、log(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(

42、); 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 (dayFilek.startsWith(key) boo = true;break;if (boo) String m = 刪除 + year + 年 + month +

43、月 + 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 + +

44、 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;

45、k dayFile.length; k+) if (dayFilek.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(d

46、ir, 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

47、(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);if (e.getSource() = btn) JF

48、ontDialog nFD = new JFontDialog(選擇字體 );nFD.setModal(true);nFD.setVisible(true);text.setFont(nFD.myFont);class JFontDialog extends JDialog private static final long serialVersionUID = 1L;JList fontpolics, fontstyle, fontsize;JTextField fontpolict, fontstylet, fontsizet;String example;JLabel FontResol

49、vent;JButton buttonok, buttoncancel;Font myFont;public JFontDialog(String title) Container container = getContentPane();container.setLayout(new BorderLayout();JPanel panel = new JPanel();panel.setLayout(new GridLayout(2, 1);JPanel FontSet, FontView;FontSet = new JPanel(new GridLayout(1, 4);FontView = new JPanel(new GridLayout(1, 2);example = AaBbCcDdEe;FontResolvent = new J

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論