有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在一个PrinterJob中打印多个JPanel

到目前为止,我的情况如下:

//选项卡是一个JTabbedPane

    for(int i = 0; i < tab.getTabCount(); i ++){
        if(tab.getComponent(i) instanceof DuctReport)
            PrintUtilities.printComponent(tab.getComponent(i), DuctReport.PRINT_SIZE);
        else if(tab.getComponent(i) instanceof Vandy)
            PrintUtilities.printComponent(tab.getComponent(i), Vandy.PRINT_SIZE);
        else
            PrintUtilities.printComponent(tab.getComponent(i), .8);
    }

//以下是PrintUtilities的代码:

    public class PrintUtilities implements Printable{

    private Component componentToBePrinted;
    private double scale;

    public static void printComponent(Component c,double scale) {
        new PrintUtilities(c,scale).print();
    }

    public PrintUtilities(Component componentToBePrinted, double scale) {
        this.componentToBePrinted = componentToBePrinted;
        this.scale = scale;

    }

    public void print() {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(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) {
            return(NO_SUCH_PAGE);
        } else {
            Graphics2D g2d = (Graphics2D)g;
            g2d.scale(scale, scale);
            g2d.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
            disableDoubleBuffering(componentToBePrinted);
            componentToBePrinted.paint(g2d);
            enableDoubleBuffering(componentToBePrinted);
            return(PAGE_EXISTS);
        }
    }

}

当我做我所拥有的事情时,我必须单独单击要打印的每个组件的“打印”,我能在一个printerjob中打印所有组件吗

我还需要将组件放在页面的中心,我尝试了以下操作:

g2d.translate((pageFormat.getImageableWidth()-componentToBePrinted.getWidth)/2,
              pageFormat.getImageableY());

但在某些情况下,组件的宽度大于可成像的宽度,有什么想法吗


共 (0) 个答案