我是java的新手,所以请帮我解决我的问题。 这是我打印收据账单的代码
public class BillPrintable implements Printable{
public int print(Graphics graphics,PageFormat pageformat,int pageIndex)throws PrinterException
{
int result = NO_SUCH_PAGE;
if(pageIndex == 0)
{
Graphics2D g2d= (Graphics2D) graphics;
double width = pageformat.getImageableWidth();
g2d.translate((int) pageformat.getImageableX(), (int) pageformat.getImageableY());
FontMetrics metrics=g2d.getFontMetrics(new Font("Arial",Font.BOLD,7));
int idLength=metrics.stringWidth("000");
int amtLength=metrics.stringWidth("000000");
int qtyLength=metrics.stringWidth("000000");
int priceLength=metrics.stringWidth("00000");
int prodLength=(int)width-idLength-amtLength-priceLength-17;
int productPosition = 0;
int discountPosition=prodLength+0;
int pricePosition =discountPosition+idLength + 10;
int qtyPosition = pricePosition + priceLength + 4;
int amtPosition = qtyPosition + qtyLength;
try {
int y=20;
int yShift = 10;
int headerRectHeight = 15;
int headerRectHeighta = 40;
g2d.setFont(new Font("Monospaced",Font.PLAIN,9));
g2d.drawString("............................", 12, y);y+=yShift;
g2d.drawString(" Restaurant Bill Reciept ", 12, y);y+=yShift;
g2d.drawString("----------------------------", 12, y);y+=headerRectHeighta;
g2d.drawString("......................................", 10, y);y+=yShift;
g2d.drawString("......................................", 10, y);;y+=yShift;
g2d.drawString(" Free Home Delivery ", 10, y);;y+=yShift;
g2d.drawString(" 9895776277 ", 10, y);;y+=yShift;
g2d.drawString("......................................", 10, y);;y+=yShift;
g2d.drawString(" Thanks To Visit Our Restaurant ", 10, y);;y+=yShift;
g2d.drawString("......................................", 10, y);;y+=yShift;
}
catch(Exception r)
{
r.printStackTrace();
}
result = PAGE_EXISTS;
}
return result;
}
}
这里我得到的错误就像这个The type bill.BillPrintable must implement the inherited abstract method Printable.print(Graphics, PageFormat, int)
。不知道问题是如何产生的以及如何解决问题。请帮助我提前解决
这是我导入的语句。请看看
import java.awt.Font;
import java.awt.Graphics2D;
import javafx.print.Paper;
import com.sun.prism.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.*;
import java.awt.print.*;
import java.awt.print.PageFormat.*;
import java.awt.print.Paper.*;
import javax.swing.UIManager;
import java.awt.FontMetrics;
答案 0 :(得分:4)
您从错误的包中导入类Graphics
:
import com.sun.prism.Graphics
必须是
import java.awt.Graphics
以下导入也是无关紧要的:
import java.text.*;
import java.awt.print.*;
import java.awt.print.PageFormat.*;
import java.awt.print.Paper.*;
注意:您不应该直接使用com.sun
课程
答案 1 :(得分:0)
错误是覆盖从父类继承的方法print(Graphics, PageFormat, int)
。参数应该与父方法相同,或者java将它声明为新方法,而不是覆盖方法。此方法也是静态的,因此您可以调用此方法,而无需实例化类。
答案 2 :(得分:0)
来自Wrong包的图形
请试试这个
import java.awt.Graphics