最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java編程打印購(gòu)物小票實(shí)現(xiàn)代碼

 更新時(shí)間:2017年11月23日 14:59:52   作者:RexFang  
這篇文章主要介紹了Java編程打印購(gòu)物小票實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。

簡(jiǎn)單介紹運(yùn)行環(huán)境:

語(yǔ)言:Java

工具:eclipse

系統(tǒng):Windows7

(打印設(shè)備暫時(shí)沒(méi)有,所以只能提供預(yù)覽圖)

最近,項(xiàng)目需要為商城做一個(gè)購(gòu)物小票的打印功能,日常我們?nèi)コ匈I(mǎi)東西,結(jié)賬的時(shí)候收銀員都會(huì)打印一個(gè)小票,一般的商城也都需要這樣的一個(gè)小功能,本文給出的 demo 是在 58mm 的熱敏打印機(jī)下的例子,如果是其他紙張類(lèi)型的打印機(jī),調(diào)整紙張寬度即可。

package test;
import java.awt.*;
import java.awt.print.*;
/**
 * 打印機(jī)測(cè)試類(lèi)(58mm)
 * 1、目標(biāo)打印機(jī)必須設(shè)置為默認(rèn)打印機(jī)
 * 2、打印頁(yè)面的寬度和具體的打印機(jī)有關(guān),一般為打印紙的寬度,需要配置成系統(tǒng)參數(shù)
 * 3、一個(gè)漢字的寬度大概是12點(diǎn)
 */
public class PrintTest {
	public static void main(String[] args){
		if(PrinterJob.lookupPrintServices().length>0){
			/*
        打印格式
       */
			PageFormat pageFormat = new PageFormat();
			//設(shè)置打印起點(diǎn)從左上角開(kāi)始,從左到右,從上到下打印
			pageFormat.setOrientation(PageFormat.PORTRAIT);
			/*
        打印頁(yè)面格式設(shè)置
       */
			Paper paper = new Paper();
			//設(shè)置打印寬度(固定,和具體的打印機(jī)有關(guān))和高度(跟實(shí)際打印內(nèi)容的多少有關(guān))
			paper.setSize(140, 450);
			//設(shè)置打印區(qū)域 打印起點(diǎn)坐標(biāo)、打印的寬度和高度
			paper.setImageableArea(0, 0, 135, 450);
			pageFormat.setPaper(paper);
			//創(chuàng)建打印文檔
			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, "http://////////////////////////////", 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, "收貨地址:北京市海淀區(qū)上地十街10號(hào)百度大廈", 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);
					//藥店名稱(chēng)
					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, "規(guī)格", (lineWidth/10)*4, yIndex, lineWidth, lineHeight);
					yIndex = drawString(graphics2D, "單價(jià)", (lineWidth/10)*8, yIndex, lineWidth, lineHeight);
					yIndex = drawString(graphics2D, "數(shù)量", (lineWidth/10)*10, yIndex, lineWidth, lineHeight);
					for (int i=0; i<5; i++){
						graphics2D.setFont(new Font("宋體", Font.PLAIN, 7));
						yIndex = drawString(graphics2D, "E復(fù)合維生素B片100片E復(fù)合維生素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, "會(huì)員名稱(chēng):小清新", 5, yIndex + lineHeight * 2, lineWidth, lineHeight);
					yIndex = drawString(graphics2D, "總  數(shù):6", 5, yIndex + lineHeight, lineWidth, lineHeight);
					yIndex = drawString(graphics2D, "總  計(jì):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, "電話(huà):020-123456", 5, yIndex + lineHeight * 2, lineWidth, lineHeight);
					yIndex = drawString(graphics2D, "地址:北京市海淀區(qū)上地十街10號(hào)百度大廈", 5, yIndex + lineHeight, lineWidth, lineHeight);
					yIndex = yIndex + 20;
					graphics2D.drawLine(0, yIndex, 140, yIndex);
					return PAGE_EXISTS;
				}
			}
			, pageFormat);
			//獲取默認(rèn)打印機(jī)
			PrinterJob printerJob = PrinterJob.getPrinterJob();
			printerJob.setPageable(book);
			try {
				printerJob.print();
			}
			catch (PrinterException e) {
				e.printStackTrace();
				System.out.println("打印異常");
			}
		} else{
			System.out.println("沒(méi)法發(fā)現(xiàn)打印機(jī)服務(wù)");
		}
	}
	/**
   * 字符串輸出
   * @param graphics2D  畫(huà)筆
   * @param text     打印文本
   * @param x       打印起點(diǎn) x 坐標(biāo)
   * @param y       打印起點(diǎn) y 坐標(biāo)
   * @param lineWidth   行寬
   * @param lineHeight  行高
   * @return 返回終點(diǎn) y 坐標(biāo)
   */
	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;
		}
	}
}

運(yùn)行結(jié)果:

效果預(yù)覽:

總結(jié)

簡(jiǎn)單說(shuō)就是編寫(xiě)一段Java程序,將輸出結(jié)果另存為“ *.xps  ”格式文件,由打印機(jī)輸出,非常簡(jiǎn)單。希望對(duì)大家有所幫助。如有問(wèn)題歡迎留言指出。感謝朋友們對(duì)本站的支持。

相關(guān)文章

  • 集群環(huán)境中使用ehcache_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    集群環(huán)境中使用ehcache_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了集群環(huán)境中使用ehcache的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • java生成圖片驗(yàn)證碼返回base64圖片信息方式

    java生成圖片驗(yàn)證碼返回base64圖片信息方式

    這篇文章主要介紹了java生成圖片驗(yàn)證碼返回base64圖片信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 關(guān)于SpringMVC的異常處理機(jī)制詳細(xì)解讀

    關(guān)于SpringMVC的異常處理機(jī)制詳細(xì)解讀

    這篇文章主要介紹了關(guān)于SpringMVC的異常處理機(jī)制詳細(xì)解讀,SpringMVC是目前主流的Web?MVC框架之一,本文將分析SpringMVC的異常處理內(nèi)容,需要的朋友可以參考下
    2023-05-05
  • SpringBoot項(xiàng)目集成FTP的方法步驟

    SpringBoot項(xiàng)目集成FTP的方法步驟

    本文主要介紹了SpringBoot項(xiàng)目集成FTP的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • SpringBoot超詳細(xì)講解集成Flink的部署與打包方法

    SpringBoot超詳細(xì)講解集成Flink的部署與打包方法

    昨天折騰了下SpringBoot與Flink集成,實(shí)際上集成特簡(jiǎn)單,主要是部署打包的問(wèn)題折騰了不少時(shí)間。想打出的包直接可以java -jar運(yùn)行,同時(shí)也可以flink run運(yùn)行,或者在flink的dashboard上上傳點(diǎn)擊啟動(dòng)。結(jié)果是不行,但是使用不同的插件打包還是可以的
    2022-05-05
  • Java實(shí)現(xiàn)簡(jiǎn)單的五子棋小游戲

    Java實(shí)現(xiàn)簡(jiǎn)單的五子棋小游戲

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單的五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • SpringBoot中Elasticsearch的連接配置原理與使用詳解

    SpringBoot中Elasticsearch的連接配置原理與使用詳解

    Elasticsearch是一種開(kāi)源的分布式搜索和數(shù)據(jù)分析引擎,它可用于全文搜索、結(jié)構(gòu)化搜索、分析等應(yīng)用場(chǎng)景,本文主要介紹了SpringBoot中Elasticsearch的連接配置原理與使用詳解,感興趣的可以了解一下
    2023-09-09
  • Kotlin 基本語(yǔ)法實(shí)例詳解

    Kotlin 基本語(yǔ)法實(shí)例詳解

    這篇文章主要介紹了Kotlin 基本語(yǔ)法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • JAVA SPI機(jī)制詳解使用方法

    JAVA SPI機(jī)制詳解使用方法

    Java定義了一套JDBC的接口,但并未提供具體實(shí)現(xiàn)類(lèi),而是在不同云廠商提供的數(shù)據(jù)庫(kù)實(shí)現(xiàn)包。這篇文章給大家介紹Java的SPI機(jī)制,感興趣的朋友一起看看吧
    2022-07-07
  • PowerJob的HashedWheelTimer工作流程源碼解讀

    PowerJob的HashedWheelTimer工作流程源碼解讀

    這篇文章主要為大家介紹了PowerJob的HashedWheelTimer工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01

最新評(píng)論

阳曲县| 虹口区| 桦南县| 固镇县| 昂仁县| 扎兰屯市| 江阴市| 阿荣旗| 井研县| 永靖县| 阳高县| 井陉县| 巴南区| 织金县| 晴隆县| 柞水县| 渭源县| 安康市| 黔江区| 五河县| 土默特左旗| 岢岚县| 吉木乃县| 安塞县| 武强县| 介休市| 道真| 康马县| 分宜县| 武宁县| 平顺县| 白朗县| 高阳县| 武城县| 宜春市| 日喀则市| 舞阳县| 舒兰市| 靖西县| 邵武市| 浠水县|