您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 理论文章 > java-备忘录编写源代码及运行界面截图
//Memorial.javaimportjava.awt.*;importjava.awt.event.*;importjava.awt.image.BufferedImage;importjava.io.*;importjava.util.*;importjavax.imageio.ImageIO;importjavax.swing.*;publicclassMemorialextendsJFrameimplementsActionListener{/***@paramargs*/privateJLabeltitle,picture,mark,jl1,jl2,jl3,jl4;privateJTextFieldjt1,jt2,jt3;privateJButtonOk,Cancle;privatestaticStringname1=null;privatestaticStringpass1=null;//构造函数publicMemorial(){title=newJLabel(我的备忘录);picture=newJLabel(newImageIcon(photos/3.jpg));//背景标签/*mark=newJLabel(newImageIcon(3.jpg));*/jl1=newJLabel(登录名:);jl2=newJLabel(登录密码:);jl3=newJLabel();jl4=newJLabel(看不清);jt1=newJTextField();jt2=newJTextField();jt3=newJTextField();Ok=newJButton(登录);Cancle=newJButton(取消);}//页面布局publicvoidlaunchFrame(){JPanelp=(JPanel)getContentPane();jl3.setText(getcode());jl3.setForeground(Color.cyan);//设置验证码的颜色jl3.setBorder(BorderFactory.createRaisedBevelBorder());//使验证码凸出显示jl4.setForeground(Color.magenta);//设置“看不清”标签的前景色为洋红色jl4.addMouseListener(newaddEvent());//为“看不清”标签添加鼠标监听器Ok.addActionListener(this);//为“登录”按钮添加监听器Cancle.addActionListener(this);//为“取消”按钮添加监听器Ok.setBorder(BorderFactory.createRaisedBevelBorder());//使按钮凸出显示Cancle.setBorder(BorderFactory.createRaisedBevelBorder());//****设置背景图片p.setLayout(null);//使用setBounds(),必须使布局为nullpicture.setBounds(0,0,500,330);p.add(picture);p.setOpaque(false);//使内容窗格透明//把背景图片添加到分层窗格的最底层作为背景getLayeredPane().add(picture,newInteger(Integer.MIN_VALUE));//****设置标题title.setForeground(Color.red);title.setFont(newFont(华文行楷,Font.BOLD,20));title.setBounds(190,20,120,40);p.add(title);//****设置图标BufferedImageicon=null;try{icon=ImageIO.read(newFile(photos/9.jpg));this.setIconImage(icon);}catch(Exceptione){}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,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);}//获取验证码publicStringgetcode(){Randomrd=newRandom();Stringstr=;inta[]=newint[4];for(inti=0;i4;i++){a[i]=rd.nextInt(200);if((a[i]=48&&a[i]=57)||(a[i]=65&&a[i]=90)||(a[i]=97&&a[i]=122)){str=str+(char)a[i];}else{//获取的字符不在范围内,获取未成功;i--;//i--,保证最终获取到四位的验证码}}returnstr;}//从文件中读取正确的用户名publicstaticStringseekname()throwsIOException{InputStreamfin1=newFileInputStream(message\\用户名.txt);bytec[]=newbyte[10];intlen=0;while((len=fin1.read(c))!=-1){name1=newString(c,0,len);}returnname1;}//从文件中读取正确的登录密码publicstaticStringseekpass()throwsIOException{InputStreamfin2=newFileInputStream(message\\登录密码.txt);bytec[]=newbyte[10];intlen=0;while((len=fin2.read(c))!=-1){pass1=newString(c,0,len);}returnpass1;}//判断文本框是否为空publicbooleanisEmpty(JTextFieldjt){if(jt.getText().equals()){returntrue;}returnfalse;}//判断操作是否非法,主要是针对各文本框而言的;若文本框为空,提示填写信息publicbooleanisLegal(){if(isEmpty(jt1)){JOptionPane.showMessageDialog(null,用户名不能为空!);returnfalse;}elseif(isEmpty(jt2)){JOptionPane.showMessageDialog(null,密码不能为空!);returnfalse;}elseif(isEmpty(jt3)){JOptionPane.showMessageDialog(null,请输入验证码!);returnfalse;}else{returntrue;}}//按钮事件处理@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubObjectsource=e.getSource();Stringname2=jt1.getText();Stringpass2=jt2.getText();Stringcode2=jt3.getText();Stringcode1=jl3.getText();if(source==Ok){if(isLegal()){//调用isLegal()函数,保证文本框不为空try{//调用函数seekname(),seekpass(),文件可能不存在if(name2.equals(seekname())){if(pass2.equals(seekpass())){if(code2.equalsIgnoreCase(code1)){//在用户登录信息完全正确的条件下,进入备忘录(含日历)JOptionPane.showMessageDialog(null,登录成功!);Calendarcalendar=Calendar.getInstance();inty=calendar.get(Calendar.YEAR);intm=calendar.get(Calendar.MONTH)+1;intd=calendar.get(Calendar.DAY_OF_MONTH);newCalendarPad(y,m,d);this.dispose();//关闭登录界面}else{JOptionPane.showMessageDialog(this,验证码错误,请重新输入!,错误提示,JOptionPane.ERROR_MESSAGE);jl3.setText(getcode());}}else{JOptionPane.showMessageDialog(this,密码错误,请重新输入!,错误提示,JOptionPane.ERROR_MESSAGE);}}else{JOptionPane.showMessageDialog(this,您输入的用户名不存在,请重新输入!,错误提示,JOptionPane.ERROR_MESSAGE);}}catch(HeadlessException|IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}}}if(source==Cancle){//询问对话框intchoose=JOptionPane.showConfirmDialog(null,确定取消登录?,询问,JOptionPane.YES_NO_OPTION);if(choose==JOptionPane.YES_OPTION)System.exit(0);//退出登录系统elseif(choose==JOptionPane.NO_OPTION){}}}classaddEventextendsMouseAdapter{@OverridepublicvoidmouseClicked(MouseEvente){//TODOAuto-generatedmethodstubjl3.setText(getcode());}}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubnewMemorial().launchFrame();}}//year.javaimportjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassYearextendsJPanel//ActionListener接口{privateintyear;privateJTextFieldshowYear=null;privateJButtonnextYear,lastYear;privateCalendarPadcal;publicYear(C
本文标题:java-备忘录编写源代码及运行界面截图
链接地址:https://www.777doc.com/doc-6373289 .html