电竞比分网-中国电竞赛事及体育赛事平台

分享

SWT 功能樹簡(jiǎn)單實(shí)現(xiàn)

 nacy2012 2015-08-07

對(duì)這種需求描述不清楚,先上圖!

 

 

 

簡(jiǎn)單說就是左邊一棵功能樹,然后控制右面板的響應(yīng)的功能面板。

 

1.主面板,不多說,就SashForm做左右分欄處理。

  1. package com.fox.leftright;  
  2. import org.eclipse.swt.SWT;  
  3. import org.eclipse.swt.custom.SashForm;  
  4. import org.eclipse.swt.layout.FillLayout;  
  5. import org.eclipse.swt.widgets.Display;  
  6. import org.eclipse.swt.widgets.Shell;  
  7. /** 
  8.  * @author huangfox 
  9.  *  主面板 
  10.  */  
  11. public class MainApp {  
  12.     protected Shell shell;  
  13.     //左面板  
  14.     static LeftCom left ;  
  15.     //右面板  
  16.     static RightCom right ;   
  17.       
  18.       
  19.     /** 
  20.      * Launch the application 
  21.      * @param args 
  22.      */  
  23.     public static void main(String[] args) {  
  24.         try {  
  25.             MainApp window = new MainApp();  
  26.             window.open();  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.     /** 
  32.      * Open the window 
  33.      */  
  34.     public void open() {  
  35.         final Display display = Display.getDefault();  
  36.         createContents();  
  37.         shell.open();  
  38.         shell.layout();  
  39.         while (!shell.isDisposed()) {  
  40.             if (!display.readAndDispatch())  
  41.                 display.sleep();  
  42.         }  
  43.     }  
  44.     /** 
  45.      * Create contents of the window 
  46.      */  
  47.     protected void createContents() {  
  48.         shell = new Shell();  
  49.         shell.setSize(800600);  
  50.         shell.setText("huangfox_leftRight");  
  51.         //布局  
  52.         FillLayout insLO = new FillLayout();  
  53.         this.shell.setLayout(insLO);  
  54.           
  55.         //左右分欄,左邊功能樹、右邊功能面板  
  56.         final SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);  
  57.         sashForm.setLayout(new FillLayout());  
  58.           
  59.         left = new LeftCom(sashForm , SWT.NONE );  
  60.         right = new RightCom(sashForm, SWT.NONE);  
  61.           
  62.         sashForm.setWeights(new int[] { 30 , 70  });  
  63.           
  64.         //  
  65.     }  
  66. }  

注意:這里用到兩個(gè)靜態(tài)對(duì)象,主要也就是用到rightCom,因?yàn)樵诠δ軜淇刂频臅r(shí)候需要獲得右面板的對(duì)象,對(duì)其進(jìn)行處理。

 

2.右面板

  1. package com.fox.leftright;  
  2. import org.eclipse.swt.layout.FillLayout;  
  3. import org.eclipse.swt.widgets.Composite;  
  4. /** 
  5.  * @author huangfox 
  6.  *  右面板,可以算是一個(gè)母版吧! 
  7.  *  主要是承載具體的功能面板,例如RightComA 、RightComB .... 
  8.  */  
  9. public class RightCom extends Composite {  
  10.     /** 
  11.      * Create the composite 
  12.      * @param parent 
  13.      * @param style 
  14.      */  
  15.     public RightCom(Composite parent, int style) {  
  16.         super(parent, style);  
  17.         this.setLayout(new FillLayout());  
  18.         //  
  19.     }  
  20.     @Override  
  21.     protected void checkSubclass() {  
  22.         // Disable the check that prevents subclassing of SWT components  
  23.     }  
  24. }  

注意:右面板要設(shè)定布局,否則功能面板不能正常顯示。

this.setLayout(new FillLayout());

 

3.具體的一個(gè)功能面板

  1. package com.fox.leftright;  
  2. import org.eclipse.swt.SWT;  
  3. import org.eclipse.swt.layout.GridData;  
  4. import org.eclipse.swt.layout.GridLayout;  
  5. import org.eclipse.swt.widgets.Composite;  
  6. import org.eclipse.swt.widgets.Label;  
  7. import org.eclipse.swt.widgets.TabFolder;  
  8. import org.eclipse.swt.widgets.TabItem;  
  9. import com.swtdesigner.SWTResourceManager;  
  10. public class RightComA extends Composite {  
  11.     /** 
  12.      * Create the composite 
  13.      * @param parent 
  14.      * @param style 
  15.      */  
  16.     public RightComA(Composite parent, int style) {  
  17.         super(parent, style);  
  18.         setLayout(new GridLayout());  
  19.         setBackground(SWTResourceManager.getColor(255255230));  
  20.         final Label label = new Label(this, SWT.NONE);  
  21.         label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, truefalse));  
  22.         label.setText("功能A");  
  23. //      label.setBounds(10, 10, 480, 17);  
  24.         final TabFolder tabFolder = new TabFolder(this, SWT.NONE);  
  25.         tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, truetrue));  
  26. //      tabFolder.setBounds(10, 36, 480, 329);  
  27.         final TabItem tabItem = new TabItem(tabFolder, SWT.NONE);  
  28.         tabItem.setText("子功能1");  
  29.         final TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);  
  30.         tabItem_1.setText("子功能2");  
  31.         final TabItem tabItem_2 = new TabItem(tabFolder, SWT.NONE);  
  32.         tabItem_2.setText("子功能3");  
  33.         //  
  34.     }  
  35.     @Override  
  36.     protected void checkSubclass() {  
  37.         // Disable the check that prevents subclassing of SWT components  
  38.     }  
  39. }  

右邊的功能樹,主要是控制功能面板。

 

 

4.左面板——功能樹

  1. package com.fox.leftright;  
  2. import org.eclipse.swt.SWT;  
  3. import org.eclipse.swt.events.MouseAdapter;  
  4. import org.eclipse.swt.events.MouseEvent;  
  5. import org.eclipse.swt.events.SelectionAdapter;  
  6. import org.eclipse.swt.events.SelectionEvent;  
  7. import org.eclipse.swt.graphics.Point;  
  8. import org.eclipse.swt.layout.FillLayout;  
  9. import org.eclipse.swt.widgets.Composite;  
  10. import org.eclipse.swt.widgets.Control;  
  11. import org.eclipse.swt.widgets.Tree;  
  12. import org.eclipse.swt.widgets.TreeItem;  
  13. import com.swtdesigner.SWTResourceManager;  
  14. /** 
  15.  * @author huangfox 
  16.  * 左面板 
  17.  * 一般可以作成功能節(jié)點(diǎn)樹 
  18.  */  
  19. public class LeftCom extends Composite {  
  20.     private Tree tree;  
  21.     /** 
  22.      * Create the composite 
  23.      * @param parent 
  24.      * @param style 
  25.      */  
  26.     public LeftCom(Composite parent, int style) {  
  27.         super(parent, style);  
  28.         this.setLayout(new FillLayout());  
  29.           
  30.         //定義一顆功能樹  
  31.         tree = new Tree(this, SWT.BORDER);  
  32.         tree.setBackground(SWTResourceManager.getColor(255232232));  
  33.           
  34.         //簡(jiǎn)單構(gòu)造  
  35.         TreeItem ti = new TreeItem(tree, SWT.NONE);  
  36.         ti.setText("功能節(jié)點(diǎn)");  
  37.         TreeItem ti_a = new TreeItem(ti, SWT.NONE);  
  38.         ti_a.setText("A");  
  39.         TreeItem ti_b = new TreeItem(ti, SWT.NONE);  
  40.         ti_b.setText("B");  
  41.         TreeItem ti_c = new TreeItem(ti, SWT.NONE);  
  42.         ti_c.setText("C");  
  43.         //默認(rèn)展開,要在添加完子節(jié)點(diǎn)后設(shè)置,否則失效。  
  44.         ti.setExpanded(true);  
  45.           
  46.         //點(diǎn)擊功能樹,控制右面板的展示  
  47.         tree.addSelectionListener(new SelectionAdapter() {  
  48.             @Override  
  49.             public void widgetSelected(SelectionEvent e) {  
  50.                 //獲得點(diǎn)擊的功能節(jié)點(diǎn)(treeItem)  
  51.                 TreeItem selectedTI = (TreeItem) e.item;  
  52.                 //獲得右面板  
  53.                 RightCom right = MainApp.right;  
  54.                 //銷毀之前面板的內(nèi)容  
  55.                 Control[] rightCs = right.getChildren();  
  56.                 for (int i = 0; i < rightCs.length; i++) {  
  57.                     rightCs[i].dispose();  
  58.                 }  
  59.                 //根據(jù)點(diǎn)擊的功能節(jié)點(diǎn)展示右面板  
  60.                 String selStr = selectedTI.getText();  
  61.                 System.out.println(selStr);  
  62.                 if(selStr.equalsIgnoreCase("A")){  
  63.                     //新建右面板上的內(nèi)容,其實(shí)也是一個(gè)面板(RightComA)!  
  64.                     RightComA comA = new RightComA(right, SWT.NONE);  
  65.                     right.layout();  
  66.                 }else if(selStr.equalsIgnoreCase("B")){  
  67.                     RightComB comB = new RightComB(right, SWT.NONE);  
  68.                     right.layout();  
  69.                 }else if(selStr.equalsIgnoreCase("C")){  
  70.                     RightComC comC = new RightComC(right, SWT.NONE);  
  71.                     right.layout();  
  72.                 }  
  73.             }  
  74.         });  
  75.           
  76.           
  77.     }  
  78.     @Override  
  79.     protected void checkSubclass() {  
  80.         // Disable the check that prevents subclassing of SWT components  
  81.     }  
  82. }  

代碼中有比較詳細(xì)的注釋,主要就是樹的事件處理。

 

5.附:

功能面板B

  1. package com.fox.leftright;  
  2. import org.eclipse.swt.SWT;  
  3. import org.eclipse.swt.layout.GridData;  
  4. import org.eclipse.swt.layout.GridLayout;  
  5. import org.eclipse.swt.widgets.Composite;  
  6. import org.eclipse.swt.widgets.Label;  
  7. import org.eclipse.swt.widgets.Table;  
  8. import org.eclipse.swt.widgets.TableColumn;  
  9. import com.swtdesigner.SWTResourceManager;  
  10. public class RightComB extends Composite {  
  11.     private Table table;  
  12.     /** 
  13.      * Create the composite 
  14.      * @param parent 
  15.      * @param style 
  16.      */  
  17.     public RightComB(Composite parent, int style) {  
  18.         super(parent, style);  
  19.         setLayout(new GridLayout());  
  20.         setBackground(SWTResourceManager.getColor(223255223));  
  21.         final Label label = new Label(this, SWT.NONE);  
  22.         label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, truefalse));  
  23.         label.setText("功能B");  
  24.         table = new Table(this, SWT.BORDER);  
  25.         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, truetrue));  
  26.         table.setLinesVisible(true);  
  27.         table.setHeaderVisible(true);  
  28.         final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);  
  29.         newColumnTableColumn.setWidth(100);  
  30.         newColumnTableColumn.setText("信息項(xiàng)1");  
  31.         final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);  
  32.         newColumnTableColumn_1.setWidth(100);  
  33.         newColumnTableColumn_1.setText("信息項(xiàng)2");  
  34.         final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);  
  35.         newColumnTableColumn_2.setWidth(100);  
  36.         newColumnTableColumn_2.setText("信息項(xiàng)3");  
  37.         //  
  38.     }  
  39.     @Override  
  40.     protected void checkSubclass() {  
  41.         // Disable the check that prevents subclassing of SWT components  
  42.     }  
  43. }  

 

功能面板C

  1. package com.fox.leftright;  
  2. import org.eclipse.swt.SWT;  
  3. import org.eclipse.swt.browser.Browser;  
  4. import org.eclipse.swt.layout.GridData;  
  5. import org.eclipse.swt.layout.GridLayout;  
  6. import org.eclipse.swt.widgets.Composite;  
  7. import org.eclipse.swt.widgets.Label;  
  8. import com.swtdesigner.SWTResourceManager;  
  9. public class RightComC extends Composite {  
  10.     /** 
  11.      * Create the composite 
  12.      * @param parent 
  13.      * @param style 
  14.      */  
  15.     public RightComC(Composite parent, int style) {  
  16.         super(parent, style);  
  17.         setLayout(new GridLayout());  
  18.         setBackground(SWTResourceManager.getColor(225240255));  
  19.         final Label label = new Label(this, SWT.NONE);  
  20.         label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, truefalse));  
  21.         label.setText("功能C");  
  22.         final Browser browser = new Browser(this, SWT.NONE);  
  23.         browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, truetrue));  
  24.         browser.setUrl("http://www.");  
  25.         //  
  26.     }  
  27.     @Override  
  28.     protected void checkSubclass() {  
  29.         // Disable the check that prevents subclassing of SWT components  
  30.     }  
  31. }  


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多