SWT之ScrolledComposite

  1. import org.eclipse.swt.SWT;  
  2. import org.eclipse.swt.custom.ScrolledComposite;  
  3. import org.eclipse.swt.graphics.Color;  
  4. import org.eclipse.swt.layout.FillLayout;  
  5. import org.eclipse.swt.layout.GridLayout;  
  6. import org.eclipse.swt.widgets.Button;  
  7. import org.eclipse.swt.widgets.Composite;  
  8. import org.eclipse.swt.widgets.Display;  
  9. import org.eclipse.swt.widgets.Event;  
  10. import org.eclipse.swt.widgets.Listener;  
  11. import org.eclipse.swt.widgets.Shell;  
  12. public class ss {  
  13.     public static void main(String[] args) {  
  14.         Display display = new Display();  
  15.         Color red = display.getSystemColor(SWT.COLOR_RED);  
  16.         Color blue = display.getSystemColor(SWT.COLOR_BLUE);  
  17.         Shell shell = new Shell(display);  
  18.         shell.setLayout(new FillLayout());  
  19.         // set the size of the scrolled content - method 1  
  20.         final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL  
  21.                 | SWT.V_SCROLL | SWT.BORDER);  
  22.         final Composite c1 = new Composite(sc1, SWT.NONE);  
  23.         sc1.setContent(c1);  
  24.         c1.setBackground(red);  
  25.         GridLayout layout = new GridLayout();  
  26.         layout.numColumns = 4;  
  27.         c1.setLayout(layout);  
  28.         Button b1 = new Button(c1, SWT.PUSH);  
  29.         b1.setText("first button");  
  30.         /* 
  31.          * 这是两种用法中的一种,效果是:根据已经存在的组件,计算需要的composite的区域的大小 
  32.          * 即运行结果中红色显示的部分。 
  33.          */  
  34.         c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));  
  35.           
  36.         // set the minimum width and height of the scrolled content - method 2  
  37.         final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL  
  38.                 | SWT.V_SCROLL | SWT.BORDER);  
  39.         sc2.setExpandHorizontal(true);  
  40.         sc2.setExpandVertical(true);  
  41.         final Composite c2 = new Composite(sc2, SWT.NONE);  
  42.         sc2.setContent(c2);  
  43.         c2.setBackground(blue);  
  44.         layout = new GridLayout();  
  45.         layout.numColumns = 4;  
  46.         c2.setLayout(layout);  
  47.         Button b2 = new Button(c2, SWT.PUSH);  
  48.         b2.setText("first button");  
  49.         /* 
  50.          * 这是第二种用法,整个composite区域是用到的Composite区域 
  51.          */  
  52.         sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));  
  53.         Button add = new Button(shell, SWT.PUSH);  
  54.         add.setText("add children");  
  55.         final int[] index = new int[] { 0 };  
  56.         add.addListener(SWT.Selection, new Listener() {  
  57.             public void handleEvent(Event e) {  
  58.                 index[0]++;  
  59.                 Button button = new Button(c1, SWT.PUSH);  
  60.                 button.setText("button " + index[0]);  
  61.                 // reset size of content so children can be seen - method 1  
  62.                 c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));  
  63.                 c1.layout();  
  64.                 button = new Button(c2, SWT.PUSH);  
  65.                 button.setText("button " + index[0]);  
  66.                 // reset the minimum width and height so children can be seen -  
  67.                 // method 2  
  68.                 sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));  
  69.                 c2.layout();  
  70.             }  
  71.         });  
  72.         shell.open();  
  73.         while (!shell.isDisposed()) {  
  74.             if (!display.readAndDispatch())  
  75.                 display.sleep();  
  76.         }  
  77.         display.dispose();  
  78.     }  
  79. }  

posted on 2013-12-24 22:49  okuc  阅读(545)  评论(0编辑  收藏  举报

导航