1、java 程序源代码实现翻页/源文件名:album.java/在下载源程序中的文件夹:0709相册import java.applet.*;import java.awt.*;import java.awt.event.*;public class album extends Applet implements ActionListener Image img; int j=0; String f; Label a1=new Label(文件名:); Label a2=new Label( ); Button btn1,btn2; public void init() img = new Im
2、age22; setLayout(null); setBackground(Color.cyan); add(a1); add(a2); btn1= new Button(下一页); btn2= new Button(上一页); add(btn1); add(btn2); a1.setBounds(650,60,60,30); a2.setBounds(650,80,110,60); a2.setBounds(650,80,110,60); a1.setBackground(Color.cyan); a2.setBackground(Color.cyan); a2.setForeground(
3、Color.red); Font ft = new Font(Times New Romon,1,20); a2.setFont(ft); btn1.setBounds(650,180,60,30); btn2.setBounds(650,240,60,30); btn1.addActionListener(this); btn2.addActionListener(this); for (int i=0;i21) j=0; if(e.getSource()=btn2) j-; if(j0) j=21; repaint(); 实现图行化界面package swt_jface.demo11; i
4、mport org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.Gr
5、idLayout; import org.eclipse.swt.printing.PrintDialog; import org.eclipse.swt.printing.Printer; import org.eclipse.swt.printing.PrinterData; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.FileDialog;
6、 import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; public class ImageViewer Display display = new Display(); Shell shell = new Shell(display); Canvas canvas; Image image; String fileName; pu
7、blic ImageViewer() shell.setText(Image viewer); shell.setLayout(new GridLayout(1, true); ToolBar toolBar = new ToolBar(shell, SWT.FLAT); ToolItem itemOpen = new ToolItem(toolBar, SWT.PUSH); itemOpen.setText(Open); itemOpen.addListener(SWT.Selection, new Listener() public void handleEvent(Event event
8、) FileDialog dialog = new FileDialog(shell, SWT.OPEN); String file = dialog.open(); if (file != null) if (image != null) image.dispose(); image = null; try image = new Image(display, file); catch (RuntimeException e) if (image != null) fileName = file; else System.err.println( Failed to load image f
9、rom file: + file); canvas.redraw(); ); ToolItem itemPrintPreview = new ToolItem(toolBar, SWT.PUSH); itemPrintPreview.setText(Preview); itemPrintPreview.addListener(SWT.Selection, new Listener() public void handleEvent(Event event) ImagePrintPreviewDialog dialog = new ImagePrintPreviewDialog(ImageVie
10、wer.this); dialog.open(); ); ToolItem itemPrint = new ToolItem(toolBar, SWT.PUSH); itemPrint.setText(Print); itemPrint.addListener(SWT.Selection, new Listener() public void handleEvent(Event event) print(); ); canvas = new Canvas(shell, SWT.BORDER); canvas.setBackground(display.getSystemColor(SWT.CO
11、LOR_WHITE); canvas.setLayoutData(new GridData(GridData.FILL_BOTH); canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) if (image = null) e.gc.drawString(No image, 0, 0); else e.gc.drawImage(image, 0, 0); ); image = new Image(display, C:/icons/scene.jpg); fileName = sce
12、ne.jpg; shell.setSize(500, 400); shell.open(); while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep(); display.dispose(); void print() PrintDialog dialog = new PrintDialog(shell); PrinterData printerData = dialog.open(); if (printerData = null) return; Printer printer = new Printe
13、r(printerData); print(printer, null); void print(final Printer printer, PrintMargin printMargin) if (image = null) return; final Point printerDPI = printer.getDPI(); final Point displayDPI = display.getDPI(); System.out.println(displayDPI + + printerDPI); final PrintMargin margin = (printMargin = nu
14、ll ? PrintMargin.getPrintMargin(printer, 1.0) : printMargin); Thread printThread = new Thread() public void run() if (!printer.startJob(fileName) System.err.println(Failed to start print job!); printer.dispose(); return; GC gc = new GC(printer); if (!printer.startPage() System.err.println(Failed to
15、start a new page!); gc.dispose(); return; else int imageWidth = image.getBounds().width; int imageHeight = image.getBounds().height; double dpiScaleFactorX = printerDPI.x * 1.0 / displayDPI.x; double dpiScaleFactorY = printerDPI.y * 1.0 / displayDPI.y; double imageSizeFactor = Math.min( 1, (margin.r
16、ight - margin.left) * 1.0 / (dpiScaleFactorX * imageWidth); imageSizeFactor = Math.min( imageSizeFactor, (margin.bottom - margin.top) * 1.0 / (dpiScaleFactorY * imageHeight); gc.drawImage( image, 0, 0, imageWidth, imageHeight, margin.left, margin.top, (int) (dpiScaleFactorX * imageSizeFactor * image
17、Width), (int) (dpiScaleFactorY * imageSizeFactor * imageHeight); gc.dispose(); printer.endPage(); printer.endJob(); printer.dispose(); System.out.println(Printing job done!); ; printThread.start(); public static void main(String args) new ImageViewer(); ImagePrintPreviewDialog.java 复制代码 代码如下:package
18、 swt_jface.demo11; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; im
19、port org.eclipse.swt.printing.PrintDialog; import org.eclipse.swt.printing.Printer; import org.eclipse.swt.printing.PrinterData; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Dialog; import org.eclip
20、se.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class ImagePrintPreviewDialog extends Dialog ImageViewer viewer; Shell shell; Canvas canvas; Printer printer; PrintMargin margin; Combo combo; public Imag
21、ePrintPreviewDialog(ImageViewer viewer) super(viewer.shell); this.viewer = viewer; public void open() shell = new Shell( viewer.shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE); shell.setText(Print preview); shell.setLayout(new GridLayout(4, false); final Button buttonSelectPrinter = new
22、 Button(shell, SWT.PUSH); buttonSelectPrinter.setText(Select a printer); buttonSelectPrinter.addListener(SWT.Selection, new Listener() public void handleEvent(Event event) PrintDialog dialog = new PrintDialog(shell); PrinterData printerData = dialog.open(); if (printerData = null) return; final Prin
23、ter printer = new Printer(printerData); setPrinter( printer, Double.parseDouble( combo.getItem(combo.getSelectionIndex(); ); new Label(shell, SWT.NULL).setText(Margin in inches: ); combo = new Combo(shell, SWT.READ_ONLY); combo.add(0.5); combo.add(1.0); combo.add(1.5); combo.add(2.0); combo.add(2.5)
24、; combo.add(3.0); combo.select(1); combo.addListener(SWT.Selection, new Listener() public void handleEvent(Event event) double value = Double.parseDouble( combo.getItem(combo.getSelectionIndex(); setPrinter(printer, value); ); final Button buttonPrint = new Button(shell, SWT.PUSH); buttonPrint.setTe
25、xt(Print); buttonPrint.addListener(SWT.Selection, new Listener() public void handleEvent(Event event) if (printer = null) viewer.print(); else viewer.print(printer, margin); shell.dispose(); ); canvas = new Canvas(shell, SWT.BORDER); GridData gridData = new GridData(GridData.FILL_BOTH); gridData.hor
26、izontalSpan = 4; canvas.setLayoutData(gridData); canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) int canvasBorder = 20; if (printer = null | printer.isDisposed() return; Rectangle rectangle = printer.getBounds(); Point canvasSize = canvas.getSize(); double viewScal
27、eFactor = (canvasSize.x - canvasBorder * 2) * 1.0 / rectangle.width; viewScaleFactor = Math.min( viewScaleFactor, (canvasSize.y - canvasBorder * 2) * 1.0 / rectangle.height); int offsetX = (canvasSize.x - (int) (viewScaleFactor * rectangle.width) / 2; int offsetY = (canvasSize.y - (int) (viewScaleFa
28、ctor * rectangle.height) / 2; e.gc.setBackground( shell.getDisplay().getSystemColor(SWT.COLOR_WHITE); e.gc.fillRectangle( offsetX, offsetY, (int) (viewScaleFactor * rectangle.width), (int) (viewScaleFactor * rectangle.height); e.gc.setLineStyle(SWT.LINE_DASH); e.gc.setForeground(shell.getDisplay().g
29、etSystemColor(SWT.COLOR_BLACK); int marginOffsetX = offsetX + (int) (viewScaleFactor * margin.left); int marginOffsetY = offsetY + (int) (viewScaleFactor * margin.top); e.gc.drawRectangle( marginOffsetX, marginOffsetY, (int) (viewScaleFactor * (margin.right - margin.left), (int) (viewScaleFactor * (ma