gpt4 book ai didi

Java代码正在打印白页

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:41 26 4
gpt4 key购买 nike

我必须使用热敏打印机 Sewoo SLK-TS400 打印收据。我正在制作一个字符串并想用打印机打印它。但它正在打印白页。我已经搜索了很多东西,但什么也没发生。

账单制作方法

package printprogram;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.table.DefaultTableModel;

public class MakingBill {
public static void main(String[] args)
{

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
Date date = new Date();
Date time = new Date();
String Date = dateFormat.format(date);
String Time = timeFormat.format(time);
String Header =
" ****Super Market**** \n"
+ "Date: "+Date+" Time: "+Time+"\n"
+ "---------------------------------\n"
+ "Name Qty Rate Amt\n"
+ "---------------------------------\n";

String amt =
"\n \n \nTotal Amount = " +"\n"
+ "Tax =" + "\n"
+ "*********************************\n"
+ "Thank you. \n";

String bill = Header;
System.out.println(bill);

Main mn = new Main();
mn.printCard(bill);
}
}

以及打印账单的类

package printprogram;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class Main {

public static void printCard(final String bill) {
Printable contentToPrint = new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Graphics2D g2d = (Graphics2D) graphics.create();

g2d.setPaint(Color.black);
g2d.setFont(new Font("Arial", Font.BOLD, 10));
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableX());

g2d.drawString(bill, 0, 0);

return PAGE_EXISTS;
}
};

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(contentToPrint);
//You can show a print dialog before printing by job by wrapping the following blocks with a conditional statement if(job.printDialog()){...}
try {
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
}

}
}

请帮忙。我是java新手。

最佳答案

首先我想说抱歉,这么长时间才有人回答您的问题。现在进入主题。

您的代码存在一些问题,但它们仅出现在 Main.java

  1. 如果您有代码 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableX());,则调用 getImageableX() 两次,而不是 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

  2. 在尝试绘制字符串 bill 时,您似乎没有意识到 drawString() 无法处理新行 \n。要解决此问题,您可以使用 FontMetrics 。例如:

    private void drawString(Graphics g, String text, int x, int y) {
    for (String line : text.split("\n"))
    g.drawString(line, x, y += g.getFontMetrics().getHeight());
    }

为了确保下面提供的代码有效,我对其进行了测试并且可以正确打印。

<小时/>

这会给你留下如下所示的代码

Main.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.HeadlessException;
import java.awt.print.PrinterJob;

public class Main {
private void drawString(Graphics g, String text, int x, int y) {
for (String line : text.split("\n"))
g.drawString(line, x, y += g.getFontMetrics().getHeight());
}

public Main(String bill) {
Printable contentToPrint = new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Graphics2D g2d = (Graphics2D)graphics;

g2d.setPaint(Color.black);
g2d.setFont(new Font("Arial", Font.BOLD, 10));
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

drawString(g2d, bill, 0, 0);

return PAGE_EXISTS;
}
};

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(contentToPrint);
try {
job.printDialog();
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
} catch (HeadlessException e) {
System.err.println(e.getMessage());
}
}
}

MakingBill.java

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.table.DefaultTableModel;

public class MakingBill {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
Date date = new Date();
Date time = new Date();
String Date = dateFormat.format(date);
String Time = timeFormat.format(time);
String Header =
" ****Super Market**** \n"
+ "Date: "+Date+" Time: "+Time+"\n"
+ "---------------------------------\n"
+ "Name Qty Rate Amt\n"
+ "---------------------------------\n";

String amt =
"\n \n \nTotal Amount = " +"\n"
+ "Tax =" + "\n"
+ "*********************************\n"
+ "Thank you. \n";

String bill = Header;
System.out.println(bill);

Main mn = new Main(bill);
}
}

这会产生如下所示的输出:

Output

要使所有内容按照您的意愿排列,请添加额外的 - 或更改字体。

关于Java代码正在打印白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35671565/

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