2013-10-03 195 views
0

我知道Java中的某些組件(JTable,JTextArea)有自己的方式將它們傳遞給頁面的頁眉和頁腳。
打印一個基於我自己實現的Printable的頁面,還是我應該自己定位它們(​​例如,在Printable接口的overriden print(...)方法中)?
這個可打印的類作爲輸入接收plaint文本包,並多數民衆贊成在所有。 當然最後我可以把所有東西放在JTextArea中,但如果有其他方法,我不想這樣做。
此致敬禮。Java:使用頁眉和頁腳打印自定義(可打印)頁面

回答

1

您需要將它們放在覆蓋的print()方法中。

public class TestReport implements Printable 
{ 
    //Here is where we set up the class variables. 
    private int numberOfPages = 0;    //numberOfPages -- How many pages we will be printing. 
    private int headerLines = 0;    //headerLines -- How many lines are in the header. 
    private int footerLines = 0;    //footerLines -- How many lines are in the footer. 
    private int bodyLines = 0;     //bodyLines -- How many lines we've added to the body of the report. 
    private boolean useDefaultFooter = false; //useDefaultFooter -- If we are to use the default footer instead of a custom one. 
    private int fontSize = 10;     //fontSize -- The size of the font to use, in points. The default is 10pt. 
    private Vector header = new Vector();  //header -- The vector containing the header text. 
               //Each item in the vector is a line on the header. 
    private Vector body = new Vector();   //body -- The vector containing the body text. 
    private Vector footer = new Vector();  //footer -- The vector containing the footer. 

    //The constructor does not need to do anything. 

    /** 
    * Create a new instance or TestReport 
    */ 
    public TestReport() 
    { 

    } 

    /** 
    * Prints the final report after the information has been added. 
    */ 
    public void printReport() 
    { 

     //Get the number of lines in the header, body, and footer. If we're using the default footer, it's only one line long. 

     headerLines = header.size(); 
     bodyLines = body.size(); 
     if (useDefaultFooter == true) { 
      footerLines = 1; 
     } else { 
      footerLines = footer.size(); 
     } 
     PrinterJob printerJob = PrinterJob.getPrinterJob(); 
     PageFormat landscape = printerJob.defaultPage(); 
     landscape.setOrientation(PageFormat.LANDSCAPE); 
     printerJob.setPrintable(TestReport.this, landscape); 

     printerJob.printDialog(); 

     try { 
      printerJob.print(); 
     } catch (Exception PrintException) {} 
    } 

    public void addHeaderLine (String headerLine) 
    { 
     header.addElement(headerLine); 
    } 

    public void addBodyLine (String bodyLine) 
    { 
     body.addElement(bodyLine); 
    } 

    public void addFooterLine (String footerLine) 
    { 
     footer.addElement(footerLine); 
    } 

    public void setFontSize(int fontSize) 
    { 
     this.fontSize = fontSize; 
    } 

    public void useDefaultFooter() 
    { 
     useDefaultFooter = true; 
    } 


    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException 
    { 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setColor(Color.black); 
     Font myFont = new Font("Courier", Font.PLAIN, fontSize); 
     g2.setFont(myFont); 

     int fontHeight = g2.getFontMetrics().getHeight(); 
     int fontDescent = g2.getFontMetrics().getDescent(); 

     double pageHeight = pageFormat.getHeight(); 
     double pageWidth = pageFormat.getWidth(); 

     int lineHeight = fontHeight + fontDescent; 

     int linesPerPage = (int)((pageHeight - 72 - 72)/lineHeight); 

     int bodyLinesPerPage = linesPerPage - headerLines - footerLines; 

     int numberOfPages = (int)Math.ceil((double)bodyLines/bodyLinesPerPage); 

     if (pageIndex >= numberOfPages) 
     { 
      return Printable.NO_SUCH_PAGE; 
     } 
     //Start at one inch down, one inch accross 
     int currentY = 72 + lineHeight; 
     int currentX = 72; 

     //Draw the header 
     for (int i = 0; i < header.size(); i++) 
     { 
      g2.drawString(header.get(i), currentX, currentY); 
      currentY = currentY + lineHeight; 
     } 
     //Draw the body 
     for (int i = bodyLinesPerPage * pageIndex; i < (bodyLinesPerPage * (pageIndex + 1)) ; i++) 
     { 
      if (i < body.size()){ 
       g2.drawString(body.get(i), currentX, currentY); 
      } 
      currentY = currentY + lineHeight; 
     } 
     if (useDefaultFooter == true) 
     { 
      GregorianCalendar calendar = new GregorianCalendar(); 
      int todayYear = calendar.get(GregorianCalendar.YEAR); 
      int todayMonth = calendar.get(GregorianCalendar.MONTH) + 1; 
      int todayDay = calendar.get(GregorianCalendar.DAY_OF_MONTH); 

      String theMonth = "0" + todayMonth; 
      String theDay = "0" + todayDay; 

      g2.drawString("SYSTEM DATE: " + todayYear + "-" + theMonth.substring 
          (theMonth.length() - 2) + "-" + 
          theDay.substring(theDay.length() - 2) 
          + " | PAGE " + (pageIndex + 1) 
          + " OF " + numberOfPages, currentX, currentY); 
     } 
     else 
     { 
      for (int i = 0; i < footer.size(); i++) 
      { 
       g2.drawString(footer.get(i), currentX, currentY); 
       currentY = currentY + lineHeight; 
      } 
     } 
     return Printable.PAGE_EXISTS;  
    } 
}