代码比较简单,供大家参考:
package taishan; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.PrintJob; import java.awt.Toolkit; import java.awt.geom.AffineTransform; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.util.Properties; import javax.swing.JFrame; @SuppressWarnings("serial") public class PrintFrame extends JFrame { private final static int startX = 200; private final static int startY = 200; private final static String TAISHAN = "泰山Office"; public PrintFrame() { this.getContentPane().setBackground(Color.WHITE); //this.setFont(font); } @Override public void paint(Graphics g) { super.paint(g); g.drawString(TAISHAN, startX, startY); } public void startPrint() { Toolkit kit = Toolkit.getDefaultToolkit(); Properties props = new Properties(); props.put("awt.print.printer", "durango"); props.put("awt.print.numCopies", "1"); PrintJob printJob = kit.getPrintJob(this, "Print Frame", props); Graphics g = printJob.getGraphics(); try { this.paintAll(g); } finally { g.dispose(); } printJob.end(); } public static void main(String[] args) { //主要用处是显示画面。 PrintFrame frame = new PrintFrame(); frame.setSize(1000, 600); frame.setVisible(true); frame.startPrint(); } }