在同一页中打印两个 JPanel
我曾尝试在同一页中打印 2 个 JPanel,每个 jpanel 完全适合半页。
我所做的是将 jpanels 包装在另一个实现 Printable 的 jpanel 中。
问题是,仅正确打印第一个(顶部)jpanel,
页面的其余部分填充有灰色方块
这是Wrapper Jpanel的代码
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException
{
if (pageIndex == 0)
{
Graphics2D g2 = (Graphics2D) graphics;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.scale(.485, .473);
paint(graphics);
g2.setColor(Color.white);
g2.fillRect(0, 0, 1, 1);
return Printable.PAGE_EXISTS;
} else
{
return Printable.NO_SUCH_PAGE;
}
}
这是调用代码
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName("Factura Procter & Gamble");
if (pj.printDialog())
{
try
{
for (int i = 0; i < 1; i++)
{
pj.setPrintable(listaPaneles.get(i));
pj.print();
}
} catch (PrinterException e)
{
System.out.println(e);
}
}
请帮助我,谢谢。
I've been tried to print 2 JPanels in the same page, each jpanel fits perfectly in a half page.
What I do is wrap the jpanels in another jpanel that implements Printable.
The problem is that, only print correctly the first (top) jpanel,
the rest of the page is filled with a gray square
This is the code of the Wrapper Jpanel
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException
{
if (pageIndex == 0)
{
Graphics2D g2 = (Graphics2D) graphics;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.scale(.485, .473);
paint(graphics);
g2.setColor(Color.white);
g2.fillRect(0, 0, 1, 1);
return Printable.PAGE_EXISTS;
} else
{
return Printable.NO_SUCH_PAGE;
}
}
This the calling code
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName("Factura Procter & Gamble");
if (pj.printDialog())
{
try
{
for (int i = 0; i < 1; i++)
{
pj.setPrintable(listaPaneles.get(i));
pj.print();
}
} catch (PrinterException e)
{
System.out.println(e);
}
}
please help me, Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为您不会将它们全部打印出来,
而是
尝试
其次,也许您需要将两个面板包装在另一个
Printable
容器中并仅打印这个容器。Firstly, I think that you are not printing them all
instead of
try
Secondly, maybe you need to wrap your two panels inside another
Printable
container and print this one only.