import java.awt.BorderLayout
import java.awt.Color
import java.awt.Frame
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.geom.Line2D
import java.awt.geom.Rectangle2D
import java.awt.print.PageFormat
import java.awt.print.PrinterException
import java.awt.print.PrinterJob
import javax.swing.JButton
import javax.swing.JDialog
import javax.swing.JPanel
import wf.common.SystemProperties
public class PrintPreviewDialog extends JDialog implements ActionListener{
private JButton nextButton = new JButton("Next")
private JButton previousButton = new JButton("Previous")
private JButton closeButton = new JButton("Close")
private JPanel buttonPanel = new JPanel()
private PreviewCanvas canvas
public PrintPreviewDialog(Frame parent, String title, boolean modal,
PrintTest pt, String str) {
super(parent, title, modal)
canvas = new PreviewCanvas(pt, str)
setLayout()
}
private void setLayout() {
this.getContentPane().setLayout(new BorderLayout())
this.getContentPane().add(canvas, BorderLayout.CENTER)
nextButton.setMnemonic('N')
nextButton.addActionListener(this)
buttonPanel.add(nextButton)
previousButton.setMnemonic('N')
previousButton.addActionListener(this)
buttonPanel.add(previousButton)
closeButton.setMnemonic('N')
closeButton.addActionListener(this)
buttonPanel.add(closeButton)
this.getContentPane().add(buttonPanel, BorderLayout.SOUTH)
this.setBounds((int) ((SystemProperties.SCREEN_WIDTH - 400) / 2),
(int) ((SystemProperties.SCREEN_HEIGHT - 400) / 2), 400, 400)
}
public void actionPerformed(ActionEvent evt) {
Object src = evt.getSource()
if (src == nextButton)
nextAction()
else if (src == previousButton)
previousAction()
else if (src == closeButton)
closeAction()
}
private void closeAction() {
this.setVisible(false)
this.dispose()
}
private void nextAction() {
canvas.viewPage(1)
}
private void previousAction() {
canvas.viewPage(-1)
}
class PreviewCanvas extends JPanel {
private String printStr
private int currentPage = 0
private PrintTest preview
public PreviewCanvas(PrintTest pt, String str) {
printStr = str
preview = pt
}
public void paintComponent(Graphics g) {
super.paintComponent(g)
Graphics2D g2 = (Graphics2D) g
PageFormat pf = PrinterJob.getPrinterJob().defaultPage()
double xoff
double yoff
double scale
double px = pf.getWidth()
double py = pf.getHeight()
double sx = getWidth() - 1
double sy = getHeight() - 1
if (px / py <sx / sy) {
scale = sy / py
xoff = 0.5 * (sx - scale * px)
yoff = 0
} else {
scale = sx / px
xoff = 0
yoff = 0.5 * (sy - scale * py)
}
g2.translate((float) xoff, (float) yoff)
g2.scale((float) scale, (float) scale)
Rectangle2D page = new Rectangle2D.Double(0, 0, px, py)
g2.setPaint(Color.white)
g2.fill(page)
g2.setPaint(Color.black)
g2.draw(page)
try {
preview.print(g2, pf, currentPage)
} catch (PrinterException pe) {
g2.draw(new Line2D.Double(0, 0, px, py))
g2.draw(new Line2D.Double(0, px, 0, py))
}
}
public void viewPage(int pos) {
int newPage = currentPage + pos
if (0 <= newPage &&newPage <preview.getPagesCount(printStr)) {
currentPage = newPage
repaint()
}
}
}
}
package wf.common
import java.awt.Dimension
import java.awt.Font
import java.awt.GraphicsEnvironment
import java.awt.Toolkit
public final class SystemProperties {
public static final double SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().getWidth()
public static final double SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().getHeight()
public static final String USER_DIR = System.getProperty("user.dir")
public static final String USER_HOME = System.getProperty("user.home")
public static final String USER_NAME = System.getProperty("user.name")
public static final String FILE_SEPARATOR = System.getProperty("file.separator")
public static final String LINE_SEPARATOR = System.getProperty("line.separator")
public static final String PATH_SEPARATOR = System.getProperty("path.separator")
public static final String JAVA_HOME = System.getProperty("java.home")
public static final String JAVA_VENDOR = System.getProperty("java.vendor")
public static final String JAVA_VENDOR_URL = System.getProperty("java.vendor.url")
public static final String JAVA_VERSION = System.getProperty("java.version")
public static final String JAVA_CLASS_PATH = System.getProperty("java.class.path")
public static final String JAVA_CLASS_VERSION = System.getProperty("java.class.version")
public static final String OS_NAME = System.getProperty("os.name")
public static final String OS_ARCH = System.getProperty("os.arch")
public static final String OS_VERSION = System.getProperty("os.version")
public static final String[] FONT_NAME_LIST = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()
public static final Font[] FONT_LIST = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()
}
1.
/**缓漏 * 开始打印
*/
public void startPrint() {
Toolkit kit = Toolkit.getDefaultToolkit() //获取工具箱
Properties props = new Properties()
props.put("awt.print.printer", "durango") //设置打印属性
props.put("awt.print.numCopies", "2")
春世 if (kit != null) {
//获取工具箱自带的打印对象
PrintJob printJob = kit.getPrintJob(owner, "Print View Frame", props)
if (printJob != null) {
Graphics pg = printJob.getGraphics() //获取打印对象的图形环境
if (pg != null) {
try {
this.paintAll(pg) //打印该窗体及其扒哪肢所有的组件
} finally {
pg.dispose() //注销图形环境
}
}
printJob.end() //结束打印作业
}
}
}
2.OpenSwing里面有一个Demo,你可以看看
java.io.PrintStream类的对象,java.io.PrintStream类有些什么方法等一下再说。被关键字static修饰的数据成员或方法可以直接通过“类名.数据成员”或“类名.方法”来引用,而无须先建立对象。所以System.out是应用了out这个静态数据成员。
1、System 是一个类,out是一个static PrintStream 对象。由于它是“静态”的,所以不需要我们创建任何东西,所以只需直接用它即可。
2、println()的意思是“把我给你的东西打印到控制台,并用一个新行结束”。所以在任何Java 程序中,一旦要把某些内容打印到控制台,就可条件反射地茄仿携写上System.out.println("内容")。
拓展大答资料:
1、Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。
2、Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程颤伏序等。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)