如何通过打印机打印JFrame的内容

时间:2013-11-01 10:33:09

标签: java printing jframe

我想通过打印机打印JFrame/JPanel的内容怎么做?可以给我一个代码示例吗?我不懂官方教程。

编辑: 我觉得很清楚。我不想将其打印到文件然后手动打印。我想用java来做这件事。

1 个答案:

答案 0 :(得分:0)

    BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    this.paint(g);
    g.dispose();

    // File to save output Image
    File imageOut = new File("screenshot.png");
    try {
        ImageIO.write(image, "png", imageOut);
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }

将其作为JFrame

相关问题