我只能打印一页。无法打印第二页或其他页面

时间:2019-05-20 06:59:00

标签: java netbeans

早安,

我正在使用Java中的Netbeans制作我的桌面应用程序。我的问题是我有一个要打印的组件,例如JPanel或textarea。当我打印它时,它仅打印第一页,而不打印第二页或其他页。有人知道吗?提前致谢。下面是我的代码。

public class PrintSection implements Printable,Pageable{


    private Component component;
    private PageFormat format;
    private int numPages;
    private double scaleX,scaleY;
    private int pos;




    public PrintSection(Component component) {
        this.component = component;
        //setUp();
    }

    public void setUp(){

         Dimension totalSpace = this.component.getPreferredSize();

        format = PrinterJob.getPrinterJob().defaultPage();
        numPages = (int) Math.ceil(totalSpace .height/format.getImageableHeight());

    }

    public void print(){

        PrinterJob printJob = PrinterJob.getPrinterJob();

        format = printJob.pageDialog(printJob.defaultPage());
         Paper paper = new Paper();
        double margin = 0; // half inch
        paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
        - margin * 2);
        format.setPaper(paper);
        format.setOrientation(this.pos);
        printJob.setPrintable(this,format);

        Book book = new Book();
        book.append(this,format);
        printJob.setPageable(book);

        if(printJob.printDialog()){
            try {
                printJob.print();
            } catch (PrinterException ex) {
                Logger.getLogger(PrintSection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }





    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {


       if(pageIndex<0 | (pageIndex >=numPages)){
            return Printable.NO_SUCH_PAGE;
        }
        else{


            Graphics2D g = (Graphics2D) graphics;

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


            g.setPaint(Color.ORANGE);
            g.scale(this.scaleX, this.scaleY);
            //g.dispose();


            disableDoubleBuffering(this.component);
            component.paint(graphics);
            enableDoubleBuffering(this.component);



            return Printable.PAGE_EXISTS;
        }
    }

    @Override
    public int getNumberOfPages() {
        return numPages;
    }

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

    @Override
    public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException {
        return this;
    }

    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);
    }

    public void setComponent(Component component) {
        this.component = component;
    }

    public void setScaleX(double scaleX) {
        this.scaleX = scaleX;
    }

    public void setScaleY(double scaleY) {
        this.scaleY = scaleY;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }




}

0 个答案:

没有答案