public class Test1{
JTable jTable1;
Test1(){
String columns[] = new String[] {"Name","Quantity","Rate","Amount"};
Object data[][]={
{"PEN","10","5","50"},
{"PENCIL","10","3","30"},
{"KEYBOARD AND MOUSE","10","3","30"}
};
jTable1 = new JTable(new ExampleTableModel(data,columns));
}
public static void main(String h[]){
Test1 t=new Test1();
t.printThisBill();
}
public void printThisBill() {
TableModel mod = jTable1.getModel();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
Date date = new Date();
String Date = dateFormat.format(date);
String Time = timeFormat.format(date);
double tax=0,amt1=0;
String Header =
" ****Super Market**** \n"
+ "Date: "+Date+" Time: "+Time+"\n"
+ "---------------------------------\n"
+ "Name Qty Rate Amt\n"
+ "---------------------------------\n";
String bill = Header;
int i =0;
do{
String name = ""+ mod.getValueAt(i, 0);
String qty = ""+mod.getValueAt(i, 1);
String rate = ""+mod.getValueAt(i, 2);
String amount = ""+mod.getValueAt(i, 3);
if(name.length() > 15){
name = name.substring(0, 15)+" ";
}
rate = rate;
//String items = name+"\t"+qty+"\t"+rate+"\t"+amount+"\n";
String items = name+"\t"+qty+"\t"+rate+"\t"+amount+"\n";
bill = bill+ items;
amt1=Double.parseDouble(amount)+amt1;
i++;
}while(i <= mod.getRowCount()-1);
String amt= "\n \n \nTotal Amount = "+amt1+"\n"
+ "Tax ="+tax+ "\n"
+ "*********************************\n"
+ "Thank you. \n";
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);
try {
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
}
}
}
我正在使用 EPSON TM-81 打印机。我尝试通过上面的 java 代码打印表数据,这给了我一些错误。当我尝试运行此代码时,我收到消息“打印机不接受作业”。如果你有任何问题然后发布