我无法通过我的热敏打印机 (Epson TM-T81) 进行打印。当我打印时,我只得到白纸,上面没有任何打印。可能是什么原因?我正在使用以下代码:
public void printThisBill()
{
DefaultTableModel mod = (DefaultTableModel) jTable1.getModel();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
//get current date time with Date()
Date date = new Date();
Date time = new Date();
String Date = dateFormat.format(date);
String Time = timeFormat.format(time);
String Header =
" ****Sweets Shop**** \n"
+ "Date: "+Date+" Time: "+Time+"\n"
+ "---------------------------------\n"
+ "Name Qty Rate Amt\n"
+ "---------------------------------\n";
String amt= "\n \n \nTotal Amount = "+amt()+"\n"
+ "Tax ="+tax()+ "\n"
+ "*********************************\n"
+ "Thank you. \n";
String bill = Header;
int i =0;
do
{
String name = ""+ mod.getValueAt(i, 2);
String qty = ""+mod.getValueAt(i, 3);
String rate = ""+mod.getValueAt(i, 4);
String amount = ""+mod.getValueAt(i, 6);
String items =
name+"\t"+qty+"\t"+rate+"\t"+amount+"\n";
bill = bill+ items;
i++;
}
while(i <= mod.getRowCount()-1);
bill = bill+amt;
System.out.println(bill);
printCard(bill);
dispose();
}
并创建图形和打印我使用以下代码:
public static void printCard(final String bill ){
Printable contentToPrint = new Printable(){
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex >0){return NO_SUCH_PAGE;} //Only one page
Graphics2D g = (Graphics2D) graphics.create(); //Cast to Graphics2D object
g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
g.drawString (bill, 0, 0); //Print Hello World at offset (100, 100)
return PAGE_EXISTS; //Page exists (offsets start at zero!)
}
};
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(contentToPrint);
//You can show a print dialog before printing by job by wrapping the following blocks with a conditional statement if(job.printDialog()){...}
try {
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
}
}