我正在打印收据,这很棒。在打印jPanel时,是否有直接的方式将jPanel居中?

谢谢您的帮助。

最佳答案

有一种简单的方法。

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

关于java - 如何在打印的页面上居中放置jPanel?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28218569/

10-10 22:57