版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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;/ 构造函数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(); /为“看不清”标签添加鼠标监听器Ok.addActionListen
5、er(this); /为“登录”按钮添加监听器Cancle.addActionListener(this); /为“取消”按钮添加监听器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);/使内容窗格透明 /把背景图片添加到分层窗格的最底层作为背景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 /获取的字符不在范围内,获取未成功;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() /调用isLegal()函数,保证文本框不为空try /调用函数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();/关闭登录界面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); /退出登录系统 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、函数 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是一个以与语言环境相关的方式来格式化和分析日期的具体类import java.util.
31、Calendar;import javax.swing.JPanel; /* 1.创建线程一种方法是将类声明为 Thread 的子类。该子类应重写 Thread 类的 run 方法。 * 接下来可以分配并启动该子类的实例。 * 另一种方法是声明实现 Runnable 接口的类,该类然后实现 run 方法。 * 然后可以分配该类的实例,在创建 Thread 时作为一个参数来传递并启动。 * 2.Canvas 组件为Component的一个子类,表示屏幕上一个空白矩形区域, * 应用程序可以在该区域内绘图,或者可以从该区域捕获用户的输入事件。 * 应用程序必须为 Canvas 类创建子类,以获得有
32、用的功能(如创建自定义组件)。 * 必须重写 paint 方法,以便在 canvas 上执行自定义图形。 */class Clock extends Canvas implements Runnable CalendarPad cp; Thread t; /Multi-Thread(斜体)线程 是程序中的执行线程。Java 虚拟机允许应用程序并发地运行多个执行线程。 String time; public Clock(CalendarPad cp) this.cp=cp; setSize(280,40); setBackground( Color.pink ); t = new Thread(
33、 this ); /实例化线程 t.start(); /调用线程 Override public void run() while(true) try Thread.sleep(1000); /休眠1秒钟 catch(InterruptedException e) System.out.println("异常"); this.repaint(1000); /重画屏幕 /* Graphics 类是所有图形上下文的抽象基类, * 允许应用程序可以在组件(已经在各种设备上实现), * 以及闭屏图像上,进行绘制。 */ /对paint函数进行重写,在 canvas上执行自定义图形
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; /构造函数 public CalendarPad( int year,int month,int day ) leftPanel=new JPanel(); /左侧是日历 rightPanel=new JPanel(); /右侧显示相应的记事 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(); /左侧放置日历 JPanel leftNorth=new JPanel(); /右侧放置事件 /*设置图标*/ 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(); /创建一个具有凸出斜面边缘的边框,将组件当前背景色的较亮的色度用于突出显示,较暗的色度用于阴影 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); /*左面板北面部分的面板,由与年份和月份有关的组件组成*/ JPanel p = new JPanel(); p.setLayout(new GridLayout(1,2); p.add(ChangeYear); p.add(ChangeMonth); leftNorth.add(p); /*左侧面板整体布置*/ leftPanel.setLayout( new BorderLayout() ); leftPanel.add( leftNorth,"North" ); leftPanel.add
42、( leftCenter,"Center" ); leftPanel.add( new Clock(this),"South"); /!调用Clock类实例,显示当前时间 leftPanel.validate(); /*右侧面板整体布局*/ rightPanel.add( new Label( " 文本框输入年份,按Enter键确定(负数表示公元前)" ),"North") ; note=new Note(this); /当前日历 rightPanel.add(note,"Center"); /
43、把note放在右侧面板中间部分 setCal(year,month); /*备忘录界面总布局*/ Container c = getContentPane();/获得大内容嵌板 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); /在右侧面板上显示点击的时间 note.setText(null); note.getLog(year,month,day); /获取该天的备忘录信息 catch(Exception ee) /设置每月的天数 public void setCal(int year,int month) cal.set(year,month-1,1); /调用Calendar类的set()函数,设置年、月 week=cal.get(Calendar.DAY_OF_WE
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安全医疗责任追究制度培训
- 2025年教育信息化案例
- 磁场课件2025-2026学年北师大版物理九年级全一册
- 安全保障资金管理制度培训
- 财产赠与父母协议书
- 账目结清协议书
- 购买停车券协议书
- 贷款买车签协议书
- 甘肃省庆阳市2024-2025学年七年级下学期期末考试历史试卷(含答案)
- 2025年班组安全评价制度培训
- 2026年深度学习及其应用-复旦大学中国大学mooc课后章节答案期末练习题(典型题)附答案详解
- 2026年辽宁省沈阳市铁西区中考数学一模试卷(含答案)
- 2025年东莞市康复实验学校招聘笔试真题
- 广东省汕头市2026年中考数学模拟试卷五套附答案
- 铁路信号设备操作规程
- 机加工供应商考核制度
- 产科全身麻醉指南与专家共识2025
- 2026年中考英语预测【时文阅读】2026年欢乐春节+吉祥马
- 2025年海淀卫校新生面试题库及答案
- T∕CAFFCI 1-2018 化妆品用原料 甘草酸二钾
- 电玩设备转让合同范本
评论
0/150
提交评论