`
chun521521
  • 浏览: 276613 次
  • 性别: Icon_minigender_1
  • 来自: 长春
社区版块
存档分类
最新评论

java调用打印机

    博客分类:
  • java
阅读更多

 

网上转的,我自己测试着玩的。

 

package com.chengyi.common;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import usb.core.*;

public class Print extends JFrame implements ActionListener, Runnable {

 private static final long serialVersionUID = 2264172608765466416L;
 
 Thread readThread;
 JScrollPane p1;
 JTextField Text = new JTextField(10);
 JButton ok = new JButton("打印");
 JButton zou = new JButton("走纸");

 JTextArea area = new JTextArea(15, 10);
 JLabel l1 = new JLabel("Message:");

 JLabel l2 = new JLabel("         序号:");
 JLabel l3 = new JLabel("店号:");

 JTextField txtTitle = new JTextField(10);
 JTextField txtPhoNo = new JTextField(10);

 JLabel l4 = new JLabel("货号");
 JLabel l5 = new JLabel("商品");
 JLabel l6 = new JLabel("数量");
 JLabel l7 = new JLabel("金额");

 JTextField txtHouNo = new JTextField(10);
 JTextField txtHouName = new JTextField(10);
 JTextField txtNum = new JTextField(10);
 JTextField txtKin = new JTextField(10);

 JLabel l8 = new JLabel("共计:");
 JLabel l9 = new JLabel("件商品");
 JTextField txtGongji = new JTextField(10);

 JLabel l10 = new JLabel("现金:");
 JTextField txtXianjin = new JTextField(10);

 JLabel l11 = new JLabel("-------------------------------------------" + "-----------------------------------------------------");
 JLabel l12 = new JLabel("--------------------------------------------" + "----------------------------------------------------");

 JLabel l13 = new JLabel("--------------------------------------------" + "----------------------------------------------------");

 Print() {
  setTitle("POS测试");
  setVisible(true);
  setLayout(null);
  p1 = new JScrollPane(area);
  add(l1);
  l1.setBounds(35, 30, 100, 25);
  add(Text);
  Text.setBounds(100, 30, 150, 25);
  add(ok);

  ok.setBounds(270, 30, 80, 23);
  add(zou);
  zou.setBounds(370, 30, 80, 23);

  add(l11);
  l11.setBounds(35, 50, 400, 20);

  add(l2);
  l2.setBounds(35, 70, 100, 25);
  add(txtTitle);
  txtTitle.setBounds(100, 70, 100, 25);

  add(l3);
  l3.setBounds(250, 70, 50, 25);
  add(txtPhoNo);
  txtPhoNo.setBounds(300, 70, 100, 25);

  txtTitle.setText("002");
  txtPhoNo.setText("001");

  add(l4);
  l4.setBounds(35, 100, 50, 15);
  add(l5);
  l5.setBounds(150, 100, 100, 15);
  add(l6);
  l6.setBounds(275, 100, 50, 15);
  add(l7);
  l7.setBounds(400, 100, 50, 15);

  add(txtHouNo);
  txtHouNo.setBounds(35, 115, 50, 25);
  add(txtHouName);
  txtHouName.setBounds(150, 115, 100, 25);
  add(txtNum);
  txtNum.setBounds(275, 115, 50, 25);
  add(txtKin);
  txtKin.setBounds(400, 115, 50, 25);

  txtHouNo.setText("N2105");
  txtHouName.setText("还魄丹");
  txtNum.setText("1");
  txtKin.setText("0.50");

  add(l12);
  l12.setBounds(35, 140, 400, 20);

  add(l8);
  l8.setBounds(35, 165, 100, 25);
  add(txtGongji);
  txtGongji.setBounds(85, 165, 50, 25);
  add(l9);
  l9.setBounds(150, 165, 100, 25);

  add(l10);
  l10.setBounds(35, 190, 100, 25);
  add(txtXianjin);
  txtXianjin.setBounds(85, 190, 50, 25);
  txtGongji.setText("1");
  txtXianjin.setText("0.50");

  add(l13);
  l13.setBounds(35, 215, 400, 20);

  ok.addActionListener(this);
  zou.addActionListener(this);

  setSize(500, 500);
  setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  setResizable(false);
  setVisible(true);
  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  setLocation((screen.width - 400) / 2, (screen.height - 400) / 2);
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(1);
   }
  });
 }

 public void run() {
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
  }
 }

 public static void main(String[] args) {
  try {
   Host host = HostFactory.getHost();
   Bus[] bus = host.getBusses();
   int total_bus = bus.length;
   for (int i = 0; i < total_bus; i++) {
    Device root = bus[i].getRootHub();
    int total_port = root.getNumPorts();
    for (int j = 1; j <= total_port; j++) {
     Device device = root.getChild(j);
     if (device != null) {
      // USB操作
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }

  
  //new Print();
 }

 public void actionPerformed(ActionEvent e) {
  if (e.getSource() == ok) {

   try {
    FileWriter fw = new FileWriter("LPT1:");
    if (fw != null) {
     Text.setText("连接打印机成功! ");

     PrintWriter pw = new PrintWriter(fw);

     pw.print("\r\n");
     pw.print("      山 高 路 远 大 药 房");

     pw.print("\r\n");
     pw.print("------------------------------");
     pw.print("\r\n");

     pw.print("        序号:" + txtTitle.getText() + "   店号: " + txtPhoNo.getText());
     pw.print("\r\n");
     pw.print("货号    商品    数量    金额  ");
     pw.print("\r\n");
     pw.print("" + txtHouNo.getText() + "   " + txtHouName.getText() + "    " + txtNum.getText() + "    " + txtKin.getText());

     pw.print("\r\n");
     pw.print("共计:" + txtGongji.getText() + " 件商品 ");
     pw.print("\r\n");
     pw.print("现金:" + txtXianjin.getText());
     pw.print("\r\n");
     pw.print("------------------------------");
     pw.print("\r\n");

     setBondD(pw);
     pw.print("      欢 迎 再 次 光 临");
     pw.write(0x1c);
     pw.write(0x21);
     pw.write(0);
     pw.print("\r\n");
     pw.print("\r\n");
     pw.print("\r\n");
     pw.close();
    }

   } catch (IOException e1) {
    e1.printStackTrace();
    Text.setText("连接打印机失败!");
   }

  } else if (e.getSource() == zou) {
   try {
    FileWriter fw = new FileWriter("LPT1:");
    if (fw != null) {
     Text.setText("连接打印机成功! ");
     PrintWriter pw = new PrintWriter(fw);
     pw.print("\r\n");
     pw.print("\r\n");
     pw.close();
    }

   } catch (IOException e1) {
    e1.printStackTrace();
    Text.setText("连接打印机失败!");
   }
  }

 }

 public static void setBondD(PrintWriter pr) {
  pr.write(0x1c);
  pr.write(0x21);
  pr.write(8);
 }

 public static void setBondX(PrintWriter pr) {
  pr.write(0x1c);
  pr.write(0x21);
  pr.write(0);
 }

}

分享到:
评论
2 楼 woadaia 2015-01-26  
jar 要哪些子啊,能不能直接发我一份,qq 394305323 或者QQ邮箱
1 楼 deyangliningl 2011-12-29  

相关推荐

Global site tag (gtag.js) - Google Analytics