在Java 7中打印到打印机 [英] In Java 7 Print to a Printer
问题描述
我已经看过每个网站也可以提到我已经在这里待了3个星期
系统是W7 pro cannon mp560打印机
我有一个小应用程序创建一个文本文件我试图读取txt文件并将其打印到打印机。我宁愿从应用程序中读取JFormatedTextFields并将这些
打印到打印机但是如果我无法打印文本文件则后者处于暂停状态
我使用过Visual基本6和HTML5 JQuery,可以从这些打印,但不是java
代码下面是一个带有布局的JFrame上的按钮(null)
< private void =mode =hold>
JPanel pBtmR = new JPanel();
pBtmR.setBounds(630,620,300,120 );
pBtmR.setLayout(新BoxLayout(pBtmR,BoxLayout.Y_AXIS));
JButton btnPrint =新JButton(PRINT);
//btnPrint.setBounds(650,650,70,30);
//btnPrint.setSize(90,30);
btnPrint.setBorder(BorderFactory.createMatteBorder(3,3,3,3,Color.blue));
//btnPrint.setBorder(BorderFactory.createEmptyBorder(10,10,10,10) );
//btnPrint.setBackground(Color.blue);
btnPrint.setMaximumSize(new Dimension(70,30));
btnPrint.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPrint.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet() ;
attributes.add(DialogTypeSelection.NATIVE);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.printDialog(attributes);
// String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService()。getName();
//系统.out.println(默认打印机:+ defaultPrinter);
PrintService服务= PrintServiceLookup.lookupDefaultPrintService();
FileInputStream in = null;
try {
in = new FileInputStream(new file(C:/ Users / Dwight Norris / workspace / ZPzProject / PizzaOrder) 。文本)) ;
//System.out.println(in);
} catch(FileNotFoundException e2){
// TODO自动生成的捕获块
e2.printStackTrace();
}
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(新副本(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(in,flavor,null);
DocPrintJob job = service.createPrintJob();
//系统。 out.println(job);
PrintJobWatcher pjw = new PrintJobWatcher(job);
try {
job.print(doc,pras);
} catch(PrintException e1){
// TODO自动生成的捕获块
e1。 printStackTrace();
}
pjw.waitForDone();
试试{
in.close();
} catch(IOException e1){
// TODO自动生成的catch块
e1.printStackTrace();
}
//发送FF以弹出页面
InputStream ff = new ByteArrayInputStream(\ f.getBytes());
Doc docff = new SimpleDoc(ff,flavor,null);
DocPrintJob jobff = service.createPrintJob();
pjw = new PrintJobWatcher(jobff);
try {
jobff.print(docff,null) ;
System.out.println(我打印+作业);
} catch(PrintException e1){
// TODO自动生成的捕获块
e1.printStackTrace();
}
pjw.waitForDone();
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher (DocPrintJob工作){
job.addPrintJobListener(new PrintJobAdapter(){
public void printJobCanceled(PrintJobEvent pje){
allDone();
}
public void printJobCompleted(PrintJ obEvent pje){
allDone();
}
public void printJobFailed(PrintJobEvent pje){
allDone ();
}
public void printJobNoMoreEvents(PrintJobEvent pje){
allDone();
}
void allDone(){
synchronized(PrintJobWatcher.this){
done = true;
System.out .println(打印完成......);
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone(){
try {
while (!完成){
wait();
}
} catch(InterruptedException e){
}
}
}
});
//打印结束======== ========================>
我看过每个网站从也可以提到我已经在这3个星期
系统是W7 pro cannon mp560打印机
我有一个小应用程序创建一个文本文件我试图读取txt文件并将其打印到打印机。我宁愿从应用程序中读取J格式化文本字段并将这些
打印到打印机,但如果我无法打印文本文件,后者将被暂停
我有使用Visual Basic 6和HTML5 JQuery,可以从这些打印但不是java
我不知道问题在哪里抱歉!
很抱歉代码问题,现在我们有一个看似解决方案的回复
JPanel pBtmR = new JPanel();
pBtmR.setBounds(630,620,300,120);
pBtmR.setLayout(new BoxLayout(pBtmR,BoxLayout.Y_AXIS));
JButton btnPrint = new JButton(PRINT);
//btnPrint.setBounds(650,650,70,30);
//btnPrint.setSize(90,30);
btnPrint.setBorder(BorderFactory.createMatteBorder(3,3,3,3,Color.blue));
//btnPrint.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//btnPrint.setBackground(Color.blue);
btnPrint.setMaximumSize(new Dimension(70,30));
btnPrint.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPrint.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet( );
attributes.add(DialogTypeSelection.NATIVE);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.printDialog(attributes);
// String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService()。getName();
//System.out.println(\"Default printer:+ defaultPrinter);
PrintService service = PrintServiceLookup。 lookupDefaultPrintService();
FileInputStream in = null;
try {
in = new FileInputStream(new File(C:/ Users / Dwight Norris / workspace / ZPzProject / PizzaOrder。 txt));
//System.out.println(in);
} catch(FileNotFoundException e2){
// TODO自动生成的catch块
e2.printStackTrace ();
}
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras .add(新副本(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(in,flavor,null);
DocPrintJob job = service.createPrintJob();
//System.out.println(job);
PrintJobWatcher pjw = new PrintJobWatcher(job);
试试{
job.print(doc,pras);
} catch(PrintException e1){
// TODO自动生成的catch块
e1.printStackTrace();
}
pjw.waitForDone();
试试{
in.close();
} catch(IOException e1){
// TODO自动生成的catch块
e1.printStackTrace();
}
//发送FF以弹出页面
InputStream ff = new ByteArrayInputStream(\ f。getBytes());
Doc docff = new SimpleDoc(ff,flavor,null);
DocPrintJob jobff = service.createPrintJob();
pjw = new PrintJobWatcher(jobff);
try {
jobff.print(docff,null);
System.out.println(我打印+作业);
} catch(PrintException e1){
// TODO自动生成的catch块
e1.printStackTrace();
}
pjw.waitForDone();
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob工作){
job.addPrintJobListener(new PrintJobAdapter(){
public void printJobCanceled(PrintJobEvent pje){
allDone() ;
}
public void printJobCompleted(PrintJobEvent pje){
allDone();
}
public void printJobFailed(PrintJobEvent pje){
allDone() ;
}
public void printJobNoMoreEvents(PrintJobEvent pje){
allDone();
}
void allDone(){
synchronized(PrintJobWatcher.this) {
done = true;
System.out.println(Printing done ...);
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone(){
try {
while(!done){
wait();
}
} catch(InterruptedException e){
}
}
}
});
//打印结束================================>
你看过 http://docs.oracle.com/ javase / tutorial / 2d / printing / [ ^ ]或 http://docs.oracle.com/javase/tutorial/uiswing /misc/printtext.html [ ^ ]
I have seen every web-site from too may places to mention I have been at this for 3 weeks
System is W7 pro cannon mp560 printer
I have a small app that creates a text file I am trying to read the txt file and print it to the printer. I would rather read the JFormatedTextFields from the app and print these
to the printer but if I can not print a text file the latter is on hold
I have worked with Visual Basic 6 and HTML5 JQuery and can print from these but NOT java
code below is behind a button on a JFrame with Layout(null)
<private void="" mode="hold">
JPanel pBtmR = new JPanel();
pBtmR.setBounds(630, 620, 300, 120);
pBtmR.setLayout(new BoxLayout(pBtmR,BoxLayout.Y_AXIS));
JButton btnPrint = new JButton("PRINT");
//btnPrint.setBounds(650, 650, 70, 30);
//btnPrint.setSize(90, 30);
btnPrint.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.blue));
//btnPrint.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//btnPrint.setBackground(Color.blue);
btnPrint.setMaximumSize(new Dimension(70,30));
btnPrint.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(DialogTypeSelection.NATIVE);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.printDialog(attributes);
//String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService().getName();
//System.out.println("Default printer: " + defaultPrinter);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
FileInputStream in = null;
try {
in = new FileInputStream(new File("C:/Users/Dwight Norris/workspace/ZPzProject/PizzaOrder.txt"));
//System.out.println(in);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(in, flavor, null);
DocPrintJob job = service.createPrintJob();
//System.out.println(job);
PrintJobWatcher pjw = new PrintJobWatcher(job);
try {
job.print(doc, pras);
} catch (PrintException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pjw.waitForDone();
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// send FF to eject the page
InputStream ff = new ByteArrayInputStream("\f".getBytes());
Doc docff = new SimpleDoc(ff, flavor, null);
DocPrintJob jobff = service.createPrintJob();
pjw = new PrintJobWatcher(jobff);
try {
jobff.print(docff, null);
System.out.println("did I print "+jobff);
} catch (PrintException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pjw.waitForDone();
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
System.out.println("Printing done ...");
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
});
//END of Print ================================>
I have seen every web-site from too may places to mention I have been at this for 3 weeks
System is W7 pro cannon mp560 printer
I have a small app that creates a text file I am trying to read the txt file and print it to the printer. I would rather read the J Formatted Text Fields from the app and print these
to the printer but if I can not print a text file the latter is on hold
I have worked with Visual Basic 6 and HTML5 JQuery and can print from these but NOT java
I do not know where the issue is sorry !
Sorry about the code issue and now we have a reply that looks like a solution
JPanel pBtmR = new JPanel(); pBtmR.setBounds(630, 620, 300, 120); pBtmR.setLayout(new BoxLayout(pBtmR,BoxLayout.Y_AXIS)); JButton btnPrint = new JButton("PRINT"); //btnPrint.setBounds(650, 650, 70, 30); //btnPrint.setSize(90, 30); btnPrint.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.blue)); //btnPrint.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //btnPrint.setBackground(Color.blue); btnPrint.setMaximumSize(new Dimension(70,30)); btnPrint.setAlignmentX(Component.CENTER_ALIGNMENT); btnPrint.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); attributes.add(DialogTypeSelection.NATIVE); PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.printDialog(attributes); //String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService().getName(); //System.out.println("Default printer: " + defaultPrinter); PrintService service = PrintServiceLookup.lookupDefaultPrintService(); FileInputStream in = null; try { in = new FileInputStream(new File("C:/Users/Dwight Norris/workspace/ZPzProject/PizzaOrder.txt")); //System.out.println(in); } catch (FileNotFoundException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); pras.add(new Copies(1)); DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; Doc doc = new SimpleDoc(in, flavor, null); DocPrintJob job = service.createPrintJob(); //System.out.println(job); PrintJobWatcher pjw = new PrintJobWatcher(job); try { job.print(doc, pras); } catch (PrintException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } pjw.waitForDone(); try { in.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // send FF to eject the page InputStream ff = new ByteArrayInputStream("\f".getBytes()); Doc docff = new SimpleDoc(ff, flavor, null); DocPrintJob jobff = service.createPrintJob(); pjw = new PrintJobWatcher(jobff); try { jobff.print(docff, null); System.out.println("did I print "+jobff); } catch (PrintException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } pjw.waitForDone(); } class PrintJobWatcher { boolean done = false; PrintJobWatcher(DocPrintJob job) { job.addPrintJobListener(new PrintJobAdapter() { public void printJobCanceled(PrintJobEvent pje) { allDone(); } public void printJobCompleted(PrintJobEvent pje) { allDone(); } public void printJobFailed(PrintJobEvent pje) { allDone(); } public void printJobNoMoreEvents(PrintJobEvent pje) { allDone(); } void allDone() { synchronized (PrintJobWatcher.this) { done = true; System.out.println("Printing done ..."); PrintJobWatcher.this.notify(); } } }); } public synchronized void waitForDone() { try { while (!done) { wait(); } } catch (InterruptedException e) { } } } }); //END of Print ================================>
Have you looked at http://docs.oracle.com/javase/tutorial/2d/printing/[^], or http://docs.oracle.com/javase/tutorial/uiswing/misc/printtext.html[^]?
这篇关于在Java 7中打印到打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!