How can I print a single JPanel's contents?

前端 未结 3 455
感动是毒
感动是毒 2020-11-29 03:28

I have a JPanel with two labels with pictures. I need to print these content of the JPanel. Please help me out. How can I print only this JPa

3条回答
  •  迷失自我
    2020-11-29 04:02

    just edit and put the name of your frame, panel(jPanel1) and button(print). 'this' refers to the JFrame class(i.e my class extends javax.swing.JFrame ) just put your frame's reference instead of 'this'.

    private void PritnActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        Toolkit tkp = jPanel1.getToolkit();
        PrintJob pjp = tkp.getPrintJob(this, null, null);
        Graphics g = pjp.getGraphics();
        jPanel1.print(g);
        g.dispose();
        pjp.end();
    }
    

提交回复
热议问题