Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java小程序可以使用打印机吗?_Java_Swing_Applet - Fatal编程技术网

Java小程序可以使用打印机吗?

Java小程序可以使用打印机吗?,java,swing,applet,Java,Swing,Applet,Java小程序能否轻松地将文本/html打印到标准打印机驱动程序(使用所有通用平台Win/Mac/Linux) 是否需要对其进行签名?要打印,您需要使用,或者如果未签名的小程序试图打印,则会提示用户询问是否允许权限 以下是使用JEditorPane打印HTML的一些示例代码: public class HTMLPrinter implements Printable{ private final JEditorPane printPane; public HTMLPrinter

Java小程序能否轻松地将文本/html打印到标准打印机驱动程序(使用所有通用平台Win/Mac/Linux)


是否需要对其进行签名?

要打印,您需要使用,或者如果未签名的小程序试图打印,则会提示用户询问是否允许权限

以下是使用JEditorPane打印HTML的一些示例代码:

public class HTMLPrinter implements Printable{
    private final JEditorPane printPane;

    public HTMLPrinter(JEditorPane editorPane){
        printPane = editorPane;
    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex){
        if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;

        Graphics2D g2d = (Graphics2D)graphics;
        g2d.setClip(0, 0, (int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight());
        g2d.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());

        RepaintManager rm = RepaintManager.currentManager(printPane);
        boolean doubleBuffer = rm.isDoubleBufferingEnabled();
        rm.setDoubleBufferingEnabled(false);

        printPane.setSize((int)pageFormat.getImageableWidth(), 1);
        printPane.print(g2d);

        rm.setDoubleBufferingEnabled(doubleBuffer);

        return Printable.PAGE_EXISTS;
    }
}
然后将其发送到打印机:

HTMLPrinter target = new HTMLPrinter(editorPane);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(target);
try{
    printJob.printDialog();
    printJob.print();
}catch(Exception e){
    e.printStackTrace();
}

为了打印,安全管理器需要允许它访问打印机。这意味着要么对小程序进行签名,要么至少对Sun Java插件的最新版本进行签名,如果未签名的小程序试图打印,则会提示用户询问是否允许权限。

Wew!如果一个随机站点开始在我的工作表上打印垃圾邮件,我会很讨厌…:-)有一个对话框。JavaScript也可以这样做。尼尔·科菲:最近的版本?已经有十年了。汤姆——谢谢你的更正——真的有那么长吗。。。?你突然让我觉得自己老了!!:-)即使是较新版本的JRE也可以使用JWS部署小程序,并使用
javax.jnlp.PrintService
。它在感觉上类似于不受信任的小程序的“提示/打印”。正如Neil COffey的回答,您不需要签名。感谢您提供的精彩代码!但是,我的测试数据有一个问题:testPanel.setText(“这是一个测试打印blaa blaa”);纸上的输出有可见的HTML标记,因此它不是格式化的,例如标题,它只是输出H1标记。Tom你必须让它使用HTMLEditorKit。在使用settext设置html内容之前,请尝试testPanel.setContentType(“text/html”),运行此操作所需的所有导入是什么?