gpt4 book ai didi

java - 在java中打印jframe

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

如何打印JFrame的内容?

最佳答案

     PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok)
{
try
{
job.print();
}
catch (PrinterException ex)
{
/* The job did not successfully complete */
}
}

public int print(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 print the window and its visible contents */
jFrame.paint(g2d);
jFrame.repaint();
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}

关于java - 在java中打印jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314505/

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