您可以直接打印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
}
}