Java - 用对话框打印文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11385581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:52:35  来源:igfitidea点击:

Java - Print document with dialog?

javaswingprintingjframe

提问by Primm

How do I open up a print dialog box where you select your printer, page details, etc to print A SPECIFIED DOCUMENT OR JTEXTPANE?

如何打开一个打印对话框,您可以在其中选择打印机、页面详细信息等以打印 A SPECIFIED DOCUMENT 或 JTextPANE?

Please help!

请帮忙!

note: getDesktop().print gives me an error about printer setup, how to open the native print dialog?

注意:getDesktop().print 给我一个关于打印机设置的错误,如何打开本机打印对话框?

回答by tenorsax

Check out Lesson: Printingin Java tutorials, Using Print Setup Dialogsin particular. PrinterJob.printDialog()should do the trick.

查看课程:在 Java 中打印教程,特别是使用打印设置对话框PrinterJob.printDialog()应该可以解决问题。

回答by Konrad Reiche

An application displays a print dialog when the user presses a button related to the print command, or chooses an item from the print menu. To display this dialog, call the printDialog method of the PrinterJob class:

当用户按下与打印命令相关的按钮或从打印菜单中选择一个项目时,应用程序会显示一个打印对话框。要显示此对话框,请调用 PrinterJob 类的 printDialog 方法:

PrinterJob pj = PrinterJob.getPrinterJob();
//...
    if (pj.printDialog()) {
        try {pj.print();}
        catch (PrinterException exc) {
            System.out.println(exc);
         }
     }   
//...  

Reference:

参考