JAVA学习网首页 把生活查询网加入收藏 把JAVA学习网设为首页 联系方式
    Hi,JAVA学习
JAVA基础 设计模式 数据库 JavaBeans J2EE JavaDelphi 用户界面 综合文章  
SWT实现图片查看
时间:22/04/2007
作者:网络
来源:网络
小提示点这里把文章加入您的收藏夹,方便下次查看
设置文章字体大小:[ ]
程序演示了如何在窗体上绘制图片,以及滚动条的使用方法。
代码十分简单,我就因此不做过多的解释了。
本代码在Windows XP pro, Eclipse 2.1下测试通过。
运行此程序需要进行一些有别于普通Java Application的地方,请参见我的另一篇文章,在《Eclipse Workbench之外使用SWT和JFace》。
如果文中有不妥之处,还请指正。
  1. import org.eclipse.swt.*;
  2. import org.eclipse.swt.graphics.*;
  3. import org.eclipse.swt.widgets.*;
  4. public class d {
  5. public static void main (java/lang/String.java.html" target="_blank">String [] args) {
  6.   Display display = new Display ();
  7.   final Shell shell = new Shell (display,
  8.     SWT.SHELL_TRIM | SWT.NO_BACKGROUND |
  9.     SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
  10.   Image originalImage = null;
  11.   FileDialog dialog = new FileDialog (shell, SWT.OPEN);
  12.   dialog.setText ("选择一个图片文件或者取消");
  13.   java/lang/String.java.html" target="_blank">String string = dialog.open ();
  14.   if (string != null) {
  15.     originalImage = new Image (display, string);
  16.   }
  17.   if (originalImage == null) {
  18.     int width = 150, height = 200;
  19.     originalImage = new Image (display, width, height);
  20.     GC gc = new GC (originalImage);
  21.     gc.fillRectangle (0, 0, width, height);
  22.     gc.drawLine (0, 0, width, height);
  23.     gc.drawLine (0, height, width, 0);
  24.     gc.drawText ("默认图片", 10, 10);
  25.     gc.dispose ();
  26.   }
  27.   final Image image = originalImage;
  28.   final Point origin = new Point (0, 0);
  29.   final ScrollBar hBar = shell.getHorizontalBar ();
  30.   hBar.addListener (SWT.Selection, new Listener () {
  31.     public void handleEvent (Event e) {
  32.       int hSelection = hBar.getSelection ();
  33.       int destX = -hSelection - origin.x;
  34.       origin.x = -hSelection;
  35.       Rectangle rect = image.getBounds ();
  36.       shell.scroll (destX, 0, 0, 0, rect.width, rect.height, false);
  37.     }
  38.   });
  39.   final ScrollBar vBar = shell.getVerticalBar ();
  40.   vBar.addListener (SWT.Selection, new Listener () {
  41.     public void handleEvent (Event e) {
  42.       int vSelection = vBar.getSelection ();
  43.       int destY = -vSelection - origin.y;
  44.       origin.y = -vSelection;
  45.       Rectangle rect = image.getBounds ();
  46.       shell.scroll (0, destY, 0, 0, rect.width, rect.height, false);
  47.     }
  48.   });
  49.   shell.addListener (SWT.Resize,  new Listener () {
  50.     public void handleEvent (Event e) {
  51.       Rectangle rect = image.getBounds ();
  52.       Rectangle client = shell.getClientArea ();
  53.       hBar.setMaximum (rect.width);
  54.       vBar.setMaximum (rect.height);
  55.       hBar.setThumb (java/lang/Math.java.html" target="_blank">Math.min (rect.width, client.width));
  56.       vBar.setThumb (java/lang/Math.java.html" target="_blank">Math.min (rect.height, client.height));
  57.       int hPage = rect.width - client.width;
  58.       int vPage = rect.height - client.height;
  59.       int hSelection = hBar.getSelection ();
  60.       int vSelection = vBar.getSelection ();
  61.       if (hSelection >= hPage) {
  62.         if (hPage <= 0) hSelection = 0;
  63.         origin.x = -hSelection;
  64.       }
  65.       if (vSelection >= vPage) {
  66.         if (vPage <= 0) vSelection = 0;
  67.         origin.y = -vSelection;
  68.       }
  69.       shell.redraw ();
  70.     }
  71.   });
  72.   shell.addListener (SWT.Paintnew Listener () {
  73.     public void handleEvent (Event e) {
  74.       GC gc = e.gc;
  75.       gc.drawImage (image, origin.x, origin.y);
  76.       Rectangle rect = image.getBounds ();
  77.       Rectangle client = shell.getClientArea ();
  78.       int marginWidth = client.width - rect.width;
  79.       if (marginWidth > 0) {
  80.         gc.fillRectangle (rect.width, 0, marginWidth, client.height);
  81.       }
  82.       int marginHeight = client.height - rect.height;
  83.       if (marginHeight > 0) {
  84.         gc.fillRectangle (0, rect.height, client.width, marginHeight);
  85.       }
  86.     }
  87.   });
  88.   shell.setSize (200, 150);
  89.   shell.open ();
  90.   while (!shell.isDisposed ()) {
  91.     if (!display.readAndDispatch ()) display.sleep ();
  92.   }
  93.   display.dispose ();
  94. }
上一篇:主从按钮设计

下一篇:SWT实现图片查看

  • 初学者如何开发出一个高质量的J2EE系统
  • Linux下Tomcat的安装
  • NOKIA手机JAVA模拟器相关使用详解
  • 什 么 是 MP3
  • 利用java编写网络通信程序
  • JAVA文件加密器(收藏)
  • 用Groovy Template 生成代码 2nd
  • 一些面向对象的设计法则(4)
  • linux环境下Apache+Resin的配置
  • 设计模式袖珍版 连续转载之 - Builder
  • (原创)当server碰到server,socket连接池简单实现-----第一次在csdn发表文章,多包涵:)
  • 几点有关Web Services的技术学习总结
  • 关于分布式cache的几点认识
  • Nice用户手册(四)
  • 使用Java制作多点发送程序
  • 大道至简-Java之23种模式一点就通
  • 关于javabean的一些文档
  • JDBC访问数据库之代码生成器DaoCodeGen(V0.1)
  • 返回】 【顶部】 【关闭
    Copyright © 2005-2010 www.594k.com All Rights Reserved.
    版权所有:JAVA学习网 备案序号:皖ICP备06004238号