2011-11-21 59 views
0

我需要一些關於Java Print API的建議。我有一個程序可以通過Vector來打印每個項目。我試圖這樣做,當行數達到一定數量時,它開始一個新的頁面。我一直在尋找一個Java Tutorial example找出來,但它仍然無法正常工作。在調試模式下,一切似乎都正常工作,但出於某種原因,它將在第一張紙上打印第二頁,並將第二張紙留空。有誰知道這可能是什麼原因造成的?Java Print API跳過第一頁

編輯:下面是打印方法的代碼:

public int print(Graphics g, PageFormat pf, int page) 
     throws PrinterException { 
    page = pageNum; 
    Paper paper = pf.getPaper(); 
    pf.setOrientation(PageFormat.PORTRAIT); 
    paper.setSize(8.5 * 72, 11 * 72); 
    paper.setImageableArea(0.5 * 72, 0.5 * 72, 7.5 * 72, 10 * 72); 
    pf.setPaper(paper); 

    if (page > pageTot) { 
     return NO_SUCH_PAGE; 
    } 
    if (cov) { 
     g = drawCenteredString(dateS, 108, g, 56, Font.BOLD); 
     g = drawCenteredString("Don Stewart Daily Fulfillment", 216, g, 38, 
       Font.BOLD); 
     g = drawCenteredString(((FileNode) ((TreePath) printPaths.get(j)) 
       .getPathComponent(((TreePath) printPaths.get(j)) 
         .getPathCount() - 5)).toString(), 324, g, 64, 
       Font.BOLD); 
     g = drawCenteredString(((TreePath) printPaths.get(j)) 
       .getPathComponent(
         ((TreePath) printPaths.get(j)).getPathCount() - 3) 
       .toString(), 432, g, 42, Font.BOLD); 
     g = drawCenteredString("File Name Printed: " 
       + ((TreePath) printPaths.get(j)).getLastPathComponent() 
         .toString(), 540, g, 14, Font.BOLD); 
     g = drawCenteredString("File Location: " 
       + ((FileNode) ((TreePath) printPaths.get(j)) 
         .getLastPathComponent()).getFile() 
         .getAbsolutePath(), 648, g, 12, Font.PLAIN); 
    } 


    if (summ) { 
     int lineCount; 
     lineCount = 0; 
     int lineSpacing = 14; 
     int lineStart = 13 * 14; 
     g.setFont(new Font("Dialog", Font.PLAIN, 10)); 
     boolean color = true; 
      g = drawCenteredString(dateS, 72, g, 38, Font.BOLD); 
      g = drawCenteredString("Don Stewart Daily Summary List - " 
        + (pageNum + 1) + " of " + pageTot, 120, g, 20, Font.BOLD); 
      g.setFont(new Font("Dialog", Font.PLAIN, 10)); 
      g.drawString(
        ((TreePath) printPaths.get(x-1)) 
          .getPathComponent(
            ((TreePath) printPaths.get(x-1)) 
              .getPathCount() - 3).toString() 
          + " : " 
          + ((TreePath) printPaths.get(x-1)) 
            .getPathComponent(
              ((TreePath) printPaths.get(x-1)) 
                .getPathCount() - 5) 
            .toString(), 36, lineCount 
          * lineSpacing + lineStart); 
      lineCount++; 

     int k; 
     for (k = x; k < printPaths.size() && lineCount <= 41; k++) { 
      String type = ((TreePath) printPaths.get(k)).getPathComponent(
        ((TreePath) printPaths.get(k)).getPathCount() - 5) 
        .toString(); 
      String date = ((TreePath) printPaths.get(k)).getPathComponent(
        ((TreePath) printPaths.get(k)).getPathCount() - 3) 
        .toString(); 
      String typeU = ((TreePath) printPaths.get(k - 1)) 
        .getPathComponent(
          ((TreePath) printPaths.get(k)).getPathCount() - 5) 
        .toString(); 
      String dateU = ((TreePath) printPaths.get(k - 1)) 
        .getPathComponent(
          ((TreePath) printPaths.get(k)).getPathCount() - 3) 
        .toString(); 
      if (!(type == typeU) && (date == dateU)) { 
       lineCount++; 
       g.setColor(c1); 
       g.drawString(date + " : " + type, 36, lineCount 
         * lineSpacing + lineStart); 
       // lineCount++; 
      } 
      if (color) 
       g.setColor(c1); 
      else 
       g.setColor(c2); 
      g.drawString(((TreePath) printPaths.get(k)) 
        .getLastPathComponent().toString(), 54, lineCount 
        * lineSpacing + lineStart); 
      color = !color; 
      lineCount++; 
     } 
     pageNum++; 
     x = k; 
    } 
    return PAGE_EXISTS; 
} 
+0

您需要向我們展示相關代碼,以便我們可以幫助您。 – npinti

+0

對不起,補充。 – SaintWacko

+0

您似乎正在將頁碼傳遞給該方法,但似乎您正在用其他值覆蓋該變量。也許你正在打印第1頁(請記住,在Java中,第一個數字通常是0) – npinti

回答

0

我發現教程示例工作,所以我最終複製了從它的print()方法,然後用我的具體drawString命令來修改它。我仍然不知道我寫的是什麼問題,但現在一切正常。