我让代码打印一个字符串,它被传递到程序本身。在这里,我调用打印按钮上的代码来获得硬拷贝。具有用户详细信息的一些标签和文本字段的JForm。这是我打印字符串“Hello World”的代码。
public class PrintClass implements Printable, ActionListener {
public int display(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
g.drawString("Hello World", 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}请帮我调用JForm的构造函数,而不是传递字符串。
发布于 2012-06-09 16:50:48
将当前JFrame的Graphics绘制到BufferedImage中,然后将图像绘制到打印机的Graphics中。
Graphics g = myFrame.getContentPane().getGraphics();
// draw graphics into an image
// draw the image into the printer's graphics请务必注意,无论何时要打印表单内容,都应始终从JFrame获取新的Graphics对象
https://stackoverflow.com/questions/10959535
复制相似问题