ava备忘录编写源代码及运行界面截图.docx
《ava备忘录编写源代码及运行界面截图.docx》由会员分享,可在线阅读,更多相关《ava备忘录编写源代码及运行界面截图.docx(39页珍藏版)》请在冰豆网上搜索。
ava备忘录编写源代码及运行界面截图
//Memorial.java
importjava.awt.*;
importjava.io.*;
importjava.util.*;
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(),必须使布局为null
picture.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;i<4;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;
}
}
//按钮事件处理
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
Objectsource=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-generatedcatchblock
e1.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{
@Override
publicvoidmouseClicked(MouseEvente){
//TODOAuto-generatedmethodstub
jl3.setText(getcode());
}
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newMemorial().launchFrame();
}
}
//year.java
importjavax.swing.*;
importjava.awt.*;
publicclassYearextendsJPanel//ActionListener接口
{
privateintyear;
privateJTextFieldshowYear=null;
privateJButtonnextYear,lastYear;
privateCalendarPadcal;
publicYear(CalendarPadc)
{
showYear=newJTextField(4);
this.cal=c;
year=cal.getYear();
nextYear=newJButton("下年");
lastYear=newJButton("上年");
launch();
}
//布局函数
publicvoidlaunch(){
showYear.setForeground(Color.blue);
showYear.setFont(newFont("宋体",Font.BOLD,16));
setLayout(newGridLayout(1,3,5,5));
add(lastYear);
add(showYear);
add(nextYear);
showYear.addActionListener(newaddEvent());
lastYear.addActionListener(newaddEvent());
nextYear.addActionListener(newaddEvent());
lastYear.setBorder(BorderFactory.createRaisedBevelBorder());//使按钮凸出
nextYear.setBorder(BorderFactory.createRaisedBevelBorder());
}
//年
publicvoidsetYear(intyear)
{
this.year=year;
showYear.setText(""+year);
}
publicintgetYear()
{
returnyear;
}
//事件类
classaddEventimplementsActionListener{
@Override
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==lastYear)//对年份进行判断操作
{
year=year-1;
showYear.setText(""+year);
cal.setYear(year);
cal.setCal(year,cal.getMonth());
}
elseif(e.getSource()==nextYear)
{
year=year+1;
showYear.setText(""+year);
cal.setYear(year);
cal.setCal(year,cal.getMonth());
}
elseif(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(null,"输入的年份有误,请重新输入!
","错误提示",JOptionPane.ERROR_MESSAGE);
showYear.setText("");
}
}
catch(NumberFormatExceptionee)
{
JOptionPane.showMessageDialog(null,"请输入正确的年份格式!
","错误提示",JOptionPane.ERROR_MESSAGE);
showYear.setText("");
}
}
}
}
}
//Month.java
//对月份的选择
importjavax.swing.*;
importjava.awt.*;
publicclassMonthextendsJPanel//ActionListener接口
{
privateintmonth;
privateJTextFieldshowMonth=null;
privateJButtonlastMonth,nextMonth;
privateCalendarPadcal;
publicMonth(CalendarPadc)
{
this.cal=c;
showMonth=newJTextField();
month=c.getMonth();
nextMonth=newJButton("下月");
lastMonth=newJButton("上月");
launch();
}
//页面布局
publicvoidlaunch(){
showMonth.setEditable(false);//设置月份不可被编辑
showMonth.setForeground(Color.blue);
showMonth.setFont(newFont("宋体",Font.BOLD,16));
setLayout(newGridLayout(1,3,5,5));
add(lastMonth);
add(showMonth);
add(nextMonth);
lastMonth.addActionListener(newaddEvent());
nextMonth.addActionListener(newaddEvent());
lastMonth.setBorder(BorderFactory.createRaisedBevelBorder());
nextMonth.setBorder(BorderFactory.createRaisedBevelBorder());
showMonth.setText(""+month);
}
publicvoidsetMonth(intmonth)
{
if(month<=12&&month>=1)
{
this.month=month;
}
else
{
this.month=1;
}
showMonth.setText(""+month);
}
publicintgetMonth()
{
returnmonth;
}
//事件处理
classaddEventimplementsActionListener{
@Override
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==lastMonth)
{
if(month>=2)
{
month=month-1;
cal.setMonth(month);
cal.setCal(cal.getYear(),month);
}
elseif(month==1)
{
month=12;
cal.setMonth(month);
cal.setCal(cal.getYear(),month);
}
showMonth.setText(""+month);
}
elseif(e.getSource()==nextMonth)
{
if(month<12)
{
month=month+1;
cal.setMonth(month);
cal.setCal(cal.getYear(),month);
}
elseif(month==12)
{
month=1;
cal.setMonth(month);
cal.setCal(cal.getYear(),month);
}
showMonth.setText(""+month);
}
}
}
}
//Clock.java
//提取当前的年月日时分秒,时钟
importjava.awt.*;
/*1.创建线程一种方法是将类声明为Thread的子类。
该子类应重写Thread类的run方法。
*接下来可以分配并启动该子类的实例。
*另一种方法是声明实现