在Windows 7中用Java打印无法完成

时间:2019-05-29 13:43:44

标签: java swing

当我在Windows 7中通过Java应用程序打印某些内容时,输出为空白页,并且仅打印最后一个图形,但是当我在Windows 10中将其成功打印时,它会成功打印..所以我不知道为什么在Windows 7中会发生这种情况特别是它以前在Windows 7上打印,突然发生了我之前所说的(空白页)

这是我的代码以及下图中的输出

    public void printed_bill_printing() {
    totals_panelB.setVisible(true);
    footer_panelB.setVisible(true);

    int count_rows = tableEB.getRowCount();
    int count = 0;
    int table_height = 0;
    while (count <= count_rows - 1) {
        int row_height = tableEB.getRowHeight(count);
        table_height = row_height + table_height;
        count++;
    }


    float widtha = scrollPaneEB.getWidth();
    float heighta = table_height + 30;

    scrollPaneEB.setSize(Math.round(widtha), Math.round(heighta));

    PrinterJob printJob = PrinterJob.getPrinterJob();
    final int finalTable_height = table_height;
    printJob.setPrintable(new Printable() {
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

            if (pageIndex > 0) {
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D paint_PF = (Graphics2D) graphics;
            paint_PF.scale(.68, .6);
            B.print(paint_PF);

            Graphics2D paint_table = (Graphics2D) graphics;
            paint_table.translate(1, 430);
            paint_table.scale(.68, 1);
            scrollPaneB.setVisible(false);
            scrollPaneEB.print(paint_table);

            Graphics2D paint_egmaly = (Graphics2D) graphics;
            paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
            paint_egmaly.scale(1.45, 1);
            totals_panelB.setVisible(false);
            totals_panelB.print(paint_egmaly);


            Graphics2D paint_tawke3 = (Graphics2D) graphics;
            paint_tawke3.translate(0, 200);
            footer_panelB.setVisible(false);
            footer_panelB.print(paint_tawke3);

            return Printable.PAGE_EXISTS;
        }
    });

    PrintRequestAttributeSet printRequestAttrSet = new HashPrintRequestAttributeSet();
    printRequestAttrSet.add(new MediaPrintableArea(0, 0, 350, 500, MediaPrintableArea.MM));
    printRequestAttrSet.add(Chromaticity.MONOCHROME);

    try {
        PrintService ps = findPrintService(String.valueOf(printer1PST_combo.getSelectedItem()));
        printJob.setPrintService(ps);
        printJob.print(printRequestAttrSet);
    } catch (PrinterException ex) {

    }
    adapt_table_size(scrollPaneEB, 440, 351);

}

enter image description here

1 个答案:

答案 0 :(得分:0)

铸造图形对象不会创建新的图形对象。您反复转换了Graphics对象并将其放置在新变量中,但是它仍然是同一Graphics对象,因此您的更改是累积的。

逐步执行代码:

FileConnectionSource

这将导致paint_PF包含已传递给方法的相同打印Graphics对象。它将图形对象称为Graphics2D,但仍然是同一对象。

Graphics2D paint_PF = (Graphics2D) graphics;

现在,原始打印图形对象的尺寸为0.68 x 0.6。

paint_PF.scale(.68, .6);
B.print(paint_PF);

同样,这是相同的Graphics对象,代码只是将其设置为具有0.68 x 0.6的比例。

Graphics2D paint_table = (Graphics2D) graphics;

打印Graphics对象仍然按0.68缩放0.6;现在,您将其进一步缩放 0.68,从而生成一个Graphics对象,该对象实际上已被0.04624缩放为0.6(因为0.68×0.68 = 0.04624)。

paint_table.translate(1, 430);
paint_table.scale(.68, 1);

paint_egmaly仍包含原始打印Graphics对象。

scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);

Graphics2D paint_egmaly = (Graphics2D) graphics;

现在您的打印图形已被缩放,再次缩放,转换和再次缩放。

paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);

paint_tawke3包含原始图形对象,以及所有先前的缩放和平移。

totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);


Graphics2D paint_tawke3 = (Graphics2D) graphics;

现在Graphics对象已被缩放,缩放,转换,缩放和再次转换。

paint_tawke3.translate(0, 200);

对图形进行临时更改的一种方法是使用Graphics.create创建一个新的图形对象,然后将其应用到该对象。如果这样做,则负责通过调用其dispose方法来取消分配Graphics对象。

footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);

使用较少资源的另一个选项是在每次更改后恢复Graphics对象的转换:

Graphics2D paint_PF = (Graphics2D) graphics.create();
paint_PF.scale(.68, .6);
B.print(paint_PF);
paint_PF.dispose();     // DO NOT FORGET THIS

Graphics2D paint_table = (Graphics2D) graphics.create();
paint_table.translate(1, 430);
paint_table.scale(.68, 1);
scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);
paint_table.dispose();  // DO NOT FORGET THIS

Graphics2D paint_egmaly = (Graphics2D) graphics.create();
paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);
totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);
paint_egmaly.dispose(); // DO NOT FORGET THIS


Graphics2D paint_tawke3 = (Graphics2D) graphics.create();
paint_tawke3.translate(0, 200);
footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);
paint_tawke3.dispose(); // DO NOT FORGET THIS