0

我编写了以下代码来解决滚动画布内容中提到的问题。

package com.cavium.test.views;

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.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;

public class Test2 {
    private static final int CYCLE_OFFSET = 120;
    protected static final int Y_STEP = 20;
    static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
    static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display, shellStyle);
        shell.setLayout(new FillLayout());
        shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
        shell.setText("Canvas Test");
        shell.setSize(400, 300);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new GridLayout(1, false));

        final Canvas canvas = new Canvas(composite, canvasStyle);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Point origin = new Point(0, 0);
        final ScrollBar hBar = canvas.getHorizontalBar();
        hBar.setMaximum(2120);
        hBar.setMinimum(0);
        hBar.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                switch (event.detail) {
                case SWT.DRAG:
                    System.out.println("drag");
                    break;
                case SWT.ARROW_DOWN:
                    System.out.println(" arrow down");
                    break;
                case SWT.ARROW_UP:
                    System.out.println("arrow up");
                    break;
                case SWT.ARROW_LEFT:
                    System.out.println("arrow left");
                    break;
                case SWT.ARROW_RIGHT:
                    System.out.println("arrow right");
                    break;
                }
                System.out.println("In hori");
                int hSelection = hBar.getSelection();
                int destX = -hSelection - origin.x;
                System.out.println("origin.x " + origin.x + "hSelection " + hSelection + " destX " + destX);
                canvas.scroll(140 + destX, 0, 140, 0, 2120, 2000, false);
                origin.x = -hSelection;
                System.out.println("origin.x " + origin.x);
            }

        });
        final ScrollBar vBar = canvas.getVerticalBar();
        vBar.setMaximum(2000);
        vBar.setMinimum(0);
        vBar.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                System.out.println("In ver");
                int vSelection = vBar.getSelection();
                int destY = -vSelection - origin.y;
                canvas.scroll(0, destY, 0, 0, 2120, 2000, false);
                origin.y = -vSelection;
            }

        });
        canvas.addListener(SWT.Resize, new Listener() {
            @Override
            public void handleEvent(Event e) {
                System.out.println("In resize");
                Rectangle client = canvas.getClientArea();
                hBar.setMaximum(2120);
                vBar.setMaximum(2000);
                hBar.setThumb(Math.min(2120, client.width));
                hBar.setPageIncrement(Math.min(2120, client.width));
                vBar.setThumb(Math.min(2000, client.height));
                vBar.setPageIncrement(Math.min(2000, client.height));
                hBar.setIncrement(20);
                vBar.setIncrement(20);
                int hPage = 2120 - client.width;
                int vPage = 2000 - 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;
                }
                System.out.println("hPage " + hPage + " hSelection " + hSelection + " origin.x " + origin.x + " origin.y "
                        + origin.y);
                canvas.redraw();
            }
        });
        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {
                System.out.println("In paint");
                System.out.println(origin.x);
                for (int i = 0; i < 100; i++) {
                    e.gc.drawString("Row Header" + i, origin.x, origin.y + i * Y_STEP);
                    for (int j = 0; j < 100; j++) {
                        e.gc.drawRectangle(origin.x + CYCLE_OFFSET + j * 20, origin.y + i * Y_STEP, 20, 20);
                        e.gc.drawString(new Integer(j).toString(), origin.x + CYCLE_OFFSET + 2 + j * 20, origin.y + i * Y_STEP
                                + 1);
                    }
                }

            }

        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();

    }
}

但面临以下问题:

  1. 在水平滚动条侦听器中,无法检测到滚动条上的事件。这里什么都没有打印。
  2. 当我拖动滚动条拇指或单击拇指和左箭头按钮或右箭头按钮或左箭头按钮之间的区域时,代码不起作用。

谁能指出这段代码中的错误?我不明白滚动是如何发生的。有人可以向我提供有关 SWT、Canvas 和滚动的链接、教程/屏幕投射。

4

0 回答 0