我想通过热敏打印机打印我的jpanel。但是当我打印面板时,输出的字体与面板字体不匹配。面板有JLabel和图像。我已经发送了下面的代码。如果你能提供帮助,我将非常感激!
public static class Printer implements Printable {
final Component comp;
public Printer(Component comp) {
this.comp = comp;
}
@Override
public int print(Graphics g, PageFormat format, int page_index)
throws PrinterException {
if (page_index > 0) {
return Printable.NO_SUCH_PAGE;
}
// get the bounds of the component
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();
double pXStart = format.getImageableX();
double pYStart = format.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);
return Printable.PAGE_EXISTS;
}
}
答案 0 :(得分:1)
您必须为所有子组件设置所需的字体。将字体设置为所有标签,因为它们不使用父字体。