SWT中为Compsite显示滚动条效果

SWT 中为 Compsite 显示滚动条效果 

SWT 中 Compsite 使用样式( SWT.H_SCROLL|SWT.V_SCROLL )后只能显示滚动条而无法产生显示效果,后来发现要产生滚动效果必须借助于另外一个类 ScrolledComposite ,它是从 Compsite 继承而来,为 Compsite 提供了较好的滚动显示效果,在 ScrolledComposite 的注释中, SWT 为我们提供了一个示例程序,任何需要使用滚动效果的程序都可以参照该示例来实现。

根据注释可知, ScrolledComposite 中提供了 2 中方式来设置面板的滚动,分别如下:

1)      Set the size of the control that is being scrolled and the ScrolledComposite will show scrollbars when the contained control can not be fully seen.

2)      The second way imitates the way a browser would work.  Set the minimum size of the control and the ScrolledComposite will show scroll bars if the visible area is less than the minimum size of the control and it will expand the size of the control if the visible area is greater than the minimum size.  This requires invoking both setMinWidth(), setMinHeight() and setExpandHorizontal(), setExpandVertical().

注意在使用 ScrolledCompsite 时,不能在其中直接加入组件,而是先声明一个 Compsite 用来容纳你需要构造的一些组件,然后再将该 Compsite 设置为 ScrolledComposite 的内容,具体用法如下:

ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);

  final Composite c1 = new Composite(sc1, SWT.NONE);

//TODO 此处在 c1 中添加组件

//TODO 设置内容面板的大小,当滚动面板无法全部展现内容面板时,将会自动显示滚动条

c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));

     第一种方式是设置内容面板的大小,当滚动面板无法全部展现它时将自动出现滚动条;第二种方式是设置滚动面板自身的最小尺寸(也可以说是需要展现的最小尺寸),当滚动面板的可见区域小于最小尺寸时滚动条自动出现,这种方式跟 Swing 中的 JPanel 的设置滚动的方式的方法 setPreferredSize() 的作用相同。

 

至于在程序中的具体用法,可以参见 ScrolledCompsite 的源码,在注释中有一个 example, 虽简单但已五脏俱全。


转载自:http://blog.csdn.net/heruiup/article/details/5474732

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值