posts - 28,  comments - 15,  trackbacks - 0

              本文綜合了GridLayout、GridData、Composite以及Tree、TreeItem構造了一個比較復雜的應用例子。

             /*---------- 構造代碼 ---------*/

          public class GridLayoutObject {
           
           public void buildLayout(){
            
            Display display = new Display();
            Shell shell = new Shell(display);
            
            GridLayout configLayout = new GridLayout();
            configLayout.marginHeight = 0;
            configLayout.marginWidth = 0;
            
            shell.setLayout(configLayout);
            
            GridData gridData;
            
            SashForm form = new SashForm(shell,SWT.HORIZONTAL);
            gridData = new GridData(GridData.FILL_BOTH);
            form.setLayoutData(gridData);
              
               Composite cLeftSide = new Composite(form, SWT.BORDER);
               gridData = new GridData(GridData.FILL_BOTH);
               cLeftSide.setLayoutData(gridData);
              
               FormLayout layout = new FormLayout();
               cLeftSide.setLayout(layout);
                
               Composite cFilterArea = new Composite(cLeftSide, SWT.NONE);
               cFilterArea.setLayout(new FormLayout());
                
               final Text txtFilter = new Text(cFilterArea, SWT.BORDER);
               txtFilter.setText("hi");
               txtFilter.selectAll();
               txtFilter.setFocus();
              
               Label lblX = new Label(cFilterArea, SWT.WRAP);
               lblX.setText("l1");
              
               Label lblSearch = new Label(cFilterArea, SWT.NONE);
               lblSearch.setText("l2");
              
               Tree tree = new Tree(cLeftSide, SWT.NONE);
               FontData[] fontData = tree.getFont().getFontData();
               fontData[0].setStyle(SWT.BOLD);
               Font filterFoundFont = new Font(display, fontData);
              
               FormData formData;

                 formData = new FormData();
                 /*注意:80,代表*/
                 formData.bottom = new FormAttachment(100, -5);
                 formData.left = new FormAttachment(0, 0);
                 formData.right = new FormAttachment(100, 0);
                 cFilterArea.setLayoutData(formData);
                
                 formData = new FormData();
                 formData.top = new FormAttachment(0,5);
                 formData.left = new FormAttachment(0, 5);
                 lblSearch.setLayoutData(formData);

                 formData = new FormData();
                 formData.top = new FormAttachment(0,5);
                 formData.left = new FormAttachment(lblSearch,5);
                 formData.right = new FormAttachment(lblX, -3);
                 txtFilter.setLayoutData(formData);

                 formData = new FormData();
                 formData.top = new FormAttachment(0,5);
                 formData.right = new FormAttachment(100,-5);
                 lblX.setLayoutData(formData);

                 formData = new FormData();
                 formData.top = new FormAttachment(0, 0);
                 formData.left = new FormAttachment(0,0);
                 formData.right = new FormAttachment(100,0);
                 formData.bottom = new FormAttachment(cFilterArea,-1);
                 tree.setLayoutData(formData);
                
                 Composite cRightSide = new Composite(form, SWT.NULL);
                 configLayout = new GridLayout();
                 configLayout.marginHeight = 3;
                 configLayout.marginWidth = 0;
                 cRightSide.setLayout(configLayout);
            
                 // Header
                 Composite cHeader = new Composite(cRightSide, SWT.BORDER);
                 configLayout = new GridLayout();
                 configLayout.marginHeight = 3;
                 configLayout.marginWidth = 0;
                 configLayout.numColumns = 2;
                 configLayout.marginRight = 5;
                 cHeader.setLayout(configLayout);
                 gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
                 cHeader.setLayoutData(gridData);
            
                 cHeader.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
                 cHeader.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
                
                 Label lHeader = new Label(cHeader, SWT.NULL);
                 lHeader.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
                 lHeader.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
                 fontData = lHeader.getFont().getFontData();
                 fontData[0].setStyle(SWT.BOLD);
                 int fontHeight = (int)(fontData[0].getHeight() * 1.2);
                 fontData[0].setHeight(fontHeight);
                 Font headerFont = new Font(display, fontData);
                 lHeader.setFont(headerFont);
                 gridData = new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.HORIZONTAL_ALIGN_BEGINNING);
                 lHeader.setLayoutData(gridData);
                
                 Label usermodeHint = new Label(cHeader, SWT.NULL);
                 usermodeHint.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
                 usermodeHint.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
                 gridData = new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL);
                 usermodeHint.setLayoutData(gridData);
                
                 Composite cConfigSection = new Composite(cRightSide, SWT.NULL);
                 StackLayout layoutConfigSection = new StackLayout();
                 cConfigSection.setLayout(layoutConfigSection);
                 gridData = new GridData(GridData.FILL_BOTH);
                 gridData.horizontalIndent = 2;
                 cConfigSection.setLayoutData(gridData);
            
            
                 form.setWeights(new int[] {50,80});
                
                 Listener scResizeListener = new Listener() {
              public void handleEvent(Event event) {
               setupSC((ScrolledComposite)event.widget);
              }
             };
                 ScrolledComposite sc = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
                   sc.setExpandHorizontal(true);
                   sc.setExpandVertical(true);
                   sc.setLayoutData(new GridData(GridData.FILL_BOTH));
                  sc.getVerticalBar().setIncrement(16);
                  sc.addListener(SWT.Resize, scResizeListener);
                
                  ModelConfigSection a = new ModelConfigSection();
                  Composite c = a.configSectionCreate(sc);
                  sc.setContent(c);
                 TreeItem item1 = new TreeItem(tree,SWT.NULL);
                 item1.setText("說明");
                 item1.setData("Panel", sc);
                 layoutConfigSection.topControl = sc;
                
                 ScrolledComposite sc1 = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
                    sc1.setExpandHorizontal(true);
                    sc1.setExpandVertical(true);
                    sc1.setLayoutData(new GridData(GridData.FILL_BOTH));
                   sc1.getVerticalBar().setIncrement(16);
                   sc1.addListener(SWT.Resize, scResizeListener);
             
             
                 
                 TreeItem item2 = new TreeItem(tree,SWT.NULL);
                 item2.setText("網絡配置");
                 item2.setData("Panel",sc1);

                 ScrolledComposite sc2 = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
                    sc2.setExpandHorizontal(true);
                    sc2.setExpandVertical(true);
                    sc2.setLayoutData(new GridData(GridData.FILL_BOTH));
                   sc2.getVerticalBar().setIncrement(16);
                   sc2.addListener(SWT.Resize, scResizeListener);
                  
                 TreeItem item21 = new TreeItem(item2,SWT.NULL);
                 item21.setText("路由配置");
                 item21.setData("Panel", sc2);
                
                
                 ScrolledComposite sc3 = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
                    sc3.setExpandHorizontal(true);
                    sc3.setExpandVertical(true);
                    sc3.setLayoutData(new GridData(GridData.FILL_BOTH));
                   sc3.getVerticalBar().setIncrement(16);
                   sc3.addListener(SWT.Resize, scResizeListener);
                 TreeItem item22 = new TreeItem(item2,SWT.NULL);
                 item22.setText("網關配置");
                   item22.setData("Panel", sc3);
                
                 TreeItem[] items = { tree.getItems()[0] };
                 tree.setSelection(items);
                
                 shell.pack();
             shell.open();
             
             while(!shell.isDisposed()){
              if(!display.readAndDispatch()){
               display.sleep();
              }
             }
             
            display.dispose();
           }
           
           public void setupSC(ScrolledComposite sc) {
            Composite c = (Composite) sc.getContent();
            if (c != null) {
             Point size1 = c.computeSize(sc.getClientArea().width, SWT.DEFAULT);
             Point size = c.computeSize(SWT.DEFAULT, size1.y);
             sc.setMinSize(size);
            }
            sc.getVerticalBar().setPageIncrement(sc.getSize().y);
           }

          }

          /*----------    動作控制接口以及實現代碼 --------*/

          public interface ConfigSectionTest {
           public Composite configSectionCreate(final Composite parent);
          }

          public class ModelConfigSection implements ConfigSectionTest {

           public Composite configSectionCreate(Composite parent) {
            GridData gridData;
            GridLayout layout;
            
            final Composite cMode = new Composite(parent,SWT.WRAP);
            gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL|GridData.VERTICAL_ALIGN_FILL);
            cMode.setLayoutData(gridData);
            layout = new GridLayout();
            layout.numColumns =4;
            layout.marginHeight = 0;
            cMode.setLayout(layout);
            
            
            gridData = new GridData();
            gridData.horizontalSpan = 4;
            final Group gRadio = new Group(cMode, SWT.WRAP);
            gRadio.setText("用戶熟練程度");
            gRadio.setLayoutData(gridData);
            gRadio.setLayout(new RowLayout(SWT.HORIZONTAL));
            
            Button button0 = new Button (gRadio, SWT.RADIO);
            button0.setText("初級");
            
            Button button1 = new Button (gRadio, SWT.RADIO);
            button1.setText("中級");
            
            Button button2 = new Button (gRadio, SWT.RADIO);
            button2.setText("高級");
            
            button0.setSelection(true);
            
            gridData = new GridData(GridData.FILL_HORIZONTAL);
               final Label label = new Label(cMode, SWT.WRAP);
               gridData.horizontalSpan = 4;
               label.setLayoutData(gridData);
               label.setText("什么玩意!");
              
              
            return cMode;
           }

          }

          /*----------- 測試代碼 ----------------*/

          public class GridLayoutTest {
           
           public static void main(String[] args){
            
            GridLayoutObject obj = new GridLayoutObject();
            obj.buildLayout();
            
            
           }
          }


          運行結果:

          posted on 2009-08-13 17:27 zhangxl 閱讀(2503) 評論(0)  編輯  收藏 所屬分類: SWT、SWING、AWT

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類(17)

          隨筆檔案(28)

          文章分類(30)

          文章檔案(30)

          相冊

          收藏夾(2)

          hibernate

          java基礎

          mysql

          xml

          關注

          壓力測試

          算法

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 96415
          • 排名 - 601

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 临汾市| 罗源县| 绥江县| 永昌县| 乾安县| 建阳市| 鹤山市| 随州市| 射洪县| 吴川市| 仙游县| 和硕县| 镇平县| 托克托县| 紫阳县| 贵溪市| 迭部县| 昆山市| 云龙县| 大同县| 阳江市| 通州市| 宜昌市| 德钦县| 南开区| 漳平市| 开封市| 桑植县| 香格里拉县| 榆树市| 鄂伦春自治旗| 民乐县| 仁布县| 手游| 昂仁县| 揭西县| 徐汇区| 彰化市| 丰原市| 翁源县| 阳泉市|