package print;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class mainUI extends JFrame implements ActionListener {
/**
 * 
 */
private static final long serialVersionUID = 1L;
JButton jb=new JButton("打印");
 
  public static void main(String[] args) {
  	
  	new mainUI();
  }
  public mainUI() {
      
      Font font=new Font("宋体",Font.BOLD,50);
      jb.setFont(font);
      JTextArea jta=new JTextArea();
      jta.setFont(new Font("仿宋",Font.BOLD,30));
      JScrollPane jsp= new JScrollPane();
      jsp.add(jta);
      jb.addActionListener(this);
      this.add(jb,BorderLayout.NORTH);
      this.add(jta,BorderLayout.CENTER);
      this.setTitle("打印功能练习");
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setSize(600, 600);
      this.setLocation(100, 100);
      this.setVisible(true);
  }

   
@Override
public void actionPerformed(ActionEvent e) {

		
	if (e.getSource()==jb) {
		new PrintableDome().pint();;
}
}
}
package print;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PrintableDome implements Printable {
public int print(Graphics g, PageFormat pf, int pageIndex) {
  if (pageIndex != 0)
    return NO_SUCH_PAGE;
  Graphics2D g2 = (Graphics2D) g;
  g2.setFont(new Font("Serif", Font.PLAIN, 36));
  g2.setPaint(Color.black);
  g2.drawString("Java Source and Support!", 144, 144);
  return PAGE_EXISTS;
}

public void pint() {
  PrinterJob pj = PrinterJob.getPrinterJob();
  pj.setPrintable(new PrintableDome());
  if (pj.printDialog()) {
    try {
      pj.print();
    } catch (PrinterException e) {
      System.out.println(e);
    }
  }
}

}