我必須使用Java通過熱敏打印機打印收據。我做了一切。 我的程序從數據庫獲取數據,並使用特殊字符,製表符和\ n轉換爲一個字符串。然後將該字符串傳遞給將其轉換爲圖形的另一種方法。在java中使用熱敏打印機打印收件人
問題是,當我單擊打印按鈕時,出現白紙。我注意到,我的字符串的前4-5個字符被打印在紙張最後端的右角的賬單的最後一行。我的打印機是Epson TM -T81。
public void printThisBill()
{
DefaultTableModel mod = (DefaultTableModel) jTable1.getModel();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
//get current date time with Date()
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 = "+ amt() +"\n"
+ "Tax =" + tax() + "\n"
+ "*********************************\n"
+ "Thank you. \n";
String bill = Header;
int i = 0;
do
{
String name = ""+ mod.getValueAt(i, 2);
String qty = ""+ mod.getValueAt(i, 3);
String rate = ""+ mod.getValueAt(i, 4);
String amount = ""+ mod.getValueAt(i, 6);
if(name.length() > 12)
{
name = name.substring(0, 12)+" ";
}
else
{
for(int j= name.length()-12; j<= name.length(); j++);
{
name = name+" ";
}
}
if(qty.length()<=5)
{
for(int j= 0; j<= qty.length()-5; j++);
{
qty = qty+" ";
}
}
rate = rate;
String items =
name+"\t"+qty+"\t"+rate+"\t"+amount+"\n";
bill = bill+ items;
i++;
} while(i <= mod.getRowCount()-1);
bill = bill+amt;
System.out.println(bill);
printCard(bill);
dispose();
}
而且,打印賬單的方法是:
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());
}
}
這是什麼問題,我該怎麼解決呢?我認爲我沒有在drawString()方法中設置正確的參數。
還是別的東西?任何幫助,我會感激。
你確定你想要的風景? – jmpyle771
「0,0」是不是左下角? (自從我用打印/ g2d廢棄以來,它一直是永遠的 - 是否轉換爲左上角?) –
是的,如果PageFormat處於橫向。 – jmpyle771