此程序演示了如何在窗体上绘制图片,以及滚动条的使用方法。 代码十分简单,我就因此不做过多的解释了。 本代码在Windows XP pro, Eclipse 2.1下测试通过。 运行此程序需要进行一些有别于普通Java Application的地方,请参见我的另一篇文章,在《Eclipse Workbench之外使用SWT和JFace》。 如果文中有不妥之处,还请指正。
-
- import org.eclipse.swt.*;
- import org.eclipse.swt.graphics.*;
- import org.eclipse.swt.widgets.*;
-
- public class d {
-
- public static void main (java/lang/String.java.html" target="_blank">String [] args) {
- Display display = new Display ();
- final Shell shell = new Shell (display,
- SWT.SHELL_TRIM | SWT.NO_BACKGROUND |
- SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
- Image originalImage = null;
- FileDialog dialog = new FileDialog (shell, SWT.OPEN);
- dialog.setText ("选择一个图片文件或者取消");
- java/lang/String.java.html" target="_blank">String string = dialog.open ();
- if (string != null) {
- originalImage = new Image (display, string);
- }
- if (originalImage == null) {
- int width = 150, height = 200;
- originalImage = new Image (display, width, height);
- GC gc = new GC (originalImage);
- gc.fillRectangle (0, 0, width, height);
- gc.drawLine (0, 0, width, height);
- gc.drawLine (0, height, width, 0);
- gc.drawText ("默认图片", 10, 10);
- gc.dispose ();
- }
- final Image image = originalImage;
- final Point origin = new Point (0, 0);
- final ScrollBar hBar = shell.getHorizontalBar ();
- hBar.addListener (SWT.Selection, new Listener () {
- public void handleEvent (Event e) {
- int hSelection = hBar.getSelection ();
- int destX = -hSelection - origin.x;
- origin.x = -hSelection;
- Rectangle rect = image.getBounds ();
- shell.scroll (destX, 0, 0, 0, rect.width, rect.height, false);
- }
- });
- final ScrollBar vBar = shell.getVerticalBar ();
- vBar.addListener (SWT.Selection, new Listener () {
- public void handleEvent (Event e) {
- int vSelection = vBar.getSelection ();
- int destY = -vSelection - origin.y;
- origin.y = -vSelection;
- Rectangle rect = image.getBounds ();
- shell.scroll (0, destY, 0, 0, rect.width, rect.height, false);
- }
- });
- shell.addListener (SWT.Resize, new Listener () {
- public void handleEvent (Event e) {
- Rectangle rect = image.getBounds ();
- Rectangle client = shell.getClientArea ();
- hBar.setMaximum (rect.width);
- vBar.setMaximum (rect.height);
- hBar.setThumb (java/lang/Math.java.html" target="_blank">Math.min (rect.width, client.width));
- vBar.setThumb (java/lang/Math.java.html" target="_blank">Math.min (rect.height, client.height));
- int hPage = rect.width - client.width;
- int vPage = rect.height - client.height;
- int hSelection = hBar.getSelection ();
- int vSelection = vBar.getSelection ();
- if (hSelection >= hPage) {
- if (hPage <= 0) hSelection = 0;
- origin.x = -hSelection;
- }
- if (vSelection >= vPage) {
- if (vPage <= 0) vSelection = 0;
- origin.y = -vSelection;
- }
- shell.redraw ();
- }
- });
- shell.addListener (SWT.Paint, new Listener () {
- public void handleEvent (Event e) {
- GC gc = e.gc;
- gc.drawImage (image, origin.x, origin.y);
- Rectangle rect = image.getBounds ();
- Rectangle client = shell.getClientArea ();
- int marginWidth = client.width - rect.width;
- if (marginWidth > 0) {
- gc.fillRectangle (rect.width, 0, marginWidth, client.height);
- }
- int marginHeight = client.height - rect.height;
- if (marginHeight > 0) {
- gc.fillRectangle (0, rect.height, client.width, marginHeight);
- }
- }
- });
- shell.setSize (200, 150);
- shell.open ();
- while (!shell.isDisposed ()) {
- if (!display.readAndDispatch ()) display.sleep ();
- }
- display.dispose ();
- }
-
- }
|