当前位置: 技术问答>java相关
求打印预浏
来源: 互联网 发布时间:2015-11-09
本文导语: 同题目 | import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.*; import java.awt.print.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; public class PrintPreview extends...
同题目
|
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class PrintPreview extends JFrame
{
protected int m_wPage;
protected int m_hPage;
protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;
public PrintPreview(Printable target) {
this(target, "Print Preview");
}
public PrintPreview(Printable target, String title) {
super(title);
setSize(600, 400);
m_target = target;
JToolBar tb = new JToolBar();
JButton bt = new JButton("Print", new ImageIcon("print.gif"));
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Use default printer, no dialog
PrinterJob prnJob = PrinterJob.getPrinterJob();
prnJob.setPrintable(m_target);
setCursor( Cursor.getPredefinedCursor(
Cursor.WAIT_CURSOR));
prnJob.print();
setCursor( Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR));
dispose();
}
catch (PrinterException ex) {
ex.printStackTrace();
System.err.println("Printing error: "+ex.toString());
}
}
};
bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(4,6,4,6));
tb.add(bt);
bt = new JButton("Close");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
};
bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(2,6,2,6));
tb.add(bt);
String[] scales = { "10 %", "25 %", "50 %", "100 %" };
m_cbScale = new JComboBox(scales);
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread runner = new Thread() {
public void run() {
String str = m_cbScale.getSelectedItem().
toString();
if (str.endsWith("%"))
str = str.substring(0, str.length()-1);
str = str.trim();
int scale = 0;
try { scale = Integer.parseInt(str); }
catch (NumberFormatException ex) { return; }
int w = (int)(m_wPage*scale/100);
int h = (int)(m_hPage*scale/100);
Component[] comps = m_preview.getComponents();
for (int k=0; k
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class PrintPreview extends JFrame
{
protected int m_wPage;
protected int m_hPage;
protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;
public PrintPreview(Printable target) {
this(target, "Print Preview");
}
public PrintPreview(Printable target, String title) {
super(title);
setSize(600, 400);
m_target = target;
JToolBar tb = new JToolBar();
JButton bt = new JButton("Print", new ImageIcon("print.gif"));
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Use default printer, no dialog
PrinterJob prnJob = PrinterJob.getPrinterJob();
prnJob.setPrintable(m_target);
setCursor( Cursor.getPredefinedCursor(
Cursor.WAIT_CURSOR));
prnJob.print();
setCursor( Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR));
dispose();
}
catch (PrinterException ex) {
ex.printStackTrace();
System.err.println("Printing error: "+ex.toString());
}
}
};
bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(4,6,4,6));
tb.add(bt);
bt = new JButton("Close");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
};
bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(2,6,2,6));
tb.add(bt);
String[] scales = { "10 %", "25 %", "50 %", "100 %" };
m_cbScale = new JComboBox(scales);
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread runner = new Thread() {
public void run() {
String str = m_cbScale.getSelectedItem().
toString();
if (str.endsWith("%"))
str = str.substring(0, str.length()-1);
str = str.trim();
int scale = 0;
try { scale = Integer.parseInt(str); }
catch (NumberFormatException ex) { return; }
int w = (int)(m_wPage*scale/100);
int h = (int)(m_hPage*scale/100);
Component[] comps = m_preview.getComponents();
for (int k=0; k
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!