如何打印单个JPanel的内容? [英] How can I print a single JPanel's contents?
本文介绍了如何打印单个JPanel的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张 JPanel
,带有两个带图片的标签。我需要打印 JPanel
的这些内容。请帮帮我。我怎么才能打印这个 JPanel
的内容,因为我的 JFrame
上也有不同的组件,但我只是需要打印此 JPanel
。
I have a JPanel
with two labels with pictures. I need to print these content of the JPanel
. Please help me out. How can I print only this JPanel
's contents, as I also have different components on my JFrame
but I just need to print this JPanel
.
谢谢。
推荐答案
以下是打印任何Swing组件的示例。
Here is an example to print any Swing component.
public void printComponenet(Component component){
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName(" Print Component ");
pj.setPrintable (new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum){
if (pageNum > 0){
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
component.paint(g2);
return Printable.PAGE_EXISTS;
}
});
if (pj.printDialog() == false)
return;
try {
pj.print();
} catch (PrinterException ex) {
// handle exception
}
}
这篇关于如何打印单个JPanel的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文