java 備忘錄編寫源代碼及運行界面截圖參考模板_第1頁
java 備忘錄編寫源代碼及運行界面截圖參考模板_第2頁
java 備忘錄編寫源代碼及運行界面截圖參考模板_第3頁
java 備忘錄編寫源代碼及運行界面截圖參考模板_第4頁
java 備忘錄編寫源代碼及運行界面截圖參考模板_第5頁
已閱讀5頁,還剩27頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、/Memorial.javaimport java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;import java.io.*;import java.util.*;import javax.imageio.ImageIO;import javax.swing.*;public class Memorial extends JFrame implements ActionListener/* * param args */private JLabel title,picture,mark,jl1,jl2,

2、jl3,jl4;private JTextField jt1,jt2,jt3;private JButton Ok ,Cancle;private static String name1 =null;private static String pass1 = null;/ 構(gòu)造函數(shù)public Memorial()title = new JLabel("我的備忘錄");picture = new JLabel(new ImageIcon("photos/3.jpg");/背景標簽/*mark = new JLabel(new ImageIcon(&quo

3、t;3.jpg"); */jl1 =new JLabel( "登錄名:");jl2 =new JLabel( "登錄密碼:");jl3 =new JLabel( );jl4 = new JLabel("看不清");jt1 = new JTextField();jt2 = new JTextField();jt3 = new JTextField();Ok = new JButton ("登錄");Cancle = new JButton("取消");/頁面布局 public void

4、launchFrame()JPanel p = (JPanel) getContentPane();jl3.setText( getcode() );jl3.setForeground(Color.cyan); /設置驗證碼的顏色jl3.setBorder(BorderFactory.createRaisedBevelBorder();/使驗證碼凸出顯示jl4.setForeground(Color.magenta);/設置“看不清”標簽的前景色為洋紅色jl4.addMouseListener(new addEvent(); /為“看不清”標簽添加鼠標監(jiān)聽器Ok.addActionListen

5、er(this); /為“登錄”按鈕添加監(jiān)聽器Cancle.addActionListener(this); /為“取消”按鈕添加監(jiān)聽器1 / 32Ok.setBorder(BorderFactory.createRaisedBevelBorder(); /使按鈕凸出顯示Cancle.setBorder(BorderFactory.createRaisedBevelBorder();/*設置背景圖片p.setLayout(null);/使用setBounds(),必須使布局為nullpicture.setBounds(0,0,500,330);p.add(picture);p.setOpaqu

6、e(false);/使內(nèi)容窗格透明 /把背景圖片添加到分層窗格的最底層作為背景getLayeredPane().add(picture,new Integer(Integer.MIN_VALUE);/*設置標題title.setForeground(Color.red);title.setFont(new Font("華文行楷",Font.BOLD,20);title.setBounds(190,20,120,40); p.add(title); /*設置圖標 BufferedImage icon = null; try icon = ImageIO.read( new F

7、ile( "photos/9.jpg" ) ); this.setIconImage(icon); catch( Exception e ) jl1.setBounds(140,80,100,25);p.add(jl1);jt1.setBounds(240,80,100,25);p.add(jt1);jl2.setBounds(140,120,100,25);p.add(jl2);jt2.setBounds(240,120,100,25);p.add(jt2);jt3.setBounds(140,160,70,25);p.add(jt3);jl3.setBounds(250

8、,160,40,25);p.add(jl3);jl4.setBounds(300, 160, 50, 25);p.add(jl4);Ok.setBounds(150,220,60,30);p.add(Ok);Cancle.setBounds(260,220,60,30);p.add(Cancle);setTitle("登錄界面");setBounds(300,200,500,330);setResizable(false);/窗口不能被拖動setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /獲取

9、驗證碼public String getcode()Random rd = new Random();String str =""int a = new int 4;for(int i = 0; i < 4; i+)ai=rd.nextInt(200);if(ai>=48&&ai<=57)|(ai>=65&&ai<=90)|(ai>=97&&ai<=122)str = str + (char)ai;else /獲取的字符不在范圍內(nèi),獲取未成功;i-; / i-,保證最終獲取到四位的驗證

10、碼 return str; /從文件中讀取正確的用戶名public static String seekname() throws IOExceptionInputStream fin1 = new FileInputStream ("message用戶名.txt");byte c = new byte10;int len=0;while (len = fin1.read(c)!=-1)name1 = new String (c,0,len);return name1; /從文件中讀取正確的登錄密碼public static String seekpass() throws

11、 IOExceptionInputStream fin2 = new FileInputStream ("message登錄密碼.txt");byte c = new byte10;int len=0;while (len = fin2.read(c)!=-1)pass1 = new String (c,0,len); return pass1;/判斷文本框是否為空public boolean isEmpty( JTextField jt )if( jt.getText().equals( "" )return true;return false;/判斷

12、操作是否非法,主要是針對各文本框而言的;若文本框為空,提示填寫信息public boolean isLegal()if( isEmpty( jt1 ) ) JOptionPane.showMessageDialog(null, "用戶名不能為空!" );return false;else if( isEmpty(jt2 ) )JOptionPane.showMessageDialog(null, "密碼不能為空!" );return false;else if( isEmpty( jt3) )JOptionPane.showMessageDialog(n

13、ull, "請輸入驗證碼!" );return false;elsereturn true; /按鈕事件處理Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubObject source = e.getSource();String name2 = jt1.getText();String pass2 = jt2.getText();String code2 = jt3.getText();String code1 = jl3.getText();if(sou

14、rce = Ok)if(isLegal() /調(diào)用isLegal()函數(shù),保證文本框不為空try /調(diào)用函數(shù)seekname(),seekpass(),文件可能不存在 if(name2.equals(seekname() if(pass2.equals(seekpass()if(code2.equalsIgnoreCase( code1 ) /在用戶登錄信息完全正確的條件下,進入備忘錄(含日歷)JOptionPane.showMessageDialog(null, "登錄成功!");Calendar calendar=Calendar.getInstance(); int

15、y=calendar.get(Calendar.YEAR); int m=calendar.get(Calendar.MONTH)+1; int d=calendar.get(Calendar.DAY_OF_MONTH); new CalendarPad(y,m,d); this.dispose();/關(guān)閉登錄界面elseJOptionPane.showMessageDialog(this,"驗證碼錯誤,請重新輸入!","錯誤提示",JOptionPane.ERROR_MESSAGE);jl3.setText( getcode() );elseJOpti

16、onPane.showMessageDialog(this,"密碼錯誤,請重新輸入!","錯誤提示",JOptionPane.ERROR_MESSAGE);elseJOptionPane.showMessageDialog(this,"您輸入的用戶名不存在,請重新輸入!","錯誤提示",JOptionPane.ERROR_MESSAGE); catch (HeadlessException | IOException e1) / TODO Auto-generated catch blocke1.printStac

17、kTrace();if(source = Cancle)/詢問對話框int choose =JOptionPane.showConfirmDialog(null, "確定取消登錄?","詢問",JOptionPane.YES_NO_OPTION); if( choose = JOptionPane.YES_OPTION ) System.exit(0); /退出登錄系統(tǒng) else if( choose = JOptionPane.NO_OPTION ) class addEvent extends MouseAdapterOverridepublic v

18、oid mouseClicked(MouseEvent e) / TODO Auto-generated method stub jl3.setText( getcode() );public static void main(String args)/ TODO Auto-generated method stub new Memorial().launchFrame();/year.javaimport javax.swing.*;import java.awt.*;import java.awt.event.*;public class Year extends JPanel/Actio

19、nListener接口 private int year; private JTextField showYear=null; private JButton nextYear,lastYear; private CalendarPad cal; public Year(CalendarPad c) showYear=new JTextField(4); this.cal=c; year=cal.getYear(); nextYear=new JButton("下年"); lastYear=new JButton("上年"); launch(); /布局

20、函數(shù) public void launch() showYear.setForeground(Color.blue); showYear.setFont(new Font("宋體",Font.BOLD,16); setLayout(new GridLayout(1,3,5,5); add(lastYear); add(showYear); add(nextYear); showYear.addActionListener(new addEvent(); lastYear.addActionListener(new addEvent(); nextYear.addAction

21、Listener(new addEvent(); lastYear.setBorder(BorderFactory.createRaisedBevelBorder();/使按鈕凸出 nextYear.setBorder(BorderFactory.createRaisedBevelBorder(); /年 public void setYear(int year) this.year=year; showYear.setText(""+year); public int getYear() return year; /事件類 class addEvent implement

22、s ActionListener Override public void actionPerformed(ActionEvent e) if(e.getSource()=lastYear) /對年份進行判斷操作 year=year-1; showYear.setText(""+year); cal.setYear(year); cal.setCal(year,cal.getMonth(); else if(e.getSource()=nextYear) year=year+1; showYear.setText(""+year); cal.setYea

23、r(year); cal.setCal(year,cal.getMonth(); else if(e.getSource() = showYear) try year=Integer.parseInt(showYear.getText(); /年份控制在一定范圍 if(year>=-10000&&year<=10000) showYear.setText(""+year); cal.setYear(year); cal.setCal(year,cal.getMonth(); else JOptionPane.showMessageDialog(n

24、ull,"輸入的年份有誤,請重新輸入!","錯誤提示",JOptionPane.ERROR_MESSAGE); showYear.setText(""); catch(NumberFormatException ee) JOptionPane.showMessageDialog(null,"請輸入正確的年份格式!","錯誤提示",JOptionPane.ERROR_MESSAGE); showYear.setText(""); /Month.java/對月份的選擇import

25、 javax.swing.*;import java.awt.*;import java.awt.event.*;public class Month extends JPanel /ActionListener接口 private int month; private JTextField showMonth=null; private JButton lastMonth,nextMonth; private CalendarPad cal; public Month( CalendarPad c ) this.cal = c; showMonth = new JTextField(); m

26、onth = c.getMonth(); nextMonth = new JButton("下月"); lastMonth = new JButton("上月"); launch(); /頁面布局 public void launch() showMonth.setEditable(false); /設置月份不可被編輯 showMonth.setForeground(Color.blue); showMonth.setFont(new Font("宋體",Font.BOLD,16); setLayout(new GridLayout(

27、1,3,5,5); add(lastMonth); add(showMonth); add(nextMonth); lastMonth.addActionListener(new addEvent(); nextMonth.addActionListener(new addEvent(); lastMonth.setBorder(BorderFactory.createRaisedBevelBorder(); nextMonth.setBorder(BorderFactory.createRaisedBevelBorder(); showMonth.setText(""+m

28、onth); public void setMonth(int month) if(month<=12&&month>=1) this.month=month; else this.month=1; showMonth.setText(""+month); public int getMonth() return month; / 事件處理 class addEvent implements ActionListener Override public void actionPerformed(ActionEvent e) if(e.getSou

29、rce()=lastMonth) if(month>=2) month=month-1; cal.setMonth(month); cal.setCal(cal.getYear(),month); else if(month=1) month=12; cal.setMonth(month); cal.setCal(cal.getYear(),month); showMonth.setText(""+month); else if(e.getSource()=nextMonth) if(month<12) month=month+1; cal.setMonth(m

30、onth); cal.setCal(cal.getYear(),month); else if(month=12) month=1; cal.setMonth(month); cal.setCal(cal.getYear(),month); showMonth.setText(""+month); / Clock.java/提取當前的年月日時分秒,時鐘import java.awt.*;import java.text.SimpleDateFormat;/SimpleDateFormat是一個以與語言環(huán)境相關(guān)的方式來格式化和分析日期的具體類import java.util.

31、Calendar;import javax.swing.JPanel; /* 1.創(chuàng)建線程一種方法是將類聲明為 Thread 的子類。該子類應重寫 Thread 類的 run 方法。 * 接下來可以分配并啟動該子類的實例。 * 另一種方法是聲明實現(xiàn) Runnable 接口的類,該類然后實現(xiàn) run 方法。 * 然后可以分配該類的實例,在創(chuàng)建 Thread 時作為一個參數(shù)來傳遞并啟動。 * 2.Canvas 組件為Component的一個子類,表示屏幕上一個空白矩形區(qū)域, * 應用程序可以在該區(qū)域內(nèi)繪圖,或者可以從該區(qū)域捕獲用戶的輸入事件。 * 應用程序必須為 Canvas 類創(chuàng)建子類,以獲得有

32、用的功能(如創(chuàng)建自定義組件)。 * 必須重寫 paint 方法,以便在 canvas 上執(zhí)行自定義圖形。 */class Clock extends Canvas implements Runnable CalendarPad cp; Thread t; /Multi-Thread(斜體)線程 是程序中的執(zhí)行線程。Java 虛擬機允許應用程序并發(fā)地運行多個執(zhí)行線程。 String time; public Clock(CalendarPad cp) this.cp=cp; setSize(280,40); setBackground( Color.pink ); t = new Thread(

33、 this ); /實例化線程 t.start(); /調(diào)用線程 Override public void run() while(true) try Thread.sleep(1000); /休眠1秒鐘 catch(InterruptedException e) System.out.println("異常"); this.repaint(1000); /重畫屏幕 /* Graphics 類是所有圖形上下文的抽象基類, * 允許應用程序可以在組件(已經(jīng)在各種設備上實現(xiàn)), * 以及閉屏圖像上,進行繪制。 */ /對paint函數(shù)進行重寫,在 canvas上執(zhí)行自定義圖形

34、public void paint( Graphics g ) /super.paintComponent(g); SimpleDateFormat sdf=new SimpleDateFormat(" yyyy'年'MM'月'dd'日'HH:mm:ss");/格式化時間顯示類型 Calendar now=Calendar.getInstance(); / time=sdf.format(now.getTime(); /format()格式化時間對象,得到當前日期和時間 g.setFont(new Font("華文行

35、楷",Font.BOLD,18); g.setColor(Color.red); g.drawString(time,50,25); /使用此圖形上下文的當前字體和顏色,在指定的坐標位置,繪制由指定 string 給定的文本 /CalendarPad.java/Calendar日歷記事本import java.util.Calendar;import javax.imageio.ImageIO;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedIma

36、ge;import java.io.File;public class CalendarPad extends JFrame int year,month,day; private JTextField showDay; private JLabel title; Calendar cal =Calendar.getInstance();/ 實例化Calendar int week; Note note=null; Month ChangeMonth; Year ChangeYear; String w="星期日","星期一","星期二&quo

37、t;,"星期三","星期四","星期五","星期六" private JPanel leftPanel,rightPanel; /構(gòu)造函數(shù) public CalendarPad( int year,int month,int day ) leftPanel=new JPanel(); /左側(cè)是日歷 rightPanel=new JPanel(); /右側(cè)顯示相應的記事 this.year=year; this.month=month; this.day=day; ChangeYear=new Year(this);

38、 ChangeYear.setYear(year); ChangeMonth=new Month(this); ChangeMonth.setMonth(month); launch(); /頁面布局 public void launch() JPanel leftCenter=new JPanel(); /左側(cè)放置日歷 JPanel leftNorth=new JPanel(); /右側(cè)放置事件 /*設置圖標*/ BufferedImage icon = null; try icon = ImageIO.read( new File( "photos/7.jpg" ) )

39、; this.setIconImage(icon); catch( Exception e ) /*左面板中間部分的面板布置*/ leftCenter.setLayout(new GridLayout(7,7); title=new JLabel7; showDay=new JTextField42; /day用不可被編輯的文本框顯示 for(int j=0;j<7;j+) titlej=new JLabel(); titlej.setText(wj); /“星期日” 到 “星期六” titlej.setBorder(BorderFactory.createRaisedBevelBord

40、er(); /創(chuàng)建一個具有凸出斜面邊緣的邊框,將組件當前背景色的較亮的色度用于突出顯示,較暗的色度用于陰影 leftCenter.add(titlej); title0.setForeground(Color.orange);/設置 “星期日” 字體顏色,設置前景色 title6.setForeground(Color.green);/設置 “星期六 ”字體顏色 for(int i=0;i<42;i+) showDayi=new JTextField(); showDayi.addMouseListener(new addEvent(); showDayi.setEditable(fal

41、se); /日期不可被編輯 leftCenter.add(showDayi); /*左面板北面部分的面板,由與年份和月份有關(guān)的組件組成*/ JPanel p = new JPanel(); p.setLayout(new GridLayout(1,2); p.add(ChangeYear); p.add(ChangeMonth); leftNorth.add(p); /*左側(cè)面板整體布置*/ leftPanel.setLayout( new BorderLayout() ); leftPanel.add( leftNorth,"North" ); leftPanel.add

42、( leftCenter,"Center" ); leftPanel.add( new Clock(this),"South"); /!調(diào)用Clock類實例,顯示當前時間 leftPanel.validate(); /*右側(cè)面板整體布局*/ rightPanel.add( new Label( " 文本框輸入年份,按Enter鍵確定(負數(shù)表示公元前)" ),"North") ; note=new Note(this); /當前日歷 rightPanel.add(note,"Center"); /

43、把note放在右側(cè)面板中間部分 setCal(year,month); /*備忘錄界面總布局*/ Container c = getContentPane();/獲得大內(nèi)容嵌板 JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel); c.add(split); /*JSplitPane 用于分隔兩個(只能兩個)Component, 使用 JSplitPane.HORIZONTAL_SPLIT 可讓分隔窗格中的兩個 Component 從左到右排列*/ /*Container c = g

44、etContentPane(); c.setLayout(new GridLayout(1,2); c.add(leftPanel); c.add(rightPanel); */ /此種添加方式,兩個Component之間沒有分隔 ,比較突兀 pack(); setTitle("備忘錄"); setVisible(true); setResizable(false);/窗口不能被拖動 setBounds(350,100,620,350); validate(); /鼠標事件處理,單擊某日期,獲取該日的備忘錄信息 class addEvent extends MouseAda

45、pter Override public void mousePressed( MouseEvent e) JTextField source=(JTextField)e.getSource(); try day = Integer.parseInt(source.getText(); note.setDay(day); note.setTimeLabel(year,month,day); /在右側(cè)面板上顯示點擊的時間 note.setText(null); note.getLog(year,month,day); /獲取該天的備忘錄信息 catch(Exception ee) /設置每月的天數(shù) public void setCal(int year,int month) cal.set(year,month-1,1); /調(diào)用Calendar類的set()函數(shù),設置年、月 week=cal.get(Calendar.DAY_OF_WE

溫馨提示

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

評論

0/150

提交評論