使用java netbeans从JLabel打印图像(使用打印机)

使用java netbeans从JLabel打印图像(使用打印机),java,netbeans,Java,Netbeans,我正在使用java netbeans,希望从JLabel打印图像,但它不起作用。如果我使用TextArea.print()。我只想打印图片,请帮忙 Icon icon = noskha_kamilaa.getIcon(); BufferedImage bi=new BufferedImage(icon.getIconWidth(),icon.getIconHeight(),BufferedImage.TYPE_INT_RGB); Graphics g=bi.createGraphics();

我正在使用java netbeans,希望从JLabel打印图像,但它不起作用。如果我使用
TextArea.print()
。我只想打印图片,请帮忙

Icon icon = noskha_kamilaa.getIcon();
BufferedImage bi=new BufferedImage(icon.getIconWidth(),icon.getIconHeight(),BufferedImage.TYPE_INT_RGB);

Graphics g=bi.createGraphics();
g.dispose();

try{ 
    printAll(g);

    JOptionPane.showMessageDialog(null, "success");

}catch(java.awt.print.PrinterException  e)      // give an error
{       
    System.err.format("Cannot print %%n", e.getMessage());   
}

您正在打印前处理图形对象

g.dispose();

try{ 
    printAll(g);

    JOptionPane.showMessageDialog(null, "success");

}catch(java.awt.print.PrinterException  e)      // give an error
{       
    System.err.format("Cannot print %%n", e.getMessage());   
}
应该是

try{ 
    printAll(g);

    JOptionPane.showMessageDialog(null, "success");
    g.dispose();
}catch(java.awt.print.PrinterException  e)      // give an error
{       
    System.err.format("Cannot print %%n", e.getMessage());   
}

感谢您的回答,但是catch(java.awt.print.PrinterException e)给我一个错误异常PrinterException从未在相应的try语句体中抛出抱歉。。我没抓到你。你是什么意思?我照你说的做了,但是}catch(java.awt.print.PrinterException e)给我一个错误你能告诉我如何从标签上获取图像并打印吗??是否有打印功能默认值,如textArea.print(),您可以参考