国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品久久久久久亚洲调教 | 波多野结衣中文字幕一区二区三区 | 国产精品片aa在线观看 | 毛片网站大全 | 欧美精品一区在线 | 美日韩成人 | 久草福利资源 | 日韩高清中文字幕 | 99热在线播放 | 欧州一区二区三区 | 91大全| 少妇精品久久久久久久久久 | 久久久久国产精品午夜一区 | 欧美视频在线免费 | 国产精品18久久久久久首页狼 | 人妖天堂狠狠ts人妖天堂狠狠 | 亚洲天堂中文字幕 | a在线看| 久久丁香| 一本大道香蕉大a√在线 | 亚洲一区二区国产 | 黄色一区二区三区 | 激情综合五月 | 欧美日韩视频在线 | 一级色网站 | 欧美一级在线观看 | 亚洲精品一区 | 中文字幕在线三区 | 99精品一区二区三区 | 在线视频国产一区 | 国产精品国产精品国产专区不片 | 中文字幕在线播放一区 | 综合久久综合 | 欧美激情视频一区二区三区 | 久久一区二区三 | 日韩国产中文字幕 | 欧美日韩一区二区中文字幕 | 中文字幕免费看 | 日韩在线视频观看免费 | 久久久久久久国产精品 | 一区二区三区免费在线观看 |