1

我有打印字符串的代码,它在程序本身中传递。在这里,我在打印按钮上调用此代码以获取硬拷贝。现在我想用相同的代码打印一个 JForm,但我不知道如何做到这一点。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 的构造函数,而不是传递字符串。

4

1 回答 1

2

Graphics将当前的绘制JFrame到 a中BufferedImage,然后将图像绘制到打印机的Graphics.

Graphics g = myFrame.getContentPane().getGraphics();
// draw graphics into an image
// draw the image into the printer's graphics

请务必注意,无论何时要打印表单内容,都应始终Graphics从您的JFrame

于 2012-06-09T08:50:47.577 回答