package com.tbr.app.print;
import java.awt.event.*;
import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
import java.awt.geom.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class PrintPreviewDialog extends JDialog
implements ActionListener
{
private JButton nextButton = new JButton("后一页");
private JButton previousButton = new JButton("前一页");
private JButton closeButton = new JButton("关闭");
private JPanel buttonPanel = new JPanel();
private PreviewCanvas canvas;
public PrintPreviewDialog(Frame parent, String title, boolean modal,
Printable pt){
super(parent, title, modal);
canvas = new PreviewCanvas(pt);
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.EAST);
java.awt.Dimension size=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
size.width-=100;
size.height-=30;
this.setSize(size);
}
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 int currentPage = 0;
private Printable preview;
private int pagecount=0;
public PreviewCanvas(Printable pt){
this.pagecount = 9999;
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{
if (preview.print(g2, pf, currentPage)==Printable.NO_SUCH_PAGE){
this.currentPage--;
this.pagecount=currentPage+1;
}
}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 < pagecount){
currentPage = newPage;
repaint();
}
}
}
}
Top
16 楼tanghuan()回复于 2003-09-27 11:07:21 得分 0 import com.borland.dbswing.JdbTable;
import com.borland.dbswing.DBExceptionHandler;
import com.borland.dbswing.DBTableModel;
import javax.swing.*;
import com.borland.dx.dataset.*;
import javax.swing.table.*;
import com.borland.dx.text.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;
import com.tbr.app.print.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class MydbTable extends JdbTable implements java.awt.print.Printable {
final static Color bg = Color.white;
final static Color fg = Color.black;
final static Color red = Color.red;
final static Color white = Color.white;
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f);
final static float dash1[] = {10.0f};
final static BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
final static JButton button = new JButton("Print");
/**
* <p>Creates <code>JdbTable</code>'s default right-click popup menu.</p>
* @return The popup menu.
*/
protected JPopupMenu createPopupMenu() {
JPopupMenu menu = new JPopupMenu();
menu.setDefaultLightWeightPopupEnabled(false);
JMenuItem menuItem;
// JMenu sortMenu = new JMenu(Res._SortBy);
JMenu sortMenu = new JMenu("排序");
if (this.getDataSet().isEditing()) {
sortMenu.setEnabled(false);
}
// JCheckBoxMenuItem checkItem = new JCheckBoxMenuItem(Res._Unsorted);
JCheckBoxMenuItem checkItem = new JCheckBoxMenuItem("取消排序");
checkItem.addActionListener(this);
checkItem.setActionCommand("nosort");
SortDescriptor sortDescriptor = this.getDataSet().getSort();
String columnSortKey = "";
if (sortDescriptor == null) {