【SWT组件】内容滚动组件 ScrolledComposite

目的

记录 ScrolledComposite 使用方法,总结重要。

实践

ScrolledComposite 类注解出给出了一个Demo,其代码如下:

package com.xzbd.swt01.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;




public class CompositeTest {

	public static void main (String [] args) {
	      Display display = new Display ();
	      Color red = display.getSystemColor(SWT.COLOR_RED);
	      Color blue = display.getSystemColor(SWT.COLOR_BLUE);
	      Shell shell = new Shell (display);
	      shell.setLayout(new FillLayout());

	      // set the size of the scrolled content - method 1
	      final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
	      final Composite c1 = new Composite(sc1, SWT.NONE);
	      sc1.setContent(c1);
	      c1.setBackground(red);
	      GridLayout layout = new GridLayout();
	      layout.numColumns = 1;
	      c1.setLayout(layout);
	      Button b1 = new Button (c1, SWT.PUSH);
	      b1.setText("first button");
	      c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));

	      // set the minimum width and height of the scrolled content - method 2
	      final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
	      sc2.setExpandHorizontal(true);
	      sc2.setExpandVertical(true);
	      final Composite c2 = new Composite(sc2, SWT.NONE);
	      sc2.setContent(c2);
	      c2.setBackground(blue);
	      layout = new GridLayout();
	      layout.numColumns = 1;
	      c2.setLayout(layout);
	      Button b2 = new Button (c2, SWT.PUSH);
	      b2.setText("first button");
	      sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));

	      Button add = new Button (shell, SWT.PUSH);
	      add.setText("add children");
	      final int[] index = new int[]{0};
	      add.addListener(SWT.Selection, new Listener() {
	          public void handleEvent(Event e) {
	              index[0]++;
	              Button button = new Button(c1, SWT.PUSH);
	              button.setText("button "+index[0]);
	              // reset size of content so children can be seen - method 1
	              c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	              c1.layout();

	              button = new Button(c2, SWT.PUSH);
	              button.setText("button "+index[0]);
	              // reset the minimum width and height so children can be seen - method 2
	              sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	              c2.layout();
	          }
	      });

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

	}

总结

demo给出了两个内容滚动的案例,分别展示了设置内容容器大小和设置最小尺寸两种方法,其中重要的方法有:

  • 实例ScrolledComposite时在style中指定横向或纵向可滚动 SWT.H_SCROLL SWT.V_SCROLL
  • 创建一个子内容并指定给 ScrolledComposite
Composite c1 = new Composite(sc1, SWT.NONE);
sc1.setContent(c1);
  • ScrolledComposite的内容发生变化时要及时更新内容容器,或ScrolledComposite 本身。
// reset size of content so children can be seen - method 1
c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c1.layout();

// reset the minimum width and height so children can be seen - method 2
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c2.layout();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值