我试图在一个打印作业中将JTable和JPanel发送到打印机,如下所示:
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
attr.add(OrientationRequested.LANDSCAPE);
MessageFormat header = new MessageFormat("Page {0}");
Printable p1 = table.getPrintable(JTable.PrintMode.FIT_WIDTH, header, null);
PageFormat pf = new PageFormat();
Printable p2 = new ComponentPrinter().returnIt(jPanel1);
Book book = new Book();
book.append(p1, pf);
book.append(p2, pf);
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPageable(book);
if (pj.printDialog(attr)){
try {
pj.print(attr);
} catch (PrinterException ex) {
Exceptions.printStackTrace(ex);
}
}
我的问题是,这只会打印添加到本书中的第一个Printable;在这种情况下,JTable。如果我交换订单并首先添加面板,那么它只打印面板。有人可以指出错误吗?