gpt4 book ai didi

java - 为什么我的 Java 打印到纸张作业永远不会进入打印机队列?

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:35 25 4
gpt4 key购买 nike

有很多问题涉及打印机队列中的 PrinterJobs 而不是打印,但我还没有找到有关打印作业永远不会进入队列的讨论。

我的测试程序(参见下面的代码 - 两个类 PrintIt(可打印)和 Report(带有 main 的 jFrame)似乎运行良好,没有异常,但从未将任何内容放入打印队列中。

在代码之后,我将添加正在运行的程序的屏幕截图。

<小时/>

package SVDP;

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Paper;

/**
*
* @author Wesley Stupar
*/
public class Report extends javax.swing.JFrame {

static Graphics g;
static Paper pap;
static PageFormat pf;
static Report report;

/**
* Creates new form Report
*/
public Report() {
initComponents();
g = this.getGraphics();
pap = new Paper();
pap.setImageableArea(36, 36, 540, 720); //1/2" margins, 8.5x11
pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
pf.setPaper(pap);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
report = new Report();
report.setVisible(true);
}
});
PrintIt printer = new PrintIt();
if(g == null){
System.out.println("Graphics context is null.");
System.exit(0);
}
report.generate();
try {
System.out.println("Calling print");
printer.print(g, pf, 0);
} catch (Exception pe) {
System.out.println("Printer exception: "+pe);
}
// report.setVisible(false);
}

/**
* Generate the page to be printed
*/
public void generate(){
g.drawString("Hello printer!", 100, 100);
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
--------------------------------------------------------------------
package SVDP;

import java.awt.print.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import javax.print.PrintService;

/**
*
* @author Wesley Stupar
*/
public class PrintIt implements Printable {

PrinterJob job;

public PrintIt() {
job = PrinterJob.getPrinterJob();
if (job.printDialog()) {
try {
job.print();
} catch (Exception PrintException) {
System.out.println("PrintIt exception: "+PrintException);
}
}
}

@Override
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
job.setPrintable(this);

if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}

/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());

/* tell the caller that this page is part of the printed document */
System.out.println("Finished print");
return PAGE_EXISTS;
}

我不允许发布图像,但我将描述在 NetBeans 下运行的程序所显示的内容:

  1. jFrame 与打印对话框重叠显示。所选打印机能够打印测试页。然后对话框就OK了。 NetBeans 输出打印以下内容:运行:调用打印打印完成构建成功(总时间:12 秒)

程序继续运行,但没有任何内容进入打印队列。该程序必须手动终止。

问题结束。

最佳答案

After looking at this very helpful post:

我发现我试图在可打印之外渲染图像,并且没有正确实例化可打印。

关于java - 为什么我的 Java 打印到纸张作业永远不会进入打印机队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27786820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com