我尝试使用以下代码打印JPanel:
try {
PrinterJob gap = PrinterJob.getPrinterJob();
gap.setPrintable(this);
boolean top = gap.printDialog();
if(top){
gap.print();
}
} catch (PrinterException ex) {
JOptionPane.showMessageDialog(null, " ERROR DEL PROGRAMA", "ERROR \n " + ex , JOptionPane.INFORMATION_MESSAGE);
}
但是,它会出现选择打印机的配置菜单。我不想选择打印机,我希望它是默认打印机或者在代码中选择打印机。
答案 0 :(得分:0)
如果您不想显示打印对话框,请删除' gap.printDialog()',您只需要使用' gap.print()'
你可以做一些像这样的事情:
PrinterJob gap = PrinterJob.getPrinterJob();
PageFormat pf = gap.defaultPage();
//Manually assign some of the basic print settings (More advanced things may require you to write your own code)
gap.setJobName("My Print Jnb");
//Set custom page size?
Paper paper = new Paper();
paper.setSize(595, 842);
pf.setPaper(paper);
//Set page orientation?
pf.setOrientation(PageFormat.LANDSCAPE);
//Create the print job with our manual settings
gap.setPrintable(this, pf);
try
{
//And finally print it out
gap.print();
}
catch (PrinterException e)
{
JOptionPane.showMessageDialog(null, " ERROR DEL PROGRAMA", "ERROR \n " + ex , JOptionPane.INFORMATION_MESSAGE);
}
编辑:如果您想手动选择打印机,请查看以下问题: How do I specify the printer I want to use in Java?