首先
代碼如下
PrintTest
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import
import
public class PrintTest implements Printable
{
private String roomID;
private Date printDate;
private Integer rentType;
private Float waterFee;
private Float electricityFee;
private Float rent;
private String remark;
private static int RECEIPT_NO =
public PrintTest()
{
}
public PrintTest printTest;
public PrintTest(RentRecordParameter prentRecord
{
this
this
this
this
this
this
this
}
/** */
/**
* @param Graphic指明打印的圖形環境
* @param PageFormat指明打印頁格式(頁面大小以點為計量單位
* @param pageIndex指明頁號
**/
public int print(Graphics graphics
throws PrinterException
{
//print string
String receiptTitle =
String content
String feeItem = null;
Float rentTotal =
String content
//收款的內容(根據租賃類型
switch(this
{
case
feeItem =
rentTotal=this
break;
case
feeItem =
rentTotal=this
break;
default:
feeItem =
}
//轉換成Graphics
Graphics
//設置打印顏色為黑色
graphics
//打印起點坐標x=
double x = pf
double y = pf
System
switch (pageIndex)
{
case
//設置打印字體(字體名稱
//Java平台所定義的五種字體系列
Font font = new Font(
graphics
BasicStroke basicStroke=new BasicStroke(
float[] dash
//設置打印線的屬性
//
// graphics
// BasicStroke
//
//graphics
graphics
float height = font
System
//繪制收據標題
graphics
//繪制收據編號
String receiptNo = String
graphics
//繪制打印時間
SimpleDateFormat myDateFormat = new SimpleDateFormat(
graphics
//繪制收據標題下面的第一條橫線
graphics
(int) (y +
(int) (y +
);
//繪制收據標題下面的第二條橫線
graphics
(int) (y +
(int) (y +
);
//正文
graphics
//收費項目名稱
graphics
//繪制正文
graphics
(int)x*
(int) (y +
(int)x+
(int) (y +
);
//人民幣(大寫)
graphics
//繪制人民幣(大寫)下劃線
graphics
(int)x*
(int) (y +
(int)x+
(int) (y +
);
//(小寫)
graphics
//繪制(小寫)下劃線
graphics
(int)x*
(int) (y +
(int)x+
(int) (y +
);
//附注
graphics
//繪制附注下劃線
graphics
(int)x*
(int) (y +
(int)x+
(int) (y +
);
// 簽字(收款人)
graphics
//簽字(收款人)下劃線
graphics
(int)x*
(int) (y +
(int)x+
(int) (y +
);
return PAGE_EXISTS;
default:
RECEIPT_NO=RECEIPT_NO
return NO_SUCH_PAGE;
}
}
//方法調用
public void printReceipt(PrintTest printTest){
//通俗理解就是書
Book book = new Book();
//設置成豎打
PageFormat pf = new PageFormat();
pf
//通過Paper設置頁面的空白邊距和可打印區域
Paper p = new Paper();
p
p
pf
//把 PageFormat 和 Printable 添加到書中
book
//獲取打印服務對象
PrinterJob job = PrinterJob
//設置打印類
job
try
{
//可以用printDialog顯示打印對話框
// boolean isShowing=job
// if(isShowing)
// {
// System
RECEIPT_NO=RECEIPT_NO+
job
// }
}
catch (PrinterException e)
{
e
}
}
public static void main(String[] args)
{
// 通俗理解就是書
Book book = new Book();
// 設置成豎打
PageFormat pf = new PageFormat();
pf
// 通過Paper設置頁面的空白邊距和可打印區域
Paper p = new Paper();
p
p
pf
// 把 PageFormat 和 Printable 添加到書中
book
//獲取打印服務對象
PrinterJob job = PrinterJob
// 設置打印類
job
try
{
//可以用printDialog顯示打印對話框
//boolean a=job
//if(a)
//{
job
//}
}
catch (PrinterException e)
{
e
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25778.html