简单介绍运行环境:
语言:Java
工具:eclipse
系统:Windows7
(打印设备暂时没有,所以只能提供预览图)
最近,项目需要为商城做一个购物小票的打印功能,日常我们去超市买东西,结账的时候收银员都会打印一个小票,一般的商城也都需要这样的一个小功能,本文给出的 demo 是在 58mm 的热敏打印机下的例子,如果是其他纸张类型的打印机,调整纸张宽度即可。
package test;import java.awt.*;import java.awt.print.*;public class PrintTest {public static void main(String[] args){if(PrinterJob.lookupPrintServices().length>0){PageFormat pageFormat = new PageFormat();//设置打印起点从左上角开始,从左到右,从上到下打印pageFormat.setOrientation(PageFormat.PORTRAIT);Paper paper = new Paper();//设置打印宽度(固定,和具体的打印机有关)和高度(跟实际打印内容的多少有关)paper.setSize(140, 450);//设置打印区域 打印起点坐标、打印的宽度和高度paper.setImageableArea(0, 0, 135, 450);pageFormat.setPaper(paper);//创建打印文档Book book = new Book();book.append(new Printable() {@Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {if(pageIndex>0){return NO_SUCH_PAGE;}Graphics2D graphics2D = (Graphics2D) graphics;Font font = new Font("宋体", Font.PLAIN, 5);graphics2D.setFont(font);drawString(graphics2D, "//////////////////////////////", 10, 17, 119, 8);font = new Font("宋体", Font.PLAIN, 7);graphics2D.setFont(font);int yIndex = 30;int lineHeight = 10;int lineWidth = 120;Color defaultColor = graphics2D.getColor();Color grey = new Color(145, 145, 145);//收货信息yIndex = drawString(graphics2D, "收货人:路人甲", 10, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "收货地址:北京市海淀区上地十街10号百度大厦", 10, yIndex + lineHeight, lineWidth, lineHeight);//收货信息边框Stroke stroke = new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,0,new float[]{4, 4},0);graphics2D.setStroke(stroke);graphics2D.drawRect(5, 10, 129, yIndex);//药店名称lineWidth = 129;lineHeight = 8;graphics2D.setFont(new Font("宋体", Font.BOLD, 8));graphics2D.setColor(defaultColor);yIndex = drawString(graphics2D, "北京药店零售小票", 5, yIndex + lineHeight + 20, lineWidth, 12);graphics2D.setFont(new Font("宋体", Font.PLAIN, 6));graphics2D.setColor(grey);yIndex = drawString(graphics2D, "操作员:小清新", 5, yIndex + lineHeight + 2, lineWidth, lineHeight);yIndex = drawString(graphics2D, "日期:2017-01-05", 5 + lineWidth/2, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "品名", 5, yIndex + lineHeight * 2 - 5, lineWidth, lineHeight);yIndex = drawString(graphics2D, "规格", (lineWidth/10)*4, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "单价", (lineWidth/10)*8, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "数量", (lineWidth/10)*10, yIndex, lineWidth, lineHeight);for (int i=0; i<5; i++){graphics2D.setFont(new Font("宋体", Font.PLAIN, 7));yIndex = drawString(graphics2D, "E复合维生素B片100片E复合维生素B片100片", 5, yIndex + 15, (lineWidth/10)*7, 10);graphics2D.setFont(new Font("宋体", Font.PLAIN, 6));graphics2D.setColor(grey);yIndex = drawString(graphics2D, "100片/盒", 5, yIndex + 11, lineWidth, lineHeight);yIndex = drawString(graphics2D, "14.50", (lineWidth/10)*8, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "2", (lineWidth/10)*10, yIndex, lineWidth, lineHeight);graphics2D.setFont(new Font("宋体", Font.PLAIN, 7));yIndex = yIndex + 2;graphics2D.drawLine(5, yIndex, 5 + lineWidth, yIndex);}graphics2D.setColor(defaultColor);yIndex = drawString(graphics2D, "会员名称:小清新", 5, yIndex + lineHeight * 2, lineWidth, lineHeight);yIndex = drawString(graphics2D, "总 数:6", 5, yIndex + lineHeight, lineWidth, lineHeight);yIndex = drawString(graphics2D, "总 计:55.30", 5, yIndex + lineHeight, lineWidth, lineHeight);yIndex = drawString(graphics2D, "收 款:100.00", 5, yIndex + lineHeight, lineWidth, lineHeight);yIndex = drawString(graphics2D, "找 零:44.70", 5, yIndex + lineHeight, lineWidth, lineHeight);graphics2D.setFont(new Font("宋体", Font.PLAIN, 6));graphics2D.setColor(grey);yIndex = drawString(graphics2D, "电话:020-123456", 5, yIndex + lineHeight * 2, lineWidth, lineHeight);yIndex = drawString(graphics2D, "地址:北京市海淀区上地十街10号百度大厦", 5, yIndex + lineHeight, lineWidth, lineHeight);yIndex = yIndex + 20;graphics2D.drawLine(0, yIndex, 140, yIndex);return PAGE_EXISTS;}}, pageFormat);//获取默认打印机PrinterJob printerJob = PrinterJob.getPrinterJob();printerJob.setPageable(book);try {printerJob.print();}catch (PrinterException e) {e.printStackTrace();System.out.println("打印异常");}} else{System.out.println("没法发现打印机服务");}}private static int drawString(Graphics2D graphics2D, String text, int x, int y, int lineWidth, int lineHeight){FontMetrics fontMetrics = graphics2D.getFontMetrics();if(fontMetrics.stringWidth(text)<lineWidth){graphics2D.drawString(text, x, y);return y;} else{char[] chars = text.toCharArray();int charsWidth = 0;StringBuffer sb = new StringBuffer();for (int i=0; i<chars.length; i++){if((charsWidth + fontMetrics.charWidth(chars[i]))>lineWidth){graphics2D.drawString(sb.toString(), x, y);sb.setLength(0);y = y + lineHeight;charsWidth = fontMetrics.charWidth(chars[i]);sb.append(chars[i]);} else{charsWidth = charsWidth + fontMetrics.charWidth(chars[i]);sb.append(chars[i]);}}if(sb.length()>0){graphics2D.drawString(sb.toString(), x, y);y = y + lineHeight;}return y - lineHeight;}}}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容猜你喜欢
AI推送时光机Java编程打印购物小票实现代码
后端开发2023-05-30
Android打印机--小票打印格式及模板设置实例代码
后端开发2022-06-06
Java编程实现从尾到头打印链表代码实例
后端开发2023-05-31
java实现小游戏编程源代码
后端开发2023-09-22
微信小程序如何实现蓝牙连接小票打印机
后端开发2024-04-02
微信小程序实现购物商城(附源码)
后端开发2023-09-04
java web开发之购物车功能实现示例代码
后端开发2023-05-30
Java编程实现打地鼠文字游戏实例代码
后端开发2023-05-30
Java编程如何实现打印螺旋矩阵
后端开发2023-05-30
Java编程之如何实现双重循环打印图形
后端开发2023-05-30
Java实现猜数字小游戏代码怎么编写
后端开发2023-06-26
SSM+微信小程序实现物业管理系统及实例代码
后端开发2024-04-02
java编程几行代码实现买菜自由
后端开发2024-04-02
Java编程实现月食简单代码分享
后端开发2023-05-30
Java实现坦克大战小游戏代码如何编写
后端开发2023-06-26
Java多线程编程实现socket通信示例代码
后端开发2023-05-30
Java编程redisson实现分布式锁代码示例
后端开发2023-05-31
Java基于IDEA实现http编程的示例代码
后端开发2024-04-02
Java编程实现NBA赛事接口调用实例代码
后端开发2023-05-30
咦!没有更多了?去看看其它编程学习网 内容吧