2010-06-23 22 views
0
private void printCard() { 

     PrinterJob printjob = PrinterJob.getPrinterJob(); 
     printjob.setJobName("Label"); 

     Printable printable = new Printable() { 

       public int print(Graphics pg, PageFormat pf, int pageNum) { 

         if (pageNum > 0) { 
           return Printable.NO_SUCH_PAGE; 
         } 

         Dimension size = jLayeredPane2.getSize(); 
         BufferedImage bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); 

         jLayeredPane2.print(bufferedImage.getGraphics()); 

         Graphics2D g2 = (Graphics2D) pg; 
         g2.translate(pf.getImageableX(), pf.getImageableY()); 
         g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null); 

         return Printable.PAGE_EXISTS; 
       } 
     }; 

     Paper paper = new Paper(); 
     paper.setImageableArea(0, 0, 153, 243); 
     paper.setSize(243, 154); 

     PageFormat format = new PageFormat(); 
     format.setPaper(paper); 
     format.setOrientation(PageFormat.LANDSCAPE); 

     printjob.setPrintable(printable, format); 
     if (printjob.printDialog() == false) 
       return; 

     try { 
       printjob.print(); 
     } catch (PrinterException ex) { 
       System.out.println("NO PAGE FOUND." + ex); 

     } 
} 
+0

你能更具體嗎? – 2010-06-24 01:00:32

回答

1

從我在代碼中看到的內容,您可以撥打if (printjob.printDialog() == false)。這將始終嘗試顯示本地打印機屬性對話框。 boolean返回值取決於用戶是單擊「確定」還是取消對話框。如果要禁止對話框,請刪除if塊,因爲要執行的打印工作是通過printjob.print()調用完成的。

+0

謝謝,就是這樣!我評論了這部分,並且我不再有打印對話框。我點擊了我的程序中的打印按鈕,它只是打印到默認打印機。正是我想要的。非常感謝! – Magwich 2010-06-25 19:22:48