簡體   English   中英

如何在可滾動的JFrame中打印所有swing組件?

[英]How to print all the swing components in a scrollable JFrame?

我正在嘗試打印一個由可滾動JFrame的swing組件組成的表單。 我已經嘗試了下面的代碼來做,但我得到的打印只是當前窗口中的滾動區域。 我需要打印整個頁面。

碼:

public int print(Graphics arg0, PageFormat arg1, int arg2) throws PrinterException {
    // TODO Auto-generated method stub
    //return 0;
    if (arg2 > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    Graphics2D g2d = (Graphics2D)arg0;
    g2d.translate((int)arg1.getImageableX(), (int)arg1.getImageableY());  

    float pageWidth = (float)arg1.getImageableWidth();  
    float pageHeight = (float)arg1.getImageableHeight();  

    float imageHeight = (float)this.getHeight();  
    float imageWidth = (float)this.getWidth();  

    float scaleFactor = Math.min((float)pageWidth/(float)imageWidth, (float)pageHeight/(float)imageHeight);  

    int scaledWidth = (int)(((float)imageWidth)*scaleFactor);  

    int scaledHeight = (int)(((float)imageHeight)*scaleFactor);    

    BufferedImage canvas = new BufferedImage( this.getWidth(),  this.getHeight(), BufferedImage.TYPE_INT_RGB);  
    Graphics2D gg = canvas.createGraphics();  
    this.paint( gg );    
    Image img = canvas ;  

    g2d.drawImage(img, 0, 0, scaledWidth, scaledHeight, null );  

    return Printable.PAGE_EXISTS;  

}  

private void button4ActionPerformed() {
    // TODO add your code
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {
        try {
             job.print();
        } catch (PrinterException ex) {
            System.out.println("Error printing: " + ex);
         /* The job did not successfully complete */
        }
    }
}

此外,我需要隱藏一些按鈕和textFields打印。 (例如:“打印”按鈕)任何幫助將不勝感激。

而不是this.paint( gg ); 調用componentInTheScrollPane.paint(gg); 甚至更好地調用printAll(gg);

如果您嘗試將jScrollPane繪制到圖像,它將每次都失敗並僅打印可見部分。 要解決這個問題,您需要繪制jScrollPane的內容,理想情況下,您將在jScrollPane中有一個jLayer,jPanel或其他容器,並且可以直接將其繪制到圖像中,如下所示:

Form (jPanel top container)
 -> jScrollPane
     -> jLayer/jPanel/container
         -> Contents (tables, buttons etc)  

我不會給你如何使用整個scrollPane打印整個表單的代碼,這需要一些數學和更多的代碼,但是為了讓你從這里開始是一個示例方法,將jLayer的內容繪制到緩沖的圖像,無論如何如果它在jScrollPane中不在視圖中,然后將該圖像保存到文件:(我不會覆蓋打印這是另一個問題)

private void button4ActionPerformed() {
    try
    {
        //get height/width from the jLayeredPane or jPanel inside the scroll pane
        int imageHeight = jLayeredPane.getHeight();
        int imageWidth = jLayeredPane.getWidth();

        //Draw contents of the jLayeredPane or jPanel inside the scroll pane (but not the scroll pane itself)
        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);  
        Graphics2D gg = image.createGraphics();  
        jLayeredPane.paint(gg);

        //The "image" now has jLayers contents
        //Insert your code here to scale and print the image here
        //Some Code
        //In my example I have chosen to save the image to file with no scaling
        ImageIO.write(canvas, "png", new File("C:\\out.png"));
        //Some Code

        System.out.println("Done");
    }
    catch (IOException ex)
    {
        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM