简体   繁体   English

如何在打印的页面上居中放置jPanel?

[英]How do I center a jPanel on a page that is printed?

I am getting my receipt to print, which is great. 我正在打印收据,这很棒。 Is there a straight-forward way of centering the jPanel on the page when it is printed? 在打印jPanel时,是否有直接的方式将jPanel居中?

Thank you for your help. 谢谢您的帮助。

There's a semi-straightforward way. 有一种简单的方法。

public int print(Graphics g, PageFormat pf, int pageIndex) {
  Component myPanel =...;
  g.translate(pf.getImageableX() + pf.getImageableWidth() / 2 - myPanel.getWidth() / 2, pf.getImageableY() + pf.getImageableHeight() / 2 - myPanel.getHeight() / 2);
  //Graphics object has been set up so the printing myPanel with it should now be centered
  myPanel.print(g);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM