简体   繁体   中英

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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