2013-02-28 139 views
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滾動僅小區(C1,C2 ...),行標題2 ... etc..unchanged。當用戶點擊上/下或左箭頭/右箭頭按鈕,單擊並拖動滾動滑塊,點擊 下面

enter image description here

也讓見快照我知道我們如何能夠檢測到滾動條的鼠標事件,即拇指和右箭頭或左箭頭按鈕之間的區域?

+0

爲什麼不使用一些現有的實用程序的固定行標題的表?你看過[星雲項目](http://www.eclipse.org/nebula/)嗎? – 2013-02-28 12:18:37

回答

0

是的,你可以做到這一點,但就像你完全自定義繪製表格一樣,你也必須管理滾動效果。您可以將偵聽器添加到滾動條,並使用hBar.addSelectionListener()收聽用戶對滾動位置所做的任何更改。您需要在滾動條上(垂直和水平)設置最小,最大,頁面增量和縮略圖大小,以確保實際體驗。

+0

您能否提供一些代碼片段來滾動部分畫布和滾動條監聽器 – 2013-03-01 06:54:41

+0

這裏有相當多的片段:http://eclipse.org/swt/snippets/。你也許可以看看這個:http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/片段/ Snippet48.java – 2013-03-01 07:32:07