3

キャンバスにいくつかのテキストと長方形を描画しました。

    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.widgets.Canvas;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.ScrollBar;
    import org.eclipse.swt.widgets.Shell;

    public class Test2 {
        protected static final int Y_STEP = 20;
        static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND
                | SWT.H_SCROLL | SWT.CLOSE | SWT.V_SCROLL | 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(300, 200);

            final Canvas canvas = new Canvas(shell, canvasStyle);
            canvas.setLayout(new FillLayout());
            canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

            final Point origin = new Point(10, 20);
            final ScrollBar hBar = shell.getHorizontalBar();
            Rectangle size = canvas.getBounds();
            hBar.setMaximum(size.width);
            hBar.setMinimum(0);

            final ScrollBar vBar = shell.getVerticalBar();
            hBar.setMaximum(size.height);
            hBar.setMinimum(0);

            // Create a paint handler for the canvas
            canvas.addPaintListener(new PaintListener() {
                @Override
                public void paintControl(PaintEvent e) {
                    // Do some drawing
                    e.gc.drawString("Rows", origin.x, origin.y);
                    e.gc.drawString("Data", 120, 20);
                    for (int i = 0; i < 10; i++) {
                        e.gc.drawString("Row Header" + (i + 1), origin.x, origin.y
                                + (i + 1) * Y_STEP);
                        for (int j = 0; j < 10; j++) {
                            e.gc.drawRectangle(origin.x + 110 + (j * 20), origin.y
                                    + (i + 1) * Y_STEP, 20, 20);
                            e.gc.drawString("C" + (j + 1), origin.x + 110 + 2
                                    + (j * 20), origin.y + (i + 1) * Y_STEP + 1);
                        }
                    }

                }

            });

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

        }
    }

行ヘッダー 1、行ヘッダー 2 ...などを変更せずに、水平スクロールバーをドラッグしてセル (C1、C2 ...) のみをスクロールすることは可能ですか? 以下のスナップショットを参照してください

ここに画像の説明を入力

また、スクロールバーでマウスイベントを検出する方法を教えてください。つまり、ユーザーが上下または左矢印/右矢印ボタンをクリックしたとき、親指をクリックしてドラッグしたとき、親指と右矢印または左矢印ボタンの間の領域をクリックしたときです。

4

1 に答える 1

0

はい、これは可能ですが、テーブルを完全にカスタム描画するのと同じように、スクロール効果も管理する必要があります。スクロールバーにリスナーを追加し、を使用してユーザーがスクロール位置に加えた変更を聞くことができますhBar.addSelectionListener()。リアルなエクスペリエンスを保証するには、スクロールバー(垂直方向と水平方向の両方)で最小、最大、ページ増分、およびサムサイズを設定する必要があります。

于 2013-02-28T12:15:56.483 に答える