日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 预览打印_请问JAVA如何实现打印及打印预览功能?

發(fā)布時間:2023/12/8 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 预览打印_请问JAVA如何实现打印及打印预览功能? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

展開全部

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,

62616964757a686964616fe4b893e5b19e31333330326631PrintTest 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();

}

本回答由網(wǎng)友推薦

已贊過

已踩過<

你對這個回答的評價是?

評論

收起

總結(jié)

以上是生活随笔為你收集整理的java 预览打印_请问JAVA如何实现打印及打印预览功能?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。