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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当我调用printtable的print方法时,默认情况下JPanel不以横向模式打印相关的知识,希望对你有一定的参考价值。

这是我打印面板的功能,默认情况下它以纵向模式打印,每次都需要更改为横向,我从未使用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
  }
}

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

答案

嘿,我找到了解决方案,不知道它是如何工作的,但是我将所有纸张,页面格式和书籍参数设置在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
  }
}

以上是关于当我调用printtable的print方法时,默认情况下JPanel不以横向模式打印的主要内容,如果未能解决你的问题,请参考以下文章

当我打开另一个面板时,会调用 <mat-expansion-panel> 上的函数(关闭)

试图调用方法'applyForce'

Python:模块引用

犰狳的 print() 方法和 cout 从 Rcpp 调用时的顺序不一致

静态方法 - 如何从另一个方法调用一个方法?

哈希表打印出默认值,但将项目添加到地址边界错误时