当前位置: 技术问答>java相关
高分求救JTable打印预览程序
来源: 互联网 发布时间:2015-11-09
本文导语: 救JTable打印预览程序 | /** PrintPreview */ 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.*;...
救JTable打印预览程序
|
/**
PrintPreview
*/
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 int m_orientation;
protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;
public PrintPreview(Printable target)
{
this(target, "Print Preview", PageFormat.PORTRAIT);
}
public PrintPreview(Printable target, String title, int orientation)
{
super(title);
setSize(600, 400);
getContentPane().setLayout(new BorderLayout());
m_target = target;
m_orientation = orientation;
JToolBar tb = new JToolBar();
JButton bt = new JButton("Print", new ImageIcon("print.gif"));
ActionListener lst = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
PrinterJob prnJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = prnJob.defaultPage();
pageFormat.setOrientation(m_orientation);
prnJob.setPrintable(m_target, pageFormat);
if (!prnJob.printDialog())
return;
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
PrintPreview
*/
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 int m_orientation;
protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;
public PrintPreview(Printable target)
{
this(target, "Print Preview", PageFormat.PORTRAIT);
}
public PrintPreview(Printable target, String title, int orientation)
{
super(title);
setSize(600, 400);
getContentPane().setLayout(new BorderLayout());
m_target = target;
m_orientation = orientation;
JToolBar tb = new JToolBar();
JButton bt = new JButton("Print", new ImageIcon("print.gif"));
ActionListener lst = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
PrinterJob prnJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = prnJob.defaultPage();
pageFormat.setOrientation(m_orientation);
prnJob.setPrintable(m_target, pageFormat);
if (!prnJob.printDialog())
return;
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