posts - 28,  comments - 15,  trackbacks - 0

              本文綜合了GridLayout、GridData、Composite以及Tree、TreeItem構(gòu)造了一個(gè)比較復(fù)雜的應(yīng)用例子。

             /*---------- 構(gòu)造代碼 ---------*/

          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("說(shuō)明");
                 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("網(wǎng)絡(luò)配置");
                 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("網(wǎng)關(guān)配置");
                   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);
           }

          }

          /*----------    動(dòng)作控制接口以及實(shí)現(xiàn)代碼 --------*/

          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("用戶(hù)熟練程度");
            gRadio.setLayoutData(gridData);
            gRadio.setLayout(new RowLayout(SWT.HORIZONTAL));
            
            Button button0 = new Button (gRadio, SWT.RADIO);
            button0.setText("初級(jí)");
            
            Button button1 = new Button (gRadio, SWT.RADIO);
            button1.setText("中級(jí)");
            
            Button button2 = new Button (gRadio, SWT.RADIO);
            button2.setText("高級(jí)");
            
            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;
           }

          }

          /*----------- 測(cè)試代碼 ----------------*/

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


          運(yùn)行結(jié)果:

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

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類(lèi)(17)

          隨筆檔案(28)

          文章分類(lèi)(30)

          文章檔案(30)

          相冊(cè)

          收藏夾(2)

          hibernate

          java基礎(chǔ)

          mysql

          xml

          關(guān)注

          壓力測(cè)試

          算法

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 96364
          • 排名 - 601

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 梁山县| 南华县| 临沧市| 阿拉尔市| 扶沟县| 临汾市| 临猗县| 弥渡县| 靖西县| 湖州市| 界首市| 呼和浩特市| 通辽市| 沧源| 阳西县| 青阳县| 伊宁县| 汝南县| 石泉县| 富锦市| 昌都县| 额尔古纳市| 平阴县| 阳新县| 静海县| 太谷县| 瑞金市| 芜湖县| 玛沁县| 延长县| 靖边县| 平武县| 盐亭县| 水城县| 鄂州市| 奈曼旗| 日土县| 临海市| 城口县| 平远县| 额尔古纳市|