Java Code正在打印白页

时间:2016-02-27 15:26:55

标签: java printing

我必须使用热敏打印机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的新手。

1 个答案:

答案 0 :(得分:0)

首先,我想说对不起,有人回答你的问题需要很长时间。现在谈论主题。

您的代码存在一些问题,但它们只出现在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());
    }
    
  3. 为了确保下面提供的代码有效,我测试了它并正确打印。

    这将为您提供类似于此的代码

    <强> 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

    要根据需要排列所有内容,请添加额外的-或更改字体。