首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java:如何打印JForm

java:如何打印JForm
EN

Stack Overflow用户
提问于 2012-06-09 16:31:22
回答 1查看 628关注 0票数 1

我让代码打印一个字符串,它被传递到程序本身。在这里,我调用打印按钮上的代码来获得硬拷贝。具有用户详细信息的一些标签和文本字段的JForm。这是我打印字符串“Hello World”的代码。

代码语言:javascript
复制
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的构造函数,而不是传递字符串。

EN

回答 1

Stack Overflow用户

发布于 2012-06-09 16:50:48

将当前JFrameGraphics绘制到BufferedImage中,然后将图像绘制到打印机的Graphics中。

代码语言:javascript
复制
Graphics g = myFrame.getContentPane().getGraphics();
// draw graphics into an image
// draw the image into the printer's graphics

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10959535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档