【问题标题】:Unable to print anything from java: [duplicate]无法从 java 打印任何内容:[重复]
【发布时间】:2013-07-08 17:28:58
【问题描述】:

我无法通过我的热敏打印机 (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 = 
                "   ****Sweets Shop****       \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);


      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 pageIndex) throws PrinterException {
       if (pageIndex >0){return NO_SUCH_PAGE;} //Only one page

       Graphics2D g = (Graphics2D) graphics.create(); //Cast to Graphics2D object
                g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area

                    g.drawString (bill, 0, 0); //Print Hello World at offset (100, 100)
                return PAGE_EXISTS; //Page exists (offsets start at zero!)
            }


    };  
    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 printing


【解决方案1】:

尝试改变

g.drawString(bill,0,0); 

到这里

graphics.drawString(bill,0,0);

【讨论】:

  • 这没什么区别! :(
  • 我在我的一个应用程序中做了同样的事情,我和你的代码只有两个不同,你刚刚尝试过的那个我没有在图形上使用 .create()。它只是说 Graphics2D g = (Graphics2D) graphics;除此之外......我很难过大声笑
猜你喜欢
  • 2016-03-02
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
相关资源
最近更新 更多