import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
/**
* 使用了原始的分页方式去渲染JTextArea,提供了打印预览机制。
* <p>
* 事实上,我们还可以通过其他方式打印JTextArea:
* <ol>
* <li>{@code Component.print(Graphics g);} or
* {@code Component.printAll(Graphics g);}</li>
* <li>{@code Component.paint(Graphics g);} or
* {@code Component.paintAll(Graphics g);} whose rending may be slightly
* different to the previous method (for example, <code>JFrame</code>)</li>
* <li>{@code JTable.print();} or {@code JTextComponent.print();} provide
* especially powerful and convenient printing mechanism</li>
* </ol>
*
* @author Gaowen
*/
public class PrintUIComponent extends JPanel implements ActionListener,
Printable {
private static final long serialVersionUID = 4797002827940419724L;
private static JFrame frame;
private JTextArea textAreaToPrint;
private PrinterJob job;
private int[] pageBreaks;// array of page break line positions
private String[] textLines;
private Header header;
public PrintUIComponent() {
super(ne
Java打印和打印预览机制
最新推荐文章于 2021-03-16 03:09:47 发布
本文深入探讨了Java的Graphics2D和Java2D技术在实现打印及打印预览功能中的应用。通过理解这些核心概念,开发者可以有效地创建程序来控制打印机输出,并提供高质量的预览体验。
摘要由CSDN通过智能技术生成