printapplet.java

来自「java2图形设计卷1:awt 源码」· Java 代码 · 共 57 行

JAVA
57
字号
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Properties;

public class PrintApplet extends Applet {
	Button printButton = new Button("print ...");

	static Frame getFrame(Component c) {
		while((c = c.getParent()) != null) {
			if(c instanceof Frame)
				return (Frame)c;
		}
		return null;
	}
	static void printComponents(Component c) {
		Toolkit    tk    = Toolkit.getDefaultToolkit();
		Frame      frame = getFrame(c);
		Properties props = new Properties();

		props.put("awt.print.printer",   "durango");
		props.put("awt.print.numCopies", "2");

		if(tk != null) {
			String   name = c.getName() + " print job";
			PrintJob pj   = tk.getPrintJob(frame, name, props);

			if(pj != null) {
				Graphics pg = pj.getGraphics();

				if(pg != null) {
					try {
						c.printAll(pg);
					}
					finally {
						pg.dispose();
					}
				}
				pj.end();
			}
			System.out.println(props);
		}
	}
	public void init() {
		add(printButton);
		add(new Label("print this label"));
		add(new TextField("print this textfield"));
		add(new TextArea(10,20));

		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				printComponents(PrintApplet.this);
			}
		});
	}
}

⌨️ 快捷键说明

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