SWT之ScrolledComposite

  1. importorg.eclipse.swt.SWT;
  2. importorg.eclipse.swt.custom.ScrolledComposite;
  3. importorg.eclipse.swt.graphics.Color;
  4. importorg.eclipse.swt.layout.FillLayout;
  5. importorg.eclipse.swt.layout.GridLayout;
  6. importorg.eclipse.swt.widgets.Button;
  7. importorg.eclipse.swt.widgets.Composite;
  8. importorg.eclipse.swt.widgets.Display;
  9. importorg.eclipse.swt.widgets.Event;
  10. importorg.eclipse.swt.widgets.Listener;
  11. importorg.eclipse.swt.widgets.Shell;
  12. publicclassss{
  13. publicstaticvoidmain(String[]args){
  14. Displaydisplay=newDisplay();
  15. Colorred=display.getSystemColor(SWT.COLOR_RED);
  16. Colorblue=display.getSystemColor(SWT.COLOR_BLUE);
  17. Shellshell=newShell(display);
  18. shell.setLayout(newFillLayout());
  19. //setthesizeofthescrolledcontent-method1
  20. finalScrolledCompositesc1=newScrolledComposite(shell,SWT.H_SCROLL
  21. |SWT.V_SCROLL|SWT.BORDER);
  22. finalCompositec1=newComposite(sc1,SWT.NONE);
  23. sc1.setContent(c1);
  24. c1.setBackground(red);
  25. GridLayoutlayout=newGridLayout();
  26. layout.numColumns=4;
  27. c1.setLayout(layout);
  28. Buttonb1=newButton(c1,SWT.PUSH);
  29. b1.setText("firstbutton");
  30. /*
  31. *这是两种用法中的一种,效果是:根据已经存在的组件,计算需要的composite的区域的大小
  32. *即运行结果中红色显示的部分。
  33. */
  34. c1.setSize(c1.computeSize(SWT.DEFAULT,SWT.DEFAULT));
  35. //settheminimumwidthandheightofthescrolledcontent-method2
  36. finalScrolledCompositesc2=newScrolledComposite(shell,SWT.H_SCROLL
  37. |SWT.V_SCROLL|SWT.BORDER);
  38. sc2.setExpandHorizontal(true);
  39. sc2.setExpandVertical(true);
  40. finalCompositec2=newComposite(sc2,SWT.NONE);
  41. sc2.setContent(c2);
  42. c2.setBackground(blue);
  43. layout=newGridLayout();
  44. layout.numColumns=4;
  45. c2.setLayout(layout);
  46. Buttonb2=newButton(c2,SWT.PUSH);
  47. b2.setText("firstbutton");
  48. /*
  49. *这是第二种用法,整个composite区域是用到的Composite区域
  50. */
  51. sc2.setMinSize(c2.computeSize(SWT.DEFAULT,SWT.DEFAULT));
  52. Buttonadd=newButton(shell,SWT.PUSH);
  53. add.setText("addchildren");
  54. finalint[]index=newint[]{0};
  55. add.addListener(SWT.Selection,newListener(){
  56. publicvoidhandleEvent(Evente){
  57. index[0]++;
  58. Buttonbutton=newButton(c1,SWT.PUSH);
  59. button.setText("button"+index[0]);
  60. //resetsizeofcontentsochildrencanbeseen-method1
  61. c1.setSize(c1.computeSize(SWT.DEFAULT,SWT.DEFAULT));
  62. c1.layout();
  63. button=newButton(c2,SWT.PUSH);
  64. button.setText("button"+index[0]);
  65. //resettheminimumwidthandheightsochildrencanbeseen-
  66. //method2
  67. sc2.setMinSize(c2.computeSize(SWT.DEFAULT,SWT.DEFAULT));
  68. c2.layout();
  69. }
  70. });
  71. shell.open();
  72. while(!shell.isDisposed()){
  73. if(!display.readAndDispatch())
  74. display.sleep();
  75. }
  76. display.dispose();
  77. }
  78. }

你可能感兴趣的:(scroll)