我用下面的代码在netbeans中打印了一个JFrame,但它不起作用

时间:2019-03-01 06:27:26

标签: java netbeans printing

您可以直接打印JFrame / JPanel的图形,而不必生成Doc。

此代码应该有效:

          PrinterJob pj = PrinterJob.getPrinterJob();
          pj.setJobName("name");
            PageFormat format = pj.getPageFormat(null);
            pj.setPrintable (new Printable() {
            @Override
            public int print(Graphics pg, PageFormat pf, int pageNum) throws PrinterException {
                if (pageNum > 0){
                      return Printable.NO_SUCH_PAGE;
                      }

                      Graphics2D g2 = (Graphics2D) pg;

                      this.paint(g2);
                      return Printable.PAGE_EXISTS;
            }

          }, format);
          if (pj.printDialog() == false)
          return;
          pj.print();
          } catch (PrinterException ex) {
                // handle exception
          }
        }

0 个答案:

没有答案