gpt4 book ai didi

java - 未找到打印服务错误消息

转载 作者:搜寻专家 更新时间:2023-10-31 20:12:39 26 4
gpt4 key购买 nike

我开始创建一个简单的 Java 应用程序来使用以下代码打印“Hello World”:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;

public class HelloWorldPrinter implements Printable, ActionListener {

public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {

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());

/* Now we perform our rendering */
g.drawString("Hello world!", 100, 100);

/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}

public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
}

public static void main(String args[]) {

UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame("Hello World Printer");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JButton printButton = new JButton("Print Hello World");
printButton.addActionListener(new HelloWorldPrinter());
f.add("Center", printButton);
f.pack();
f.setVisible(true);
}
}

这不起作用,我收到“未找到打印服务”警告消息。

我使用的是 Ubuntu 12.04,Java 版本是 1.7.0_25。

我怎样才能摆脱这个警报?

最佳答案

安装 cups-pdf,它将安装一个创建 PDF 文件的虚拟打印机:

sudo apt-get install cups-pdf

关于java - 未找到打印服务错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17793404/

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