📄 printaction.java
字号:
package com.sunking.swing.print;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
import com.sunking.swing.*;
/**
* <p>Title: OpenSwing</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.0
*/
public class PrintAction
extends AbstractAction {
public static final String PRINT_ACTION = "PrintAction";
public static final String PAGESET_ACTION = "PageSetAction";
private PrintOptionDialog dialog;
private PageFormat pageFormat;
private Window owner;
private BookOfPrint bookOfPrint;
public PrintAction(BookOfPrint bookOfPrint) {
super(OpenSwingUtil.getOpenResource("Preview"),
OpenSwingUtil.getOpenSwingImage(
"preview.gif", new ImageIcon()));
putValue(Action.SHORT_DESCRIPTION,
OpenSwingUtil.getOpenResource("Preview"));
putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
KeyEvent.CTRL_DOWN_MASK));
PrinterJob printJob = PrinterJob.getPrinterJob();
pageFormat = printJob.defaultPage();
super.putValue("ConfirmAction", new ConfirmAction());
super.putValue(PRINT_ACTION, new PrintPageAction());
super.putValue(PAGESET_ACTION, new PageSetAction());
this.bookOfPrint = bookOfPrint;
}
public void actionPerformed(ActionEvent e) {
if (dialog == null) {
if (owner == null) owner = SwingUtilities.getWindowAncestor( (
Component) e.getSource());
dialog = new PrintOptionDialog(owner,
OpenSwingUtil.getOpenResource("Preview"));
dialog.setActions(
(Action) getValue("ConfirmAction"),
(Action) getValue(PRINT_ACTION),
(Action) getValue(PAGESET_ACTION)
);
dialog.setSize(800, 600);
dialog.setLocationRelativeTo(owner);
}
/**
* 生成Book
*/
dialog.setPageFormat(pageFormat);
if (owner instanceof Dialog) {
dialog.setBook(bookOfPrint.makeBook(true,pageFormat));
JDialog d = new JDialog( (Dialog) owner, dialog.getTitle(), true);
d.getContentPane().add(dialog.getContentPane());
d.setBounds(dialog.getBounds());
d.show();
}
else {
dialog.show();
dialog.setExtendedState(JFrame.MAXIMIZED_BOTH);
dialog.setBook(bookOfPrint.makeBook(true,pageFormat));
}
}
public Action getAction(String actionName) {
Object obj = getValue(actionName);
if (obj != null && obj instanceof Action)return (Action) obj;
return null;
}
class ConfirmAction
extends AbstractAction {
public ConfirmAction() {
super.putValue(Action.NAME, OpenSwingUtil.getOpenResource("Close"));
super.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_C));
}
public void actionPerformed(ActionEvent e) {
SwingUtilities.getWindowAncestor(
(Component) e.getSource()).setVisible(false);
}
}
class PrintPageAction
extends AbstractAction {
public PrintPageAction() {
super.putValue(Action.NAME, OpenSwingUtil.getOpenResource("Print"));
super.putValue(Action.SMALL_ICON,
OpenSwingUtil.getOpenSwingImage("print.gif",
new ImageIcon()));
super.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_P));
}
public void actionPerformed(ActionEvent e) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPageable(bookOfPrint.makeBook(false,pageFormat));
if (printJob.printDialog()) {
try {
printJob.print();
}
catch (Exception ee) {}
}
}
}
class PageSetAction
extends AbstractAction {
public PageSetAction() {
super.putValue(Action.NAME,
OpenSwingUtil.getOpenResource("PrintSetup"));
super.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
}
public void actionPerformed(ActionEvent e) {
PrinterJob printJob = PrinterJob.getPrinterJob();
if (dialog != null) pageFormat = dialog.getPageFormat();
pageFormat = printJob.pageDialog(pageFormat);
if (dialog != null) {
dialog.setPageFormat(pageFormat);
dialog.setBook(bookOfPrint.makeBook(true,pageFormat));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -