zoukankan      html  css  js  c++  java
  • 2016.9.22小程序

    制作日志
    实现步骤:
    a、制作一个日志类(LogObj),该类提供一个记录日志的方法
    public void writeLog(String info){}
    该方法要求记录用户登陆信息。
    b、创建目录log,用于存放日志文件。创建文件流以追加方式写入文件,以当前日期的年月日做文件名。文件名格式为:2011-12-05.txt
    c、创建一个登陆类,书写GUI界面。利用事件要求用户输入用户名和密码,如果登陆成功则写入日志。“张三在 15:32:30 登陆系统。”。如果登陆失败,则写入“非法用户张三在 15:32:30 企图登陆系统”

    1、日志类(LogObj)

     1 import java.io.File;
     2 import java.io.FileWriter;
     3 import java.io.IOException;
     4 import java.io.Writer;
     5 import java.text.SimpleDateFormat;
     6 import java.util.Date;
     7 
     8 public class LogObj {
     9     private static SimpleDateFormat dateNow = new SimpleDateFormat("yyyy-MM-dd");
    10     public static void writeLog(String info){
    11         Writer w = null ;
    12         String dateTxt = dateNow.format(new Date ());
    13     
    14          try {
    15               File f = new File("log");
    16               if (f.exists() == false){//判断目录是否存在,如果不存在则新建目录
    17                   f.mkdirs();
    18               }
    19             w = new FileWriter("log/"+dateTxt+".txt",true);
    20             w.write(info+"
    ");
    21         } catch (Exception e) {
    22             e.printStackTrace();
    23         }finally {
    24             try {
    25                 w.close();
    26             } catch (IOException e) {
    27                 e.printStackTrace();
    28             }
    29         }
    30     }
    31 }

    2、登录窗体

     1 import java.awt.event.ActionEvent;
     2 import java.awt.event.ActionListener;
     3 import java.text.SimpleDateFormat;
     4 import java.util.Date;
     5 
     6 import javax.swing.JFrame;
     7 import javax.swing.JOptionPane;
     8 
     9 import com.lovo.netCRM.component.LovoButton;
    10 import com.lovo.netCRM.component.LovoTxt;
    11 
    12 public class LoginFrame extends JFrame{
    13     private LovoTxt userName = new LovoTxt("用户名", 100, 100, this);
    14     private LovoTxt userPwd = new LovoTxt("密码", 100, 150, this);
    15     private SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
    16     public LoginFrame (){
    17         this.setLayout(null);
    18         LovoButton loginButton = new LovoButton("登录", 200, 220, this);
    19         loginButton.addActionListener(new ActionListener() {
    20             @Override
    21             public void actionPerformed(ActionEvent arg0) {
    22                 String timeTxt = time.format(new Date ());
    23                 if ("张三".equals(userName.getText()) && "111".equals(userPwd.getText())){
    24                     JOptionPane.showMessageDialog(null, "登录成功");
    25                     LogObj.writeLog(userName.getText()+"在   "+timeTxt+"登录系统");
    26                 }else{
    27                     JOptionPane.showMessageDialog(null, "登录失败");
    28                     LogObj.writeLog(userName.getText()+"在   "+timeTxt+"企图登录系统");
    29                 }
    30             }
    31         });
    32         
    33         this.setSize(500,400);
    34         this.setVisible(true);
    35         this.setDefaultCloseOperation(3);
    36         this.setLocationRelativeTo(null);
    37     }
    38     
    39     public static void main(String[] args) {
    40         new LoginFrame();
    41     }
    42 
    43 }
  • 相关阅读:
    pytest-html的更多功能
    pytest使用allure测试报告
    airtest+pytest实战教程02-APP页面元素获取
    airtest+pytest实战教程01-环境配置
    python发送邮件
    python获取文件名
    python生成GIF
    Selenium+Unittest自动化测试框架实战
    xpath定位和css定位
    Lucas(卢卡斯)定理模板&&例题解析([SHOI2015]超能粒子炮·改)
  • 原文地址:https://www.cnblogs.com/zzyytt/p/5921580.html
Copyright © 2011-2022 走看看