⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printingexample3.java

📁 一个用java开发界面的程序集(jfc核心编程)
💻 JAVA
字号:
package JFCBook.Chapter4.jdk13;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class PrintingExample3 {

	// Create default page and job attributes
    static PageAttributes page = new PageAttributes();
    static JobAttributes job = new JobAttributes();

    static void print() {
		// Print the file content
		text.setText("Printing file...");

        // Use the page and job attributes to
        // profile the print job...
		PrintJob pj = Toolkit.getDefaultToolkit().getPrintJob(
									frame, "Print File", job, page);
		if (pj == null) {
			text.setText("Printing cancelled");
			return;
		}

 		int printerResolution = pj.getPageResolution();
		int topMargin = printerResolution;			// 1 inch
		int leftMargin = printerResolution/2;		// 0.5 inches

		Dimension pageSize = new Dimension(pj.getPageDimension());
		pageSize.width -= 2 *leftMargin;
		pageSize.height -= 2 * topMargin;

		// Use try/finally to release PrintJob even if we get an exception
		try {
			String data;

			// Set margin and clip
			Graphics g = pj.getGraphics();
			g.translate(leftMargin, topMargin);	// Skip margins
			g.clipRect(0, 0, pageSize.width, pageSize.height);
			g.setFont(font);

			// Get font metrics from the printer graphics
			fm = g.getFontMetrics();
			fontHeight = fm.getHeight();
			fontAscent = fm.getAscent();

			int line = fontAscent;			// Pixel line on page
			int column = 0;					// Pixel column on page
			int maxLine = pageSize.height - fontHeight;
			int tabGap = 8 * fm.stringWidth(" ");
			boolean isTab;					// True when we find a Tab

			BufferedReader r = new BufferedReader(new FileReader(file));
			text.setText("Printing started");

			while((data = r.readLine()) != null) {
				if (data.equals("")) {
					data = " ";
				}
				StringTokenizer st = new StringTokenizer(data, " \t", true);
				while (st.hasMoreElements() == true) {
					String token = (String)st.nextElement();
					isTab = token.equals("\t");
					int stringWidth;

					if (isTab == true) {
						int nextCol = ((column + tabGap)/tabGap) * tabGap;
						stringWidth = nextCol - column;
					} else {
						stringWidth = fm.stringWidth(token);
					}

					if (column + stringWidth > pageSize.width) {
						line += fontHeight;
						column = 0;
						if (isTab == true) {
							stringWidth = tabGap;
						}
						if (line > maxLine) {
							g.dispose();					// Flush page
							g = pj.getGraphics();				// Access to printer
							g.translate(leftMargin, topMargin);	// Skip margins
							g.clipRect(0, 0, pageSize.width, pageSize.height);
							g.setFont(font);
							line = fontAscent;
							column = 0;
						}
					}

					if (isTab == false) {
						g.drawString(token, column, line);
					}
					column += stringWidth;
				}

				// End of line: go to next line
				column = pageSize.width;
			}

			g.dispose();					// Print the last page

		} catch (Exception e) {
			System.out.println("Exception while reading file data: " + e);
		} finally {
			// Always terminate the print job
			pj.end();
			text.setText("Printing completed");
		}
	}

	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println("Please supply one file name");
			System.exit(1);
		}

		file = new File(args[0]);
		if (file.canRead() == false) {
			System.out.println("Cannot read " + args[0]);
			System.exit(1);
		}

		font = new Font("serif", Font.PLAIN, 12);
		text.setEditable(false);
		frame.getContentPane().add(text, BorderLayout.CENTER);
		text.setText("Press \"Print\" to print file \"" + args[0] + "\"");
		frame.getContentPane().add(button, BorderLayout.EAST);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				print();
			}
		});

		frame.pack();
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		});
		frame.setVisible(true);
	}

	static JFrame frame = new JFrame("Printing Example 3");
	static JTextField text = new JTextField(40);
	static JButton button = new JButton("Print...");
	static File file;
	static Font font;
	static FontMetrics fm;
	static int fontHeight;
	static int fontAscent;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -