java 调收银机打印小票

最近要做一个swing的小程序监听收银机的数据并打印在这记录一下!

public class MyPrint implements Printable{
    
    static Logger log = Logger.getLogger(MyPrint.class); 
    
    //业务类
    UserService userService = new UserServiceImpl();
    PayInfoService payInfoService = new PayInfoServiceImpl();
 
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        LoginUser loginUser = userService.checkLoginUser();
        
        Graphics2D graphics2d = (Graphics2D) graphics;
        //设置字体
        graphics2d.setFont(new Font("宋体", Font.PLAIN,8));
        graphics2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        //打印格式
        graphics2d.drawString("登入时间:"+loginUser.getLoginDate(), 15, 10); 
        graphics2d.drawString("登出时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), 15, 25); 
        graphics2d.drawString("收银员:"+loginUser.getUserName(), 15, 45);
        graphics2d.drawString("收银员编号:"+loginUser.getLoginName(), 15, 60);
        graphics2d.drawString("------------------------------------------", 15, 75); 
        graphics2d.drawString("总单数:"+payInfoService.countPay(0)+"", 15, 90); 
        graphics2d.drawString("当班总收入(元):"+payInfoService.sumMoeny(0)+"", 15, 105);
        graphics2d.drawString("聚合支付单数:"+payInfoService.countPay(3)+"" , 15, 125); 
        graphics2d.drawString("聚合支付收款(元):"+payInfoService.sumMoeny(3)+"", 15, 140);
        graphics2d.drawString("微信单数:"+payInfoService.countPay(1)+"" , 15, 160); 
        graphics2d.drawString("微信收款(元):"+payInfoService.sumMoeny(1)+"", 15, 175);
        graphics2d.drawString("支付宝单数:"+payInfoService.countPay(2)+"", 15, 195); 
        graphics2d.drawString("支付宝收款(元):"+payInfoService.sumMoeny(2)+"", 15, 210);
        graphics2d.drawString("POS机单数:"+payInfoService.countPay(5)+"", 15, 230); 
        graphics2d.drawString("POS机收款(元):"+payInfoService.sumMoeny(5)+"" , 15, 245);
        graphics2d.drawString("加油卡单数:"+payInfoService.countPay(4)+"" , 15, 265); 
        graphics2d.drawString("加油卡收款(元):"+payInfoService.sumMoeny(4)+"", 15, 280); 
        graphics2d.drawString("------------------------------------------", 15, 295); 
        return PAGE_EXISTS;    
    }
    
    public static void printWordDayInfo() {
        try {
            
            Book book = new Book(); 
            PageFormat pf = new PageFormat();    
            
            pf.setOrientation(PageFormat.PORTRAIT);     
            Paper p = new Paper(); 
            //设置打印纸的大小一般是158,10000随便设的因为这是一卷纸不像A4纸是规定的
            p.setSize(158,10000); 
            //打印区域
            p.setImageableArea(0,0, 158,10000); 
            pf.setPaper(p);   
            book.append(new MyPrint(), pf);  
           
            PrinterJob job = PrinterJob.getPrinterJob(); 
            job.setPageable(book); 
            job.print(); 
        } catch (Exception e) {
            e.printStackTrace();
            log.error("打印异常:"+e.getMessage());
        }
    }
 

}

打印效果:

posted @ 2018-09-14 16:10  KitFung-P  阅读(4567)  评论(1编辑  收藏  举报