在这个例子中我如何禁止打印对话框? [英] How would I suppress the print dialog in this example?

查看:184
本文介绍了在这个例子中我如何禁止打印对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void printCard() {

        PrinterJob printjob = PrinterJob.getPrinterJob();
        printjob.setJobName("Label");

        Printable printable = new Printable() {

                public int print(Graphics pg, PageFormat pf, int pageNum) {

                        if (pageNum > 0) {
                                return Printable.NO_SUCH_PAGE;
                        }

                        Dimension size = jLayeredPane2.getSize();
                        BufferedImage bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);

                        jLayeredPane2.print(bufferedImage.getGraphics());

                        Graphics2D g2 = (Graphics2D) pg;
                        g2.translate(pf.getImageableX(), pf.getImageableY());
                        g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);

                        return Printable.PAGE_EXISTS;
                }
        };

        Paper paper = new Paper();
        paper.setImageableArea(0, 0, 153, 243);
        paper.setSize(243, 154);

        PageFormat format = new PageFormat();
        format.setPaper(paper);
        format.setOrientation(PageFormat.LANDSCAPE);

        printjob.setPrintable(printable, format);
        if (printjob.printDialog() == false)
                return;

        try {
                printjob.print();
        } catch (PrinterException ex) {
                System.out.println("NO PAGE FOUND." + ex);

        }
}


推荐答案

从我在代码中看到的,你调用 if(printjob.printDialog()== false)。这将始终尝试显示本机打印机属性对话框。 boolean 返回值基于用户是否单击确定或取消对话框。如果要禁止对话框,请删除如果块,因为要执行的打印作业是通过 printjob.print() 呼叫。

From what I see in your code, you call if (printjob.printDialog() == false). This will always try to show the native printer properties dialog. The boolean return value is based on whether the user clicks OK or cancels out of the dialog. If you want to suppress the dialog, remove that if block, as the printing work that you want to perform is done via the printjob.print() call.

这篇关于在这个例子中我如何禁止打印对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆