1. 程式人生 > >JAVA連線印表機列印小票

JAVA連線印表機列印小票

1.商品資訊類

package com.smartsuppermarket.commodity;

public class Commodity {

	// 商品名稱
	private String name;
	// 單價
	private String unit_price;
	// 數量
	private String num;

	// 總價
	private String sum;

	public Commodity(String name, String unit_price, String num, String sum, String barcode) {
		super();
		this.name = name;
		this.unit_price = unit_price;
		this.num = num;
		this.sum = sum;
		this.barcode = barcode;
	}

	// 條碼
	private String barcode;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUnit_price() {
		return unit_price;
	}

	public void setUnit_price(String unit_price) {
		this.unit_price = unit_price;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getSum() {
		return sum;
	}

	public void setSum(String sum) {
		this.sum = sum;
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

}
2.列印佈局類
package com.smartsuppermarket.printer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.Calendar;

import com.smartsuppermarket.commodity.Commodity;

//實現Printable介面 用於建立列印內容
public class SalesTicket implements Printable {
	private ArrayList<Commodity> list;
	private String cashier;
	private Font font;
	private String sale_num;
	private String sale_sum;
	private String practical;
	private String changes;
	private String orders;

	// 建構函式
	public SalesTicket(ArrayList<Commodity> list, String cashier, String orders, String sale_num, String sale_sum,
			String practical, String changes) {
		this.list = list;
		// 收銀員編號
		this.cashier = cashier;
		// 訂單標號
		this.orders = orders;
		// 商品總數
		this.sale_num = sale_num;
		// 總金額
		this.sale_sum = sale_sum;
		// 實收
		this.practical = practical;
		// 找零
		this.changes = changes;
	}

	/**
	 * @param Graphic指明列印的圖形環境
	 * @param PageFormat指明列印頁格式(頁面大小以點為計量單位,1點為1英才的1/72,1英寸為25.4毫米。A4紙大致為595×
	 *            842點)
	 * @param pageIndex指明頁號
	 **/
	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
		Component c = null;
		// 轉換成Graphics2D 拿到畫筆
		Graphics2D g2 = (Graphics2D) graphics;
		// 設定列印顏色為黑色
		g2.setColor(Color.black);

		// 列印起點座標
		double x = pageFormat.getImageableX();
		double y = pageFormat.getImageableY();

		// 虛線
		float[] dash1 = { 4.0f };
		// width - 此 BasicStroke 的寬度。此寬度必須大於或等於 0.0f。如果將寬度設定為
		// 0.0f,則將筆劃呈現為可用於目標裝置和抗鋸齒提示設定的最細線條。
		// cap - BasicStroke 端點的裝飾
		// join - 應用在路徑線段交匯處的裝飾
		// miterlimit - 斜接處的剪裁限制。miterlimit 必須大於或等於 1.0f。
		// dash - 表示虛線模式的陣列
		// dash_phase - 開始虛線模式的偏移量

		// 設定畫虛線
		g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 4.0f, dash1, 0.0f));

		// 設定列印字型(字型名稱、樣式和點大小)(字型名稱可以是物理或者邏輯名稱)
		font = new Font("宋體", Font.PLAIN, 11);
		g2.setFont(font);// 設定字型
		float heigth = font.getSize2D();// 字型高度
		// 標題
		g2.drawString("廣西機電工程學校智慧超市", (float) x, (float) y + heigth);
		float line = 2 * heigth;

		font = new Font("宋體", Font.PLAIN, 8);
		g2.setFont(font);// 設定字型
		heigth = font.getSize2D();// 字型高度

		// 顯示收銀員
		g2.drawString("收銀員:" + cashier, (float) x, (float) y + line);
		// 顯示訂單號
		g2.drawString("訂單號:" + orders, (float) x + 100, (float) y + line);

		line += heigth;
		// 顯示標題
		g2.drawString("名稱", (float) x + 20, (float) y + line);
		g2.drawString("單價", (float) x + 60, (float) y + line);
		g2.drawString("數量", (float) x + 85, (float) y + line);
		g2.drawString("總額", (float) x + 115, (float) y + line);
		line += heigth;
		g2.drawLine((int) x, (int) (y + line), (int) x + 158, (int) (y + line));

		// 第4行
		line += heigth;

		// 顯示內容
		for (int i = 0; i < list.size(); i++) {

			Commodity commodity = list.get(i);

			g2.drawString(commodity.getName(), (float) x, (float) y + line);
			line += heigth;

			g2.drawString(commodity.getBarcode(), (float) x, (float) y + line);
			g2.drawString(commodity.getUnit_price(), (float) x + 60, (float) y + line);
			g2.drawString(commodity.getNum(), (float) x + 90, (float) y + line);
			g2.drawString(commodity.getSum(), (float) x + 120, (float) y + line);
			line += heigth;

		}
		line += heigth;

		g2.drawLine((int) x, (int) (y + line), (int) x + 158, (int) (y + line));
		line += heigth;

		g2.drawString("售出商品數:" + sale_num + "件", (float) x, (float) y + line);
		g2.drawString("合計:" + sale_sum + "元", (float) x + 70, (float) y + line);
		line += heigth;
		g2.drawString("實收:" + practical + "元", (float) x, (float) y + line);
		g2.drawString("找零:" + changes + "元", (float) x + 70, (float) y + line);
		line += heigth;
		g2.drawString("時間:" + Calendar.getInstance().getTime().toLocaleString(), (float) x, (float) y + line);

		line += heigth;
		g2.drawString("天天平價,日日新鮮", (float) x + 20, (float) y + line);

		line += heigth;
		g2.drawString("錢票請當面點清,離開櫃檯恕不負責", (float) x, (float) y + line);
		switch (pageIndex) {
		case 0:

			return PAGE_EXISTS;
		default:
			return NO_SUCH_PAGE;

		}

	}

}
3.列印小票
	/**
	 * 列印銷售小票
	 * 
	 * @param order
	 *            訂單號
	 * @param num
	 *            數量
	 * @param sum
	 *            總金額
	 * @param practical
	 *            實收
	 * @param change
	 *            找零
	 */
	private void PrintSale(String order, String num, String sum, String practical, String change) {
		try {
			// 通俗理解就是書、文件
			Book book = new Book();
			// 設定成豎打
			PageFormat pf = new PageFormat();
			pf.setOrientation(PageFormat.PORTRAIT);

			ArrayList<Commodity> cmd_list = new ArrayList<Commodity>();
			// 取出資料
			for (int i = 0; i < list.size(); i++) {
				Collect c = list.get(i);
				Commodity cd = new Commodity(c.getName(), String.valueOf(c.getSell()), String.valueOf(c.getNum()),
						String.valueOf(c.getTotal()), c.getCode());
				cmd_list.add(cd);
			}

			// 通過Paper設定頁面的空白邊距和可列印區域。必須與實際列印紙張大小相符。
			Paper paper = new Paper();
			paper.setSize(158, 30000);// 紙張大小
			paper.setImageableArea(0, 0, 158, 30000);// A4(595 X
														// 842)設定列印區域,其實0,0應該是72,72,因為A4紙的預設X,Y邊距是72
			pf.setPaper(paper);

			book.append(new SalesTicket(cmd_list, Windows.user_num, order, num, sum, practical, change), pf);

			// 獲取列印服務物件
			PrinterJob job = PrinterJob.getPrinterJob();
			// 設定列印類
			job.setPageable(book);

			job.print();
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}




//-------使用方法----------
// 訂單編號 數量 總額 實收 找零 負責人
				String num = String.valueOf(number);
				String sum1 = String.valueOf(CD.getSum());
				String ptc = CD.getPractical();
				String change = String.valueOf(CD.getChange());
				String mess[] = { order, num, sum1, ptc, change, Windows.user_num };
				// 插入資料到訂單銷售表
				tm.InsertOrders(mess);
				// 列印小票
				PrintSale(order, num, sum1, ptc, change);