我在java打印时遇到这个奇怪的错误。
2014-07-20 13:04:58.800 java[53716:d07] Cocoa AWT: Running on AppKit thread 0 when not expected. (
0 libawt_lwawt.dylib 0x00000001a6fefd0b nsPrintInfoToJavaPageFormat + 50
1 libawt_lwawt.dylib 0x00000001a6ff0500 Java_sun_lwawt_macosx_CPrinterJob_getDefaultPage + 51
2 ??? 0x000000010896fb7e 0x0 + 4439079806
3 ??? 0x000000010895b360 0x0 + 4438995808
4 ??? 0x000000010895b290 0x0 + 4438995600
)
我尝试使用此代码打印一个简单的页面:
PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new IntroPage(), printJob.defaultPage());
PageFormat documentPageFormat = new PageFormat();
documentPageFormat.setOrientation(PageFormat.PORTRAIT);
printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
问题是这一行book.append(new IntroPage(), printJob.defaultPage());
正在产生错误,IntroPage()正在实现Printable
并高估了print
方法。这是代码:
public class IntroPage implements Printable {
public int print(Graphics g, PageFormat pageFormat, int page) {
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setPaint(Color.black);
Font titleFont = new Font("helvetica", Font.PLAIN, 8);
g2d.setFont(titleFont);
FontMetrics fontMetrics = g2d.getFontMetrics();
g2d.drawString("Hello World!", 5, 10);
return (PAGE_EXISTS);
}
}
我正在使用:
Product Version: NetBeans IDE 8.0
Java: 1.8.0; Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
System: Mac OS X version 10.9.2
有没有人有过这个问题来帮助我如何解决它?我正在搜索Netbeans bug没有运气,Mac OSX更新但我的计算机已经更新。也许我在IntroPage
的代码上做错了。
仅供参考: