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

📄 printapplet.java

📁 java2图形设计卷1:awt 源码
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -