java - 在java中打印jframe

标签 java swing jframe

如何打印JFrame的内容?

最佳答案

     PrinterJob job = PrinterJob.getPrinterJob();
     job.setPrintable(this);
     boolean ok = job.printDialog();
         if (ok)
        {
            try
            {
              job.print();
            }
            catch (PrinterException ex)
            {
            /* The job did not successfully complete */
            }
        }

       public int print(Graphics g, PageFormat pf, int page) throws
                                                    PrinterException {

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Now print the window and its visible contents */
    jFrame.paint(g2d);
    jFrame.repaint();
    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

关于java - 在java中打印jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314505/

相关文章:

java - 出现 Null Pointed 异常错误但运行正常? Java JComboBox(下拉框)

c++ - 用 jni 显示 hello world swing

java - 键入时,从存储的现有文本中将单词(文本)插入到 JTextArea 中

java - 如何在Java中创建一个像JOptionPane.showMessageDialog()这样的JFrame对话框等待按下按钮?

java.awt.Frame.setBackground(Color arg0) 不显示粉红色

java - 想要创建一个选择模式并在单击 2 个 JPanels 后将其关闭

java - 2D几何形状顶点坐标检测

java - Swing - 典型桌面应用程序中的并发

java - 具有继承性的构建器模式

Java GUI 垂直对齐