當前位置:商標查詢大全網 - 彩票開獎 - java是如何實現打印功能的?

java是如何實現打印功能的?

print . Java-打印內容定義

[代碼]

導入Java . awt . *;

導入Java . awt . event . *;

導入Java . awt . print . *;

導入Java . awt . image . *;

導入javax . swing . *;

導入javax . swing . event . *;

導入javax . swing . border . *;

導入javax . swing . event . *;

導入Java . util . *;

導入Java . io . *;

公共類打印實現可打印{

int m _ wPage

int m _ hPage

int m _ orientation

可打印的m _ target

int maxNumPage = 1;

String title= "數據表打印";

Font titleFont=new Font ("bold ",字體。加粗,14);

布爾hasTail = true

int tail align = 0;

int head align = 0;

int topSpace = 0;

int left space = 0;

int yStart = 0;

int yEnd = 0;

int xStart = topSpace

int xEnd = 0;

int x=0,y = 0;

String strTemp= "打印內容";

public void doPrint(){

嘗試{

m_orientation=PageFormat。人像;

//設置打印對象和默認紙張。

printer job prn job = printer job . getprinter job();

page format page format = prn job . default page();

page format . set orientation(m _ orientation);

m _ wPage =(int)(page format . getwidth());

m _ hPage =(int)(page format . getheight());

//根據默認紙張設置,將要打印的表單傳入打印對象。

prnJob.setPrintable(this,page format);

如果(!prnJob.printDialog())返回;

prn job . print();

}catch(PrinterException ex){

ex . printstacktrace();

System.err.println("打印錯誤:"+ex . tostring());

}

}

/**

*初始化打印參數

*/

public void initPrintParameter()

{

}

/**

*構造打印機要打印的打印內容。

*/

公共int print(圖形pg,頁面格式頁面格式,

int pageIndex)引發PrinterException {

//初始化打印參數

initPrintParameter();

//將畫布設置為頁面大小

pg . translate((int)page format . getimageablex(),

(int)page format . getimageabley());

int w page = 0;

int h page = 0;

//根據打印機頁面設置調整畫布大小。

if(page format . get orientation()= = page format。肖像){

wPage =(int)page format . getimageablewidth();

hPage =(int)page format . getimageableheight();

}

否則{

wPage =(int)page format . getimageablewidth();

wPage+= wPage/2;

hPage =(int)page format . getimageableheight();

pg.setClip(0,0,wPage,h page);

}

wPage = wPage-2 * left space;

h page = h page-2 * topSpace;

xStart = leftSpace

xEnd = w page-2;

//設置畫布的顏色和字體

int y = topSpace

pg . set font(title font);

pg . set color(color . black);

//畫出標題並居中。

font fn = pg . get font();

font metrics FM = pg . getfont metrics();

y+= FM . getascent();

alignText(title,pg,y,xStart,xEnd,head align);

y+= 30;

x = left space+2;

Font headerFont =新字體。加粗,14);

pg . set font(header font);

FM = pg . getfont metrics();

int h = FM . getascent();

yStart = y-1;

y+= h;

pg . set font(header font);

FM = pg . getfont metrics();

int header = y;

h = FM . get height();

//計算行高、每頁行數、總行數以及指定頁碼的起始行和結束行。

int rowH=Math.max(h,10);

int tailH = rowH+30;

int rowPerPage = 0;

int left pix = 0;

if(hasTail){

rowPerPage =(h page-header-tailH)/rowH;

left pix =(h page-header-tailH)% rowH;

yEnd = h page-left pix-tailH+2;

}

否則{

rowPerPage =(h page-header)/rowH;

left pix =(h page-header)% rowH;

yEnd = h page-left pix+2;

}

pg.drawString(strTemp,x,y);

//繪制表格邊框

pg.drawLine(xStart,yStart,xStart,yEnd);

pg.drawLine(xStart,yStart,xEnd,y start);

pg.drawLine(xEnd,yStart,xEnd,yEnd);

pg.drawLine(xStart,yEnd,xEnd,yEnd);

//打印頁碼

if(hasTail){

int page number = page index+1;

string s = " page "+page number+";

alignText(s,pg,yEnd+30,xStart,xEnd,tail align);

}

system . GC();

返回PAGE _ EXISTS

}

/**

*文本排列,y坐標,顯示範圍(開始-結束)

* 0表示居中顯示,1表示左對齊,2表示右對齊。

*/

private void alignText(String s,Graphics pg,int y,int start

int end,int mode){

font fn = pg . get font();

font metrics FM = pg . getfont metrics();

int wString = FM . string width(s);

int x = start

開關(模式)

{

案例0:

if((end-start-wString)& gt;0)x = start+(end-start-wString)/2;

打破;

案例1:

打破;

案例二:

if((end-start-wString)& gt;0)x = start+(end-start-wString);

打破;

}

pg .抽繩(s,x,y);

}

公共靜態void main(String[] args){

Print p = new Print();

p . do print();

}

}

[代碼]

操作方法:

& gt賈瓦茨-Print.java

& gtjava打印

自己跑吧