我想打印一个包含大量文本字段和包含数据的按钮的jframe.我想通过单击打印jbutton打印此jframe.我试过这段代码.
protected void print() {
PrinterJob job = PrinterJob.getPrinterJob();
if(job.printDialog()){
try {
job.setPrintable(new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if(pageIndex == 0){
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
Dimension dim = ODietListJFrame.this.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = pageFormat.getImageableHeight();
double pWidth = pageFormat.getImageableWidth();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
g2d.scale(xRatio, yRatio);
ODietListJFrame.this.printAll(g2d);
return PAGE_EXISTS;
}
return NO_SUCH_PAGE;
}
});
job.print();
} catch (PrinterException e) {
e.printStackTrace();
}
} else {
System.err.println("Error printing");
}
}
我的问题是我的jframe太大了,必须打印两页,这段代码打印第二页.我想从jframe打印第一部分.
感谢帮助.
解决方法:
因此,我们需要做的第一件事是确保打印的组件符合它的首选尺寸……
component.setSize(component.getPreferredSize());
这很重要,但请记住,这会影响屏幕上的组件……
接下