急求一个用Java实现的打印及打印预览功能的Demo

作者&投稿:御树 (若有异议请与网页底部的电邮联系)
请问JAVA如何实现打印及打印预览功能?~

package com.szallcom.tools;

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();
}

Toolkit kit = Toolkit.getDefaultToolkit();
Properties props = new Properties();
props.put("awt.print.printer", "your printer");
props.put("awt.print.numCopies", "1");
PrintJob printJob = kit.getPrintJob(this, "打印", props); //this指你的JFrame
Graphics2D g2 = (Graphics2D)printJob.getGraphics();
PageFormat pf = new PageFormat();
g2.translate(pf.getImageableX(), pf.getImageableY());
jtable.printAll(g2);
g2.dispose();
printJob.end();
这是最简单的代码,复杂的只能自己学习了

package com.szallcom.tools;

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();
}

用个小插件就哦了


用java编写一个方法实现如下功能:求一个整数的绝对值,在主方法中调用该...
public class Test{ public static void main(String []args){ System.out.println(abs(-5));System.out.println(abs(1));} public static int abs(int a){ if(a>0){ return a;}else{ return 0-a;} } }

请教一个JAVA的作业,求解答,非常谢谢!
接口代码如下:\/ 接口Shape \/ public interface Shape { public double area();} Ciecle类代码如下:public class Circle implements Shape { private double x; \/\/圆心坐标x private double y; \/\/圆心坐标y private double r; \/\/半径 public Circle(double r){ this.r = r; \/\/初始化...

用java实现输入一个数字n代表共n个数据,之后依次输入n个数据,分别求最...
java.util.Scanner;public class Evaluate {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入资料笔数:");int count = scanner.nextInt();int sum = 0,max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;for (int i = 1;...

用Java实现一个地铁票价计算程序,希望给出主要算法与数据结构
根据某市地铁线路图写一个地铁票价计算程序 需求描述:1.计费规则:最低2元,超过5站以上每站加收0.5元,换乘重新起算,例如L1先坐4站,换乘L2再坐6站,结果就是2+2.5=5.5元 2.程序启动以后读取输入文件(in.txt),内容格式如:L2-8,L2-2 X3,L3-8 ...每行表示一次行程,起点站和终点...

用java语言编程实现计算器的基本功能
import java.awt.*;import java.awt.event.*;public class JSQ extends WindowAdapter { Panel p1 = new Panel();Panel p2 = new Panel();Panel p3 = new Panel();TextField txt;private Button[] b = new Button[19];private String ss[] = { "1", "2", "3", "+", "-", "4...

用java实现,一个文件中有100万条商品id,求出商品最多的前100个...
package com.wang;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;public class Zuoye{public static void main(...

用java程序设计一简易计算器:可以实现+,—,*,\/,%和三角运算。急用...
1 2015-01-31 用JAVA编写一个简单的计算器,要求如下 17 2015-01-11 求一个用JAVA编写的计算器程序(1)实现简单加、减、乘、除... 2012-07-02 用JAVA编写一个计算器 320 2011-07-06 你好,请问你有JAVA程序计算器的代码么。 能实现普通的运...更多...

编写JAVA程序实现一个简单的日历(高分求高手)(追加分)
编写JAVA程序实现一个简单的日历(高分求高手)(追加分) 主要功能包括显示当月的日历表,当日特殊显示。一定要用JAVA语言写。谢谢了eryaruoshui@126.com... 主要功能包括显示当月的日历表 ,当日特殊显示。 一定要用JAVA语言写。谢谢了eryaruoshui@126.com 展开  我来答 ...

用java 编写一个程序,要求输入圆的半径,求圆的周长,面积.
实现思路:输入一个半径的值,之后即可求出周长和面积:代码举例:import javax.swing.JOptionPane;class account { public static void main(String[] args){ String radiusString = JOptionPane.showInputDialog(null ,"请输入半径 : " , "计算" , JOptionPane.QUESTION_MESSAGE);double radius = ...

求java高手用while实现 输入一个数 然后输出这个数的倒序
这个叫整数反转,很简单,希望你能理解,加油,不懂再问我。import java.util.Scanner;public class LianXi6_6 { public static void main(String[] args){ Scanner s=new Scanner(System.in);System.out.println("请输入一个整数:");int a=s.nextInt();while(a>0){ int b=a%10;a=a\/...

汇川区19837259985: 请问java如何实现打印以及打印预览功能?请问用JAVA如何实现
底顾川贝: JAVA的分页打印与分页预览

汇川区19837259985: 怎样用java实现打印功能 -
底顾川贝: Print.java--打印内容定义 [code] import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.event.*; import java.util.*; import...

汇川区19837259985: 怎么做一个word模板,用java调用打印功能 -
底顾川贝: 1、首先新建一个空白文档,并另存为“Normal.dotm(Word2007及以后版本)Normal.dot(Word2003-2007版本)”. 2、进行需要的模板设置,对字体、段落、页眉页脚,页面边距等进行设置. 3、对字体、段落的设置:在空白的新文档中,右击...

汇川区19837259985: 用java实现打印功能 怎么写 求大神求源码 -
底顾川贝: PrintService[] services = PrinterJob.lookupPrintServices();//查找系统中所有的可用打印服务 PrintService service = ServiceUI.printDialog(null, 500, 500, services, services[0], null, new HashPrintRequestAttributeSet());//创建一个默认的打...

汇川区19837259985: 用java写一个方法实现读取一个.java文件里的内容并打印到控制台,打印出来的内容必须和排版的格式一样. -
底顾川贝: import java.io.*; public class ShowFile {public static void main(String args[]){String infname="ShowFile.java"; //默认的输入文件名 try{File fin=new File(infname); //转入的文件对象 BufferedReader in = new BufferedReader(new FileReader(fin...

汇川区19837259985: 求救:用java编写一个日期打印程序 实现当前日期的昨天明天的日期打印 -
底顾川贝: public class MyDate{ private int year; private int month; private int day; public MyDate(int year, int month, int day){ this.year = year; this.month = month; this.day = day; } public MyDate nextDay(){ int newDay = day + 1; int newMonth = month; int ...

汇川区19837259985: 急求一个用Java实现的打印及打印预览功能的Demo -
底顾川贝: package com.szallcom.tools; 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....

汇川区19837259985: 用java编写的程序中,如何实现打印的功能 -
底顾川贝: /developerworks/cn/java/l-javaprint/用java编写的程序中,如何实现打印的功能

汇川区19837259985: 用JAVA语言编译一个程序"打印1到100的值" -
底顾川贝: public class test { public static void main(String [] args) { System.out.println(fuc(100)); } public static double fuc(int n) { if(n==1) return 1; else...

汇川区19837259985: 请问JAVA如何实现打印及打印预览功能? -
底顾川贝: package com.szallcom.tools;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Frame;import java.awt.Graphics;import java.awt.Graphics2D;import java.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网