`

再说SWT中的滚动面板ScrolledComposite实现

 
阅读更多
记得以前写过一篇关于滚动面板的文章 SWT中 ScrolledComposite 滚动面板 “不可用” 等常见问题的简单解释,最近又需要实现一个滚动的composite的效果,
当然还是想到了ScrolledComposite,不过看过源码后,对其理解又更加深了一下,其源码的注释中提供的例子代码,是极好的,掠过来看看:
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());//关键点1.外层的容器layout为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);//关键点2.设置Scrolled容器的Content为内层的容器
      c1.setBackground(red);
      GridLayout layout = new GridLayout();
      layout.numColumns = 4;
      c1.setLayout(layout);
      Button b1 = new Button (c1, SWT.PUSH);
      b1.setText("first button");
      c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));//关键点3
      
      // 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 = 4;
      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 ();
 }


有了这个例子(注意有两种不同的实现方式哦),正确快速的实现滚动面板,那就是三下五除二的事情了。最后,还是那句话,源码是最好的文档,多多看开源项目的源码,尤其是Eclipse的,这样不但能更快的了解其Api,而且其中的好多设计方法和设计思想也是值得我们借鉴和学习的。
分享到:
评论

相关推荐

    ScrolledComposite自动出现滚动条

    ScrolledComposite自动出现最适合大小的滚动条代码

    org.eclipse.swt.win32

    org.eclipse.swt.custom.ScrolledComposite.class org.eclipse.swt.custom.ScrolledCompositeLayout.class org.eclipse.swt.custom.StackLayout.class org.eclipse.swt.custom.StyleRange.class org.eclipse.swt....

    Eclipse_Swt_Jface_核心应用_部分19

    6.7 滚动面板(ScrolledComposite) 95 6.7.1 设置滚动条的样式 96 6.7.2 滚动面板的其他方法 97 6.8 本章小结 97 第7章 SWT布局管理器 98 7.1 布局管理器概述 98 7.1.1 绝对定位 98 7.1.2 托管定位 ...

    simple.zip

    swt常有控件入门demo Button1.java Canvas1.java Combo1.java Menu1.java SashForm1.java ScrolledComposite1.java SimpleEditor1.java SimpleEditor2.java Slider1.java TabFolder1.java ToolBarExample.java ...

Global site tag (gtag.js) - Google Analytics