gpt4 book ai didi

java:如何打印 JForm

转载 作者:行者123 更新时间:2023-11-30 04:45:55 25 4
gpt4 key购买 nike

我有打印字符串的代码,该代码是在程序本身中传递的。在这里,我在“打印”按钮上调用此代码以获取硬拷贝。现在我想用相同的代码打印 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 的构造函数,而不是传递字符串。

最佳答案

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

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

需要注意的是,每当您想要打印表单内容时,都应该从 JFrame 获取新的 Graphics 对象

关于java:如何打印 JForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10959535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com