我有一个实现可打印的类。在这个类中,我有一个公共方法,它创建一个BufferedImages列表,每个列表都打印在一个页面上。现在我想添加一个PrintDialog,允许用户选择要打印的页面和打印的拷贝数。
我在互联网上做了一些研究,发现我可能不得不使用Book类,但我不知道如何在我的情况下使用它。
有人能举个例子吗?谢谢你的帮助..。
好的。下面是我使用的代码:
/**
* Starts the print job
* Allows variable scaling
*/
public void startPrint(float scale, JTable rowHeader, JTable mainTable, boolean includeRowHeaders, boolean includeColumnHeaders) throws PrinterException{
//getPages returns a List<BufferedImage
this.pages = getPages(scale, rowHeader, mainTable, includeRowHeaders, includeColumnHeaders);
this.numberOfPages = this.pages.size();
HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
attr.add(new MediaPrintableArea(0f, 0f, 612/72f, 792/72f, MediaPrintableArea.INCH));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
job.print(attr);
}编辑 Ok。我已经取得了一些进展。新问题来了。我现在可以选择页面范围,但是PrintDialog复制JSpinner没有响应性,而且无论JSpinner文本字段包含什么,每次页面都会调用两次JSpinner方法。这是我的密码。
/**
* Starts the print job
* Allows variable scaling
*/
public void startPrint(float scale, JTable rowHeader, JTable mainTable, boolean includeRowHeaders, boolean includeColumnHeaders) throws PrinterException{
//getPages returns a List<BufferedImage>
this.pages = getPages(scale, rowHeader, mainTable, includeRowHeaders, includeColumnHeaders);
this.numberOfPages = this.pages.size();
HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
attr.add(new JobName("Test Print", null));
attr.add(new MediaPrintableArea(0f, 0f, 612/72f, 792/72f, MediaPrintableArea.INCH));
attr.add(new PageRanges(1, this.numberOfPages));
attr.add(new Copies(1));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
if(job.printDialog(attr)){
job.print(attr);
}
}
/**
* The actual print routine
* Prints the class level List<BufferedImage>
* one after another
*/
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
if (pageIndex > this.numberOfPages - 1){
return NO_SUCH_PAGE;
}
else{
graphics.drawImage(this.pages.get(pageIndex), 0, 0, null);
JOptionPane.showMessageDialog(null, pageIndex);
return PAGE_EXISTS;
}
}发布于 2016-10-23 07:51:52
我在编辑的问题上弄错了。我在Microsoft文档编写器上进行测试。当我把这个项目转移到另一个电脑上,用一个真正的物理打印机,它工作的很好.
https://stackoverflow.com/questions/40198644
复制相似问题