2013-07-08 173 views
0

我无法通过我的热敏打印机(Epson TM-T81)进行打印。当我打印时,我只拿到白纸,没有打印任何东西。可能是什么原因?我使用下面的代码:无法从java打印任何东西:

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()); 
    } 



} 
+0

你能从外部Java打印吗? –

+0

你可以打印到其他打印机吗? –

+0

[用java打印热敏打印机收信](http://stackoverflow.com/questions/17505070/printing-reciepts-with-thermal-printer-in-java) –

回答

0

尝试改变

g.drawString(bill,0,0); 

这个

graphics.drawString(bill,0,0); 
+0

这没有什么区别! :( –

+0

我在我的一个应用程序中完成了同样的事情,而且我的代码只有两个区别,那就是你刚刚尝试过的,而且我没有在图形上使用.create(),它只是说Graphics2D g =(Graphics2D )图形;除此之外...我难住了哈哈 – booleanCube