一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Java編程打印購物小票實現代碼

Java編程打印購物小票實現代碼

2021-02-20 11:25RexFang JAVA教程

這篇文章主要介紹了Java編程打印購物小票實現代碼,具有一定參考價值,需要的朋友可以了解下。

簡單介紹運行環境:

語言:java

工具:eclipse

系統:windows7

打印設備暫時沒有,所以只能提供預覽圖)

最近,項目需要為商城做一個購物小票的打印功能,日常我們去超市買東西,結賬的時候收銀員都會打印一個小票,一般的商城也都需要這樣的一個小功能,本文給出的 demo 是在 58mm 的熱敏打印機下的例子,如果是其他紙張類型的打印機,調整紙張寬度即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package test;
import java.awt.*;
import java.awt.print.*;
/**
 * 打印機測試類(58mm)
 * 1、目標打印機必須設置為默認打印機
 * 2、打印頁面的寬度和具體的打印機有關,一般為打印紙的寬度,需要配置成系統參數
 * 3、一個漢字的寬度大概是12點
 */
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("沒法發現打印機服務");
        }
    }
    /**
   * 字符串輸出
   * @param graphics2d  畫筆
   * @param text     打印文本
   * @param x       打印起點 x 坐標
   * @param y       打印起點 y 坐標
   * @param linewidth   行寬
   * @param lineheight  行高
   * @return 返回終點 y 坐標
   */
    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;
        }
    }
}

運行結果:

Java編程打印購物小票實現代碼

效果預覽:

Java編程打印購物小票實現代碼

總結

簡單說就是編寫一段java程序,將輸出結果另存為“ *.xps  ”格式文件,由打印機輸出,非常簡單。希望對大家有所幫助。如有問題歡迎留言指出。感謝朋友們對本站的支持。

原文鏈接:http://www.cnblogs.com/rexfang/p/7441146.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品第二页 | 和岳m的小说 | 91精品国产99久久 | 2020国语对白露脸 | 波多野结衣在线观看视频 | 欧美久久久久久久一区二区三区 | 男人猛戳女人下部30分钟 | 亚洲黄网站wwwwww | 男人女人插 | 三级黄色片在线免费观看 | 韩国三级日本三级香港三级黄 | 男人狂擦女人的下面视频 | 国产爱啪啪| 国产精品视频二区不卡 | 羞羞视频麻豆 | 天堂成人影院 | 高h细节肉爽文办公室 | 女教师三级做受 | 国模丰满美女冰漪34d | 亚洲精品一区制服丝袜 | 国产免费久久精品44 | 236zz宅宅最新伦理 | 国产v日韩v欧美v精品专区 | 欧美一级鲁丝片免费看 | 手机在线观看伦理片 | h肉动漫在线视频无修无遮挡 | 被高跟鞋调教丨vk | 无码中文字幕热热久久 | 成人啪精品视频免费网站 | 精品播放 | 日韩精品久久不卡中文字幕 | 亚洲AV无码乱码国产麻豆穿越 | 99re这里只有精品视频在线观看 | 国产午夜亚洲精品理论片不卡 | 成年男女免费视频网站 | 欧美成人香蕉在线观看 | 亚洲欧美日韩国产精品影院 | 四虎私人影院 | 国产精品国产高清国产专区 | 无限在线看免费视频大全 | 男人午夜免费视频 |