如何在多页中打印大型 JPanel [英] How to print a large JPanel in several page

查看:27
本文介绍了如何在多页中打印大型 JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个非常大的面板,这个面板包含一些组件,如 jtable、jlabel 和其他 jpanel.现在我想在不同的页面上打印它.但我不知道该怎么做.我已经在我的面板类中实现了 Printable.但是如果我打印它,我只会得到一页.

I want to print a very large panel and this panel contains some components like jtable, jlabel and others jpanel. Now i want to print it in differents pages. But i don't know how to do it. I have implemented Printable in my panel class. But if i print it, I get only one page.

推荐答案

我对 Harry 的问题的编辑不是接受了,所以我将我的编辑作为新答案发布.

My edit to the question from Harry was not accepted, so I post my edits as a new answer.

以下代码对我有用(我测试过):

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.*;

import javax.swing.RepaintManager;

public class PrintMultiPageUtil implements Printable, Pageable {
    private Component componentToBePrinted;
    private PageFormat format;
    private int numPages;

    public PrintMultiPageUtil(Component componentToBePrinted) {
        this.componentToBePrinted = componentToBePrinted;

        // get total space from component  
        Dimension totalSpace = this.componentToBePrinted.getPreferredSize();

        // calculate for DIN A4
        format = PrinterJob.getPrinterJob().defaultPage();
        numPages = (int) Math.ceil(totalSpace .height/format.getImageableHeight());
    }

    public void print() {
        PrinterJob printJob = PrinterJob.getPrinterJob();

        // show page-dialog with default DIN A4
        format = printJob.pageDialog(printJob.defaultPage());

        printJob.setPrintable(this);
        printJob.setPageable(this);

        if (printJob.printDialog())
            try {
                printJob.print();
            } catch(PrinterException pe) {
                System.out.println("Error printing: " + pe);
            }
    }

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
        if ((pageIndex < 0) | (pageIndex >= numPages)) {
            return(NO_SUCH_PAGE);
        } else {
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY() - pageIndex * pageFormat.getImageableHeight());
            disableDoubleBuffering(componentToBePrinted);
            componentToBePrinted.paint(g2d);
            enableDoubleBuffering(componentToBePrinted);
            return(PAGE_EXISTS);
        }
    }

    public static void disableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(false);
    }

    public static void enableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(true);
    }

    @Override
    public int getNumberOfPages() {
        // TODO Auto-generated method stub
        return numPages;
    }

    @Override
    public PageFormat getPageFormat(int arg0) throws IndexOutOfBoundsException {
        return format;
    }

    @Override
    public Printable getPrintable(int arg0) throws IndexOutOfBoundsException {
        // TODO Auto-generated method stub
        return this;
    }
}

numPages

我将 numPages 的表达式更改为:

I changed the expression for numPages to:

(int) Math.ceil(page.height/format.getImageableHeight())

这将总高度(jpanel 的高度)除以一页的高度,从而计算出所有页数.

This divides the total height (height of the jpanel) through the height of one page, thus calculating the number of all pages.

g2d.translate

我做了以下更改:在这一行:

I did the following change: In this line:

g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY() - pageIndex * pageFormat.getImageableHeight());

componentToBePrinted.getPreferredSize().height 更改为 pageFormat.getImageableHeight().g2d.translate 的第一个或第二个参数的正值分别将图形向右或向下移动.

Changed componentToBePrinted.getPreferredSize().height to pageFormat.getImageableHeight(). A positive value for the first or the second parameter of g2d.translate moves the graphic to the right or down respectively.

.getImageableX().getImageableY() 帮助定位图形,使其不与填充重叠.

.getImageableX() and .getImageableY() help position the graphic so that it doesn't overlap with the padding.

对于pageIndex大于0- pageIndex * pageFormat.getImageableHeight()移动图片pageIndex-乘以页面高度到顶部.因此,pageIndex 所指的区域被打印出来.

For pageIndex greater than 0, - pageIndex * pageFormat.getImageableHeight() moves the image pageIndex-times the page-height to the top. So the area, that the pageIndex refers to is printed.

原始损坏源:

https://community.oracle.com

这篇关于如何在多页中打印大型 JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆