對這種需求描述不清楚,,先上圖,! 簡單說就是左邊一棵功能樹,,然后控制右面板的響應的功能面板,。 1.主面板,不多說,就SashForm做左右分欄處理,。 - package com.fox.leftright;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.custom.SashForm;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
-
-
-
-
- public class MainApp {
- protected Shell shell;
-
- static LeftCom left ;
-
- static RightCom right ;
-
-
-
-
-
-
- public static void main(String[] args) {
- try {
- MainApp window = new MainApp();
- window.open();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
- public void open() {
- final Display display = Display.getDefault();
- createContents();
- shell.open();
- shell.layout();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- }
-
-
-
- protected void createContents() {
- shell = new Shell();
- shell.setSize(800, 600);
- shell.setText("huangfox_leftRight");
-
- FillLayout insLO = new FillLayout();
- this.shell.setLayout(insLO);
-
-
- final SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);
- sashForm.setLayout(new FillLayout());
-
- left = new LeftCom(sashForm , SWT.NONE );
- right = new RightCom(sashForm, SWT.NONE);
-
- sashForm.setWeights(new int[] { 30 , 70 });
-
-
- }
- }
注意:這里用到兩個靜態(tài)對象,主要也就是用到rightCom,,因為在功能樹控制的時候需要獲得右面板的對象,,對其進行處理。 2.右面板 - package com.fox.leftright;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.widgets.Composite;
-
-
-
-
-
- public class RightCom extends Composite {
-
-
-
-
-
- public RightCom(Composite parent, int style) {
- super(parent, style);
- this.setLayout(new FillLayout());
-
- }
- @Override
- protected void checkSubclass() {
-
- }
- }
注意:右面板要設定布局,,否則功能面板不能正常顯示,。 this.setLayout(new FillLayout()); 3.具體的一個功能面板 - package com.fox.leftright;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.layout.GridData;
- import org.eclipse.swt.layout.GridLayout;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.TabFolder;
- import org.eclipse.swt.widgets.TabItem;
- import com.swtdesigner.SWTResourceManager;
- public class RightComA extends Composite {
-
-
-
-
-
- public RightComA(Composite parent, int style) {
- super(parent, style);
- setLayout(new GridLayout());
- setBackground(SWTResourceManager.getColor(255, 255, 230));
- final Label label = new Label(this, SWT.NONE);
- label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
- label.setText("功能A");
-
- final TabFolder tabFolder = new TabFolder(this, SWT.NONE);
- tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-
- final TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
- tabItem.setText("子功能1");
- final TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
- tabItem_1.setText("子功能2");
- final TabItem tabItem_2 = new TabItem(tabFolder, SWT.NONE);
- tabItem_2.setText("子功能3");
-
- }
- @Override
- protected void checkSubclass() {
-
- }
- }
右邊的功能樹,主要是控制功能面板,。 4.左面板——功能樹 - package com.fox.leftright;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.MouseAdapter;
- import org.eclipse.swt.events.MouseEvent;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.graphics.Point;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Control;
- import org.eclipse.swt.widgets.Tree;
- import org.eclipse.swt.widgets.TreeItem;
- import com.swtdesigner.SWTResourceManager;
-
-
-
-
-
- public class LeftCom extends Composite {
- private Tree tree;
-
-
-
-
-
- public LeftCom(Composite parent, int style) {
- super(parent, style);
- this.setLayout(new FillLayout());
-
-
- tree = new Tree(this, SWT.BORDER);
- tree.setBackground(SWTResourceManager.getColor(255, 232, 232));
-
-
- TreeItem ti = new TreeItem(tree, SWT.NONE);
- ti.setText("功能節(jié)點");
- TreeItem ti_a = new TreeItem(ti, SWT.NONE);
- ti_a.setText("A");
- TreeItem ti_b = new TreeItem(ti, SWT.NONE);
- ti_b.setText("B");
- TreeItem ti_c = new TreeItem(ti, SWT.NONE);
- ti_c.setText("C");
-
- ti.setExpanded(true);
-
-
- tree.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
-
- TreeItem selectedTI = (TreeItem) e.item;
-
- RightCom right = MainApp.right;
-
- Control[] rightCs = right.getChildren();
- for (int i = 0; i < rightCs.length; i++) {
- rightCs[i].dispose();
- }
-
- String selStr = selectedTI.getText();
- System.out.println(selStr);
- if(selStr.equalsIgnoreCase("A")){
-
- RightComA comA = new RightComA(right, SWT.NONE);
- right.layout();
- }else if(selStr.equalsIgnoreCase("B")){
- RightComB comB = new RightComB(right, SWT.NONE);
- right.layout();
- }else if(selStr.equalsIgnoreCase("C")){
- RightComC comC = new RightComC(right, SWT.NONE);
- right.layout();
- }
- }
- });
-
-
- }
- @Override
- protected void checkSubclass() {
-
- }
- }
代碼中有比較詳細的注釋,,主要就是樹的事件處理,。 5.附: 功能面板B - package com.fox.leftright;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.layout.GridData;
- import org.eclipse.swt.layout.GridLayout;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.Table;
- import org.eclipse.swt.widgets.TableColumn;
- import com.swtdesigner.SWTResourceManager;
- public class RightComB extends Composite {
- private Table table;
-
-
-
-
-
- public RightComB(Composite parent, int style) {
- super(parent, style);
- setLayout(new GridLayout());
- setBackground(SWTResourceManager.getColor(223, 255, 223));
- final Label label = new Label(this, SWT.NONE);
- label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
- label.setText("功能B");
- table = new Table(this, SWT.BORDER);
- table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- table.setLinesVisible(true);
- table.setHeaderVisible(true);
- final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
- newColumnTableColumn.setWidth(100);
- newColumnTableColumn.setText("信息項1");
- final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
- newColumnTableColumn_1.setWidth(100);
- newColumnTableColumn_1.setText("信息項2");
- final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
- newColumnTableColumn_2.setWidth(100);
- newColumnTableColumn_2.setText("信息項3");
-
- }
- @Override
- protected void checkSubclass() {
-
- }
- }
功能面板C - package com.fox.leftright;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.browser.Browser;
- import org.eclipse.swt.layout.GridData;
- import org.eclipse.swt.layout.GridLayout;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Label;
- import com.swtdesigner.SWTResourceManager;
- public class RightComC extends Composite {
-
-
-
-
-
- public RightComC(Composite parent, int style) {
- super(parent, style);
- setLayout(new GridLayout());
- setBackground(SWTResourceManager.getColor(225, 240, 255));
- final Label label = new Label(this, SWT.NONE);
- label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
- label.setText("功能C");
- final Browser browser = new Browser(this, SWT.NONE);
- browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- browser.setUrl("http://www.");
-
- }
- @Override
- protected void checkSubclass() {
-
- }
- }
|