Une brève introduction à l'environnement d'exploitation:
Langue: Java
Outil: éclipse
Système: Windows 7
(Le dispositif d'impression n'en a pas encore, donc seules des photos d'aperçu peuvent être fournies)
Récemment, le projet doit imprimer des reçus d'achat pour le centre commercial. Dans la vie quotidienne, nous allons au supermarché pour acheter des choses. Lors de la vérification, le caissier imprimera un reçu. Généralement, le centre commercial a également besoin d'une si petite fonction. La démo donnée dans cet article est un exemple d'une imprimante thermique de 58 mm. S'il s'agit d'une imprimante d'autres types de papier, ajustez simplement la largeur du papier.
Test de package; import java.awt. *; Importer java.awt.print. *; / ** * Classe de test d'imprimante (58 mm) * 1. L'imprimante cible doit être définie comme l'imprimante par défaut * 2. La largeur de la page d'impression est liée à l'imprimante spécifique. Il s'agit généralement de la largeur du papier d'impression et doit être configuré comme paramètres système * 3. La largeur d'un caractère chinois est d'environ 12 points * / classe publique PrintTest {public static void main (String [] args) {if (PrinterJob.lookupprintServices (). Longueur> 0) {/ * Imprimer le point de départ de PageFormat = new PageFormat (); Corner, de gauche à droite, de haut en bas, PageFormat.SetOrientation (PageFormat.Portrait); / * Paramètres de format de page d'impression * / Paper Paper = new Paper (); // Définir la largeur d'impression (fixe, lié à l'imprimante spécifique) et la hauteur (liée au contenu imprimé réel) Paper.setSize (140, 450); // Réglez le point de départ de la pointe d'impression, la largeur d'impression et la largeur d'impression. 135, 450); pageformat.setpaper (papier); // créer un livre de document d'impression livre = new book (); book.append (new imprimable () {@Override public int trict (graphics graphics, pageFormat PageFormat, int pageIndex) lance printexception {if (pageIndex> 0) {return no_such_page;} graphics2d graphics2d = (graphics2d) Fraphics;} graphics2d graphics2d = (graphics2d) Fraphics;} graphics2d graphics2d = (graphics2d) Fraphics;} graphics2d graphics2d = (graphics2d) Fraphics;} graphics2d graphics2d = (graphics2d) Fraphics;} Graphics2d FONT = NOUVEAU FONT ("安体", FONT.PLAIN, 5); Graphics2d.setfont (Font); DrawString (Graphics2d, »/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////DEBLE ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////DEBLE ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////DEBLE ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////DEBLE 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 gris = new Color (145, 145); // reçoit des informations yindex = drawstring (graphics2D, "Consignee: Passherby A", 10, Yindex, LineWidth, Lineheight); Yindex = Drawstring (Graphics2d, "Adresse reçue: Build Build BasicStroke.JOIN_BEVEL,0,new float[]{4, 4},0);graphics2D.setStroke(stroke);graphics2D.drawRect(5, 10, 129, yIndex);//Drugstore name lineWidth = 129;lineHeight = 8;graphics2D.setFont(new Font("宋体", Font.BOLD, 8)); graphics2d.setColor (defaultColor); yindex = drawstring (Graphics2d, "Beijing Pharmacy Retail Ticket", 5, Yindex + LineHeight + 20, LineWidth, 12); Graphics2d.Setfont (New Font ("宋体", Font.Plain (6)); graphics2d.setColor (Gray); yindex = drawstring2d, graphics2d.setColor (Gray); yindex = drawstring2d, graphics2d.setColor (Gray); yindex = drawstring2d, graphics2d.setColor (Gray); YinDex = DrawString2d, graphics2d.setcolor (gris); yindex = DrawString2D. "Opérateur: xiaoqing", 5, yindex + lineheight + 2, linewidth, lineheight); yindex = drawstring (graphics2d, "date: 2017-01-05", 5 + linewidth / 2, yindex, linewidth, lineheight); yindex = drawstring (graphics2d, "Product Nom", 5, yindex + ling LineWidth, LineHeight); yindex = drawstring (graphics2d, "Spécification", (linewidth / 10) * 4, yindex, linewidth, lineheight); yindex = drawstring (graphics2d, "prix unitaire" (LineWidth / 10) * 10, yindex, linewidth, lineheight); for (int i = 0; i <5; i ++) {graphics2d.setfont (nouvelle police ("宋体", font.plain, 7)); yindex = drawstring (graphics2d, "e vitamine b complets 100 e vitamin b complexe (LineWidth / 10) * 7, 10); Graphics2d.setfont (New Font ("安安", Font.Plain, 6)); Graphics2d.setColor (Gray); Yindex = Drawstring (Graphics2d (LineWidth / 10) * 8, yindex, linewidth, lineheight); yindex = drawstring (graphics2d, "2", (linewidth / 10) * 10, yindex, linewidth, lineheight); graphics2d.setfont (nouvelle police ("宋体" yindex, 5 + linewidth, yindex);} graphics2d.setColor (defaultColor); yindex = drawstring (graphics2d, "Nom du membre: xiaoqing", 5, yindex + lineheight * 2, linewidth, lineheight); yindex = drawstring (Graphics2d, "total: 6", 5, yindex = lineight, linerth, "total: 6", 5, yindex + linewheeg LineHeight); yindex = drawstring (graphics2d, "total: 55.30", 5, yindex + lineheight, linewidth, lineheight); yindex = drawstring (Graphics2d, "Cash: 100.00", 5, yindex + lineheight, "Change: 44.70". LineHeight, LineWidth, LineHeight); Graphics2d.setfont (New Font ("宋体", Font.Plain, 6)); Graphics2D.SetColor (Gray); Yindex = Drawstring (Graphics2d "Adresse: Baidu Building, No. 10 Shangdi Street, Haidian District, Beijing", 5, Yindex + Lineheight, Linewidth, Lineheight); Yindex = Yindex + 20; Graphics2D.Drawline (0, Yindex, 140, Yindex); Retour Page_Exists;}}, PageForat); // Get the Par défaut Imprimante ProrterJob PRIMERJOB = PageForat); // Get the Par défaut PRIMER PRINTERJOB PRIMERJOB) Priterjob.getPrinerJob (); printPagable (book); try {priterjob.print ();} catch (priterException e) {e.printStackTrace (); System.out.println ("Exception d'impression");}} else {System.out.print ("Print Service ne peut pas être découvert");}} @Param Text Imprimer Text * @Param x Imprimer Point de départ X coordonnée * @param y Imprimer Point de départ Point Y Coordonnée * @Param LineWidth Line Width * @param LineHeight Line Height * @return Return to End Point Y Coordate * / Private Static int Drawstring (int LineHiC graphics2d.getfontmetrics (); if (Fontmetrics.StringWidth (text) <linewidth) {graphics2d.Drawstring (text, x, y); return y;} else {char [] chars = text.tocharArray (); int charwidth = 0; stringbuffer sb = new stringbuffer (); pour (int i = 0;; i ++) {if ((charwidth + fontmetrics.charwidth (chars [i]))> linewidth) {graphics2d.Drawstring (sb.tostring (), x, y); sb.setLength (0); y = y + lineheight; charwidth = Fontmetrics.Charwidth (Chars [i]); sb.append (Chars [i]);} else {Charswidth = charwidth + Fontmetrics.Charwidth (Chars [i]); sb.append (Chars [i]);}} if (sb.Length ()> 0) {Graphics2d. y); y = y + lineheight;} return y - LineHeight;}}}Résultats en cours:
Aperçu de l'effet:
Résumer
Autrement dit, il s'agit d'écrire un programme Java, d'enregistrer le résultat de sortie en tant que fichier de format "* .xps" et de le sortir par l'imprimante, ce qui est très simple. J'espère que cela sera utile à tout le monde. Si vous avez des questions, veuillez laisser un message et le signaler. Merci vos amis pour votre soutien à ce site.