当我调用printtable的print方法时,默认情况下JPanel不以横向模式打印

问题描述 投票:0回答:1

这是我打印面板的功能,默认情况下它以纵向模式打印,每次都需要更改为横向,我从未使用java进行打印,所以我无法理解以下大部分代码

    public static void Print(JPanel component){
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName(" Print Component ");

    pj.setPrintable (new Printable() {    
        public int print(Graphics pg, PageFormat pf, int pageNum){
        if (pageNum > 0){
            return Printable.NO_SUCH_PAGE;
        }
            pf=pj.defaultPage();
            Paper paper = pf.getPaper();
            paper.setSize(8.5 * 72, 14 * 72);
            paper.setImageableArea(0.5 * 72, 0.0 * 72, 8 * 72, 14 * 72);
            pf.setPaper(paper);
            pf.setOrientation(PageFormat.LANDSCAPE);

            Book book = new Book();//java.awt.print.Book
            book.append(this, pf);
            pj.setPageable(book);

            Graphics2D g2 = (Graphics2D) pg;
            g2.translate(pf.getImageableX() + pf.getImageableWidth() / 2 -  component.getWidth() / 2, pf.getImageableY() + pf.getImageableHeight() / 2 - component.getHeight() / 2);
            g2.scale(pf.getImageableWidth()/component.getWidth(), pf.getImageableHeight()/component.getHeight());
            component.paint(g2);
            return Printable.PAGE_EXISTS;
    }
  });

  if (pj.printDialog() == false)
  return;

  try {
        pj.print();
  } catch (PrinterException ex) {
        // handle exception
  }
}

请帮忙,我做错了什么?我尝试过很多不同的东西,但似乎没什么用。

java swing printing jpanel
1个回答
0
投票

嘿,我找到了解决方案,不知道它是如何工作的,但是我将所有纸张,页面格式和书籍参数设置在Print函数和附加书籍之外,并使用新的Printable(传递所有PageFormat和Paper参数)和I得到了我想要的结果。这是可以帮助某人的代码。默认情况下,在Landscae Orientation中在纸上打印Jpanel。

public static void Print(JPanel component){
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName(" Print Component ");

    pj.setPrintable (new Printable() {    
        public int print(Graphics pg, PageFormat pf, int pageNum){
        if (pageNum > 0){
            return Printable.NO_SUCH_PAGE;
        }    
            Graphics2D g2 = (Graphics2D) pg;
            g2.translate(pf.getImageableX() + pf.getImageableWidth() / 2 - component.getWidth() / 2, pf.getImageableY() + pf.getImageableHeight() / 2 - component.getHeight() / 2);
          //g2.scale(pf.getImageableWidth()/component.getWidth(), pf.getImageableHeight()/component.getHeight());
            component.paint(g2);
            return Printable.PAGE_EXISTS;
    }     
  });

            PageFormat pf = pj.defaultPage();
            pf = pj.defaultPage();
            Paper paper = pf.getPaper();
            paper.setSize(8.5 * 72, 14 * 72);
            paper.setImageableArea(0.5 * 72, 0.0 * 72, 8 * 72, 14 * 72);
            pf.setPaper(paper);
            pf.setOrientation(PageFormat.LANDSCAPE);
            Book book = new Book();//java.awt.print.Book
            book.append((new Printable() {    
            public int print(Graphics pg, PageFormat pf, int pageNum){
            if (pageNum > 0){
            return Printable.NO_SUCH_PAGE;
            }    
            Graphics2D g2 = (Graphics2D) pg;
            g2.translate(pf.getImageableX() + pf.getImageableWidth() / 2 - component.getWidth() / 2, pf.getImageableY() + pf.getImageableHeight() / 2 - component.getHeight() / 2);
          //g2.scale(pf.getImageableWidth()/component.getWidth(), pf.getImageableHeight()/component.getHeight());
            component.paint(g2);
            return Printable.PAGE_EXISTS;
    }     
  }), pf);
            pj.setPageable(book); 
            if (pj.printDialog() == false)
            return;

  try {
        pj.print();
  } catch (PrinterException ex) {
        // handle exception
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.