2011-03-15 4 views
1

JFrame의 내용을 인쇄하는 방법은 무엇입니까?자바에서 인쇄 jframe

+5

화면, 표준 출력 또는 프린터에 인쇄 하시겠습니까? –

+1

Oohh JFrame을 AsciiArt 변환기로 변환하면 재미있을 것입니다. –

+1

이 링크는 당신이 찾고있는 것을 정확하게 제공합니다. 유용한 추가 정보 http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html –

답변

2
 PrinterJob job = PrinterJob.getPrinterJob(); 
    job.setPrintable(this); 
    boolean ok = job.printDialog(); 
     if (ok) 
     { 
      try 
      { 
       job.print(); 
      } 
      catch (PrinterException ex) 
      { 
      /* The job did not successfully complete */ 
      } 
     } 

     public int print(Graphics g, PageFormat pf, int page) throws 
                PrinterException { 

    if (page > 0) { /* We have only one page, and 'page' is zero-based */ 
     return NO_SUCH_PAGE; 
    } 

    /* User (0,0) is typically outside the imageable area, so we must 
    * translate by the X and Y values in the PageFormat to avoid clipping 
    */ 
    Graphics2D g2d = (Graphics2D)g; 
    g2d.translate(pf.getImageableX(), pf.getImageableY()); 

    /* Now print the window and its visible contents */ 
    jFrame.paint(g2d); 
    jFrame.repaint(); 
    /* tell the caller that this page is part of the printed document */ 
    return PAGE_EXISTS; 
}