مقدمة موجزة لبيئة التشغيل:
اللغة: جافا
الأداة: Eclipse
النظام: Windows 7
(لا يحتوي جهاز الطباعة على جهاز حتى الآن ، لذلك يمكن توفير صور معاينة فقط)
في الآونة الأخيرة ، يحتاج المشروع إلى طباعة إيصالات التسوق للمركز التجاري. في الحياة اليومية ، نذهب إلى السوبر ماركت لشراء الأشياء. عند التحقق ، سيقوم أمين الصندوق بطباعة إيصال. بشكل عام ، يحتاج المركز التجاري أيضًا إلى مثل هذه الوظيفة الصغيرة. العرض التوضيحي الوارد في هذه المقالة هو مثال لطابعة حرارية 58 مم. إذا كانت طابعة من أنواع الورق الأخرى ، فما عليك سوى ضبط عرض الورق.
اختبار الحزمة ؛ استيراد java.awt.* ؛ استيراد java.awt.print.* ؛/*** فئة اختبار الطابعة (58 مم)* 1. إنه عمومًا عرض ورقة الطباعة ويجب تكوينه كمعلمات النظام* 3. عرض الحرف الصيني حوالي 12 نقطة*/طباعة الطبقة العامة {public static void main (string [] args) {if (printerjob.lookupprintservices (). الزاوية ، من اليسار إلى اليمين ، من أعلى إلى أسفل ، pageformat.setorientation (pageformat.portrait) ؛/* إعدادات تنسيق صفحة الطباعة*/ورقة الورق = ورقة جديدة () 135 ، 450) ؛ pageformat.setpaper (paper) ؛ // إنشاء كتاب طباعة دفتر المستندات = New Book () ؛ book.append (new printable () {Override public int print (رسومات الرسومات ، pageformat pageformat ، int pageIndEx2D prinkception {pageIndex> 0) font = new font ("安体" ، font.plain ، 5) ؛ Graphics2d.setfont (font) ؛ drainstring (Graphics2D ، "///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////. 119 ، 8) ؛ font = new font ("宋体" ، font.plain ، 7) ؛ Graphics2d.setfont (font) ؛ int yindex = 30 ؛ int lineHeight = 10 ؛ int linewidth = 120 ؛ color defaultColor = drawerS2D.getColor () "المرسل إليه: المارة A" ، 10 ، yindex ، linewidth ، lineheight) ؛ yindex = derkstring (graphics2d ، "العنوان المستلم: Baidu build basicstroke.join_bevel ، 0 ، تعويم جديد [] {4 ، 4} ، 0) ؛ Graphics2d.SetStroke (stroke) ؛ graphics2d.drawrect (5 ، 10 ، 129 ، yindex) ؛ 8) ؛) الرباط (Graphics2d ، "المشغل: XiaoQing" ، 5 ، Yindex + LineHeight + 2 ، LineWidth ، lineheight) ؛ yindex = dringstring (Graphics2d ، "Date: 2017-01-05" ، 5 + linewidth/2 ، yindex ، lineWidth) ؛ yindex = *2 - 5 ، linewidth ، lineheight) ؛ yindex = dringstring (graphics2d ، "specification" ، (linewidth/10)*4 ، yindex ، linewidth ، lineHeight) ؛ "الكمية" ، (linewidth/10)*10 ، yindex ، linewidth ، lineheight) ؛ لـ (int i = 0 ؛ i <5 ؛ i ++) {graphics2d.setfont (خط جديد ("宋体" ، font.plain ، 7)) 15 ، (linewidth/10)*7 ، 10) ؛ Graphics2d.setfont (خط جديد ("安安" ، font.plain ، 6)) ؛ Graphics2d.setColor (Gray) ؛ (linewidth/10)*8 ، yindex ، linewidth ، lineheight) ؛ yindex = draphstring (graphics2d ، "2" ، (linewidth/10)*10 ، yindex ، linewidth ، lineheight) ؛ graphics2d.setfont (new font ("宋体" ، font.plain ، 7)) ؛ yindex ، 5 + linewidth ، yindex) ؛} graphics2d.setColor (defaultColor) ؛ yindex = draphstring (graphics2d ، "اسم العضو: xiaoqing" ، 5 ، yindex + lineheight * 2 ، linewidth ، lineheight) lineheight) ؛ yindex = dringstring (Graphics2d ، "Total: 55.30" ، 5 ، yindex + lineheight ، linewidth ، lineheight) ؛ Lineheight ، linewidth ، lineHeight) ؛ Graphics2d.setfont (خط جديد ("宋体" ، font.plain ، 6)) ؛ Graphics2d.setColor (Gray) ؛ yindex = dredstring (graphics2d ، "tel: 020-123456" ، 5 ، yindex + lineHight * 2 ، lineHight ، lineheight) "العنوان: مبنى بايدو ، رقم 10 شارع شانغدي ، منطقة هايديان ، بكين" ، 5 ، Yindex + lineheight ، linewidth ، lineheight) ؛ yindex = yindex + 20 ؛ graphics2d.drawline (0 ، yindex ، 140 ، yindex) ؛ printerjob.getPrinterJob () ؛ printpagable (book) ؛ حاول {printerjob.print () ؛} catch (printerxception e) { param text text text* param x print point x stipendinate* @param y print pint point y companinate* @param linewidth width* @param lineheight line height* @return to end point y companinate*/private static int drawString (graphics2d ، string string ، int y ، int linewidth) graphics2d.getfontmetrics () ؛ if (fontmetrics.stringwidth (text) <lineWidth) {graphics2d.drawString (text ، x ، y) ؛ return y ؛} else {char [] chars = text.tochararray () ؛ int charswidth = 0 ؛ i ++) {if ((charswidth + fontmetrics.charwidth (chars [i]))> linewidth) {graphics2d.drawString (sb.toString () ، x ، y) ؛ sb.setlength (0) ؛ y = y + linehight ؛ fontmetrics.charwidth (chars [i]) ؛ sb.append (chars [i]) ؛} آخر {charswidth = charswidth + fontmetrics.charwidth (chars [i]) ؛ sb.append (chars [i]) ؛ y) ؛ y = y + lineHeight ؛} إرجاع y - lineheight ؛}}}نتائج التشغيل:
معاينة التأثير:
لخص
ببساطة ، هو كتابة برنامج Java ، حفظ نتيجة الإخراج كملف تنسيق "*.xps" ، وإخراجه بواسطة الطابعة ، وهو أمر بسيط للغاية. آمل أن يكون ذلك مفيدًا للجميع. إذا كان لديك أي أسئلة ، فيرجى ترك رسالة وإشرافها. شكرا لك يا أصدقائك لدعمكم لهذا الموقع.