Composite的滚动条这样用

   今天有个工作是给一个视图(view)上的Composite添加滚动条,原以为可以这样实现:
     Composite scrolledComposite = new Composite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
   运行后一看,的确是有滚动条,但是滚动条里的Composite根本不会跟着动,一查才知道,swt中有一个专门负责这种效果的composite组件ScrolledComposite, 汗!
   按照ScrolledComposite源码API的说明,有两种使用方式,示例代码如下:
  1.     public static void main(String[] args) {
  2.         Display display = new Display();
  3.         Color red = display.getSystemColor(SWT.COLOR_RED);
  4.         Color blue = display.getSystemColor(SWT.COLOR_BLUE);
  5.         Shell shell = new Shell(display);
  6.         shell.setLayout(new FillLayout());

  7.         // set the size of the scrolled content - method 1
  8.         final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
  9.         final Composite c1 = new Composite(sc1, SWT.NONE);
  10.         sc1.setContent(c1);
  11.         c1.setBackground(red);
  12.         GridLayout layout = new GridLayout();
  13.         layout.numColumns = 4;
  14.         c1.setLayout(layout);
  15.         Button b1 = new Button(c1, SWT.PUSH);
  16.         b1.setText("first button");
  17.         c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));

  18.         // set the minimum width and height of the scrolled content - method 2
  19.         final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
  20.         sc2.setExpandHorizontal(true);
  21.         sc2.setExpandVertical(true);
  22.         final Composite c2 = new Composite(sc2, SWT.NONE);
  23.         sc2.setContent(c2);
  24.         c2.setBackground(blue);
  25.         layout = new GridLayout();
  26.         layout.numColumns = 4;
  27.         c2.setLayout(layout);
  28.         Button b2 = new Button(c2, SWT.PUSH);
  29.         b2.setText("first button");
  30.         sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
  31.         Button add = new Button(shell, SWT.PUSH);
  32.         add.setText("add children");
  33.         final int[] index = new int[] { 0 };
  34.         add.addListener(SWT.Selection, new Listener() {
  35.             public void handleEvent(Event e) {
  36.                 index[0]++;
  37.                 Button button = new Button(c1, SWT.PUSH);
  38.                 button.setText("button " + index[0]);
  39.                 // reset size of content so children can be seen - method 1
  40.                 c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
  41.                 c1.layout();
  42.                 button = new Button(c2, SWT.PUSH);
  43.                 button.setText("button " + index[0]);
  44.                 // reset the minimum width and height so children can be seen - method 2
  45.                 sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
  46.                 c2.layout();
  47.             }
  48.         });
  49.         shell.open();
  50.         while (!shell.isDisposed()) {
  51.             if (!display.readAndDispatch())
  52.                 display.sleep();
  53.         }
  54.         display.dispose();
  55.     }

   现在了解了吧,自己选种方式,照着做就OK了!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值