gpt4 book ai didi

java - 打印小程序 GUI

转载 作者:行者123 更新时间:2023-12-01 04:29:37 30 4
gpt4 key购买 nike

我正在尝试打印与小程序 GUI 中相同的内容。

问题是,当我打印它时,它并不完全相同......每个组件的大小以不成比例的方式变化。

图形用户界面:

enter image description here

打印:

enter image description here

边距不是问题,我可以轻松修复它们...但是正如您所看到的条形码和垂直文本的大小不正确。

主类:

    public class Impresion extends Applet{
PrinterJob job = PrinterJob.getPrinterJob();
Panel test;
Label test2;
Label test3;
String copyParam = null;
String dialogParam = null;
String codeParam = null;
String descParam = null;
public void init(){
copyParam = this.getParameter("copias");
dialogParam = this.getParameter("ventanita");
codeParam = this.getParameter("codigo");
descParam = this.getParameter("descripcion");
copyParam = "1";
dialogParam = "1";
codeParam = "002/113";
descParam = "asd";

if (copyParam == null || dialogParam == null || codeParam == null || descParam == null){
System.out.println("Faltan parametros. - "+copyParam+" - "+dialogParam+" - "+codeParam+" - "+descParam);
} else {
job.setPrintable(new Dibujar(codeParam, descParam));
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
float leftMargin = (float) 0.1;
float topMargin = (float) 0.1;
float rightMargin = (float) 0.1;
float bottomMargin = (float) 0.1;
float mediaWidth = (float) 60.0;
float mediaHeight = (float) 50.0;
aset.add(new MediaPrintableArea(leftMargin, topMargin, (mediaWidth - leftMargin - rightMargin), (mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));
int copias = Integer.parseInt(copyParam);
aset.add(new Copies(copias));
boolean imprimir = true;
if (dialogParam.indexOf('1') != -1){
imprimir = job.printDialog(aset);
}
if (imprimir){
try {
job.print(aset);
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
}
}

可打印类:

    public class Dibujar implements Printable{
String codeParam = null;
String descParam = null;
public Dibujar(String code, String desc) {
codeParam = code;
descParam = desc;
}

public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException{
if (pageIndex > 0){
return NO_SUCH_PAGE;
}
Barcode b = null;
try {
b = BarcodeFactory.createCodabar(codeParam);
b.setDrawingText(false);
b.setBarWidth(1);
b.draw((Graphics2D) g, 30, 8);
} catch (BarcodeException | OutputException e1) {
e1.printStackTrace();
}
g.setFont(new Font("Arial", Font.BOLD, 9));
String description = descParam;
g.drawString(description, 16, 70);
AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(90),16,8);
Graphics2D g2 = (Graphics2D) g;
g2.transform(at);
g.setFont(new Font("Arial", Font.BOLD, 14));
g2.drawString(codeParam.replace("/", ""),16,8);
g2.transform(new AffineTransform());
return PAGE_EXISTS;
}
}

最佳答案

调整这里的参数:

aset.add(new MediaPrintableArea(leftMargin, topMargin, (mediaWidth - leftMargin - rightMargin), (mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));

或者调整这两个参数:

b.draw((Graphics2D) g, 30, 8);
//....
g2.drawString(codeParam.replace("/", ""),16,8);

关于java - 打印小程序 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18104941/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com