java如何指定打印机打印

ytl13966 2017-08-15 06:00:51
各位大佬帮忙给看一下我要实现这样一个功能:
如图将我输入的文字替换ZPL命令中的指定字符串,然后将字符串输出给打印机,我现在的代码是点击打印之后会跳出选择打印机的界面,我如何才能跳过这一步,将字符串直接发给指定的打印机:上图:
上码:
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

public class mySwin extends JFrame {
String s=null;
String str=null;
String str1=null;
String str2=null;
String str3=null;
String str4=null;
String str5=null;
String str6=null;
String str7=null;
String str8=null;

private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mySwin frame = new mySwin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public mySwin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(400, 250, 600, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JPanel panel = new JPanel();
panel.setBounds(10, 10, 564, 271);
contentPane.add(panel);
panel.setLayout(null);

JLabel label = new JLabel("产品型号:");
label.setBounds(34, 10, 70, 15);
panel.add(label);

textField = new JTextField();
textField.setBounds(96, 10, 290, 21);
panel.add(textField);
textField.setColumns(10);

JLabel label_1 = new JLabel("软件版本:");
label_1.setBounds(34, 44, 70, 15);
panel.add(label_1);

textField_1 = new JTextField();
textField_1.setBounds(96, 41, 290, 21);
panel.add(textField_1);
textField_1.setColumns(10);

JLabel label_2 = new JLabel("批次号:");
label_2.setBounds(34, 75, 54, 15);
panel.add(label_2);

textField_2 = new JTextField();
textField_2.setBounds(96, 72, 119, 21);
panel.add(textField_2);
textField_2.setColumns(10);

JLabel label_3 = new JLabel("线体:");
label_3.setBounds(242, 75, 54, 15);
panel.add(label_3);

textField_3 = new JTextField();
textField_3.setBounds(284, 72, 102, 21);
panel.add(textField_3);
textField_3.setColumns(10);

JLabel lblPmc = new JLabel("PMC:");
lblPmc.setBounds(34, 106, 54, 15);
panel.add(lblPmc);

textField_4 = new JTextField();
textField_4.setBounds(96, 103, 119, 21);
panel.add(textField_4);
textField_4.setColumns(10);

JLabel label_4 = new JLabel("数量:");
label_4.setBounds(242, 106, 54, 15);
panel.add(label_4);

textField_5 = new JTextField(0);
textField_5.setBounds(284, 103, 66, 21);
panel.add(textField_5);
textField_5.setColumns(10);

JLabel label_5 = new JLabel("序号:");
label_5.setBounds(34, 140, 54, 15);
panel.add(label_5);

textField_6 = new JTextField();
textField_6.setBounds(96, 137, 66, 21);
panel.add(textField_6);
textField_6.setColumns(10);

JLabel label_6 = new JLabel("备注:");
label_6.setBounds(34, 181, 54, 15);
panel.add(label_6);

textField_7 = new JTextField();
textField_7.setBounds(96, 162, 440, 53);
panel.add(textField_7);
textField_7.setColumns(10);


JLabel label_7 = new JLabel("日期:");
label_7.setBounds(34, 233, 54, 15);
panel.add(label_7);

textField_8 = new JTextField();
textField_8.setBounds(96, 230, 127, 21);
panel.add(textField_8);
textField_8.setColumns(10);

JButton btnNewButton = new JButton("打印");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

File f=new File( "d:/123.prn");
File f1=new File("d:/234.prn");
//创建文件
FileInputStream inf=null;
FileOutputStream outf=null;
try {
outf=new FileOutputStream(f1);
inf=new FileInputStream(f);
byte []bytes=new byte[1024];
int n=0;
while((n=inf.read(bytes))!=-1){
s=new String(bytes,0,n);

str=s.replace("ABC1234",textField.getText() );
str1=str.replace("DEF567", textField_1.getText());
str2=str1.replace("GHI890", textField_2.getText());
str3=str2.replace("JKL121", textField_3.getText());
str4=str3.replace("MNO235", textField_4.getText());
str5=str4.replace("PQR147", textField_5.getText());
str6=str5.replace("STU258", textField_6.getText());
str7=str6.replace("ZCV3159", textField_7.getText());
str8=str7.replace("ZW369", textField_8.getText());
System.out.println(str8);



outf.write(str8.getBytes());

}


} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
MBFrame mb=new MBFrame();

}finally{
try {
inf.close();
outf.close();
} catch (IOException e2) {
// TODO 自动生成的 catch 块
e2.printStackTrace();
}
}






try{ int a=Integer.parseInt( textField_6.getText());

if(a==0){
XhFrame xh=new XhFrame();
}
else{DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService []pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200, pservices,
defaultService, flavor, aset);
if(service != null){
try {
DocPrintJob pj =service.createPrintJob();
FileInputStream fis = new FileInputStream("D:"+ File.separator +"234.prn");//打印D盘zhidao.txt文档。
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
pj.print(doc, aset);
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (PrintException ee) {
ee.printStackTrace();
}
}else {
System.out.println("打印失败");
}

textField_6.setText(a+1+"");}
}catch(Exception a){
XhFrame xh=new XhFrame();
}




}}


);

btnNewButton.setBounds(442, 317, 93, 35);
contentPane.add(btnNewButton);

JButton btnNewButton_1 = new JButton("补打");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_1.setBounds(302, 317, 93, 35);
contentPane.add(btnNewButton_1);

}
}
class XhFrame extends JFrame{

public XhFrame(){
JPanel pl=new JPanel();
JLabel lab_7 = new JLabel("序号不能为空或0");
pl.add(lab_7);
this.setTitle("提示");
this.setSize(300,150);
this.setLocation(550, 400);
this.add(pl);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
}
class MBFrame extends JFrame{

public MBFrame(){
JPanel pl=new JPanel();
JLabel lab_7 = new JLabel("没有找到模板");
pl.add(lab_7);
this.setTitle("提示");
this.setSize(300,150);
this.setLocation(550, 400);
this.add(pl);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
}
标红的部分就是需要修改的地方,请大牛帮忙;
...全文
444 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ytl13966 2017-08-15
  • 打赏
  • 举报
回复
100分都没有人要么???
ytl13966 2017-08-15
  • 打赏
  • 举报
回复
求关注呀,没有人知道么?
ytl13966 2017-08-15
  • 打赏
  • 举报
回复
自己顶顶1111111111111

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧