參考書籍完成的一個可以打印一個單獨JTable組件的涵數,希望對大家有所幫助.
private void printTable() {
Toolkit kit = Toolkit.getDefaultToolkit(); //獲取工具箱
Properties props = new Properties();
props.put("awt.print.printer", "durango"); //設置打印屬性
props.put("awt.print.numCopies", "2");
if (kit != null) {
//獲取工具箱自帶的打印對象
PrintJob printJob = kit.getPrintJob(this, "打印 頁面", props);
if (printJob != null) {
Graphics pg = printJob.getGraphics(); //獲取打印對象的圖形環境
Graphics2D g2 = (Graphics2D) pg; ///
PageFormat pf = new PageFormat(); ///
g2.translate(pf.getImageableX(), pf.getImageableY()); ///轉換坐標,確定打印邊界
if (pg != null) {
try {
pg.dispose(); // Shoot the page to printer
this.jScrollPane14.printAll(pg); //打印該窗體的組件
}
finally {
pg.dispose(); //注銷圖形環境pageIndex
}
}
printJob.end(); //結束打印作業
}
}
}