问题描述
程序如下:我想让我输入字符后直接创建一棵树。。该怎么写。。我写的是鼠标点下后树才出现。packagetree;importorg.eclipse.jface.dialogs.InputDialog;importorg.eclipse.swt.SWT;importorg.eclipse.swt.widgets.Display;importorg.eclipse.swt.widgets.Event;importorg.eclipse.swt.widgets.Listener;importorg.eclipse.swt.widgets.Shell;importorg.eclipse.swt.widgets.Tree;importorg.eclipse.swt.widgets.TreeItem;importorg.eclipse.swt.custom.SashForm;importorg.eclipse.swt.widgets.Group;importorg.eclipse.swt.layout.FillLayout;importorg.eclipse.swt.widgets.Menu;importorg.eclipse.swt.widgets.MenuItem;importorg.eclipse.swt.events.SelectionAdapter;importorg.eclipse.swt.events.SelectionEvent;importorg.eclipse.ui.forms.widgets.FormToolkit;importorg.eclipse.swt.events.MouseAdapter;importorg.eclipse.swt.events.MouseEvent;publicclassTestextendsShell{/***Launchtheapplication.*@paramargs*/publicintm;publicStrings;privatefinalFormToolkitformToolkit=newFormToolkit(Display.getDefault());publicstaticvoidmain(Stringargs[]){try{Displaydisplay=Display.getDefault();finalShellshell=newTest(display);shell.open();shell.layout();while(!shell.isDisposed()){if(!display.readAndDispatch()){display.sleep();}}}catch(Exceptione){e.printStackTrace();}}/***Createtheshell.*@paramdisplay*/publicTest(Displaydisplay){super(display,SWT.SHELL_TRIM);setLayout(newFillLayout(SWT.HORIZONTAL));{SashFormsashForm=newSashForm(this,SWT.NONE);{finalGroupg=newGroup(sashForm,SWT.NONE);g.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseDown(MouseEvente){while(s!=null){Treetree=formToolkit.createTree(g,SWT.NONE);tree.setBounds(0,10,85,85);formToolkit.paintBordersFor(tree);finalTreeItemroot=newTreeItem(tree,SWT.NULL);root.setText(s);TreeItemr1=newTreeItem(root,SWT.NULL);r1.setText("协议");TreeItemr2=newTreeItem(root,SWT.NULL);r2.setText("服务");TreeItemr3=newTreeItem(root,SWT.NULL);r3.setText("环境");s=null;}}});}{Groupgroup=newGroup(sashForm,SWT.NONE);}sashForm.setWeights(newint[]{1,1});}{Menumenu=newMenu(this,SWT.BAR);setMenuBar(menu);{MenuItemmenuItem=newMenuItem(menu,SWT.CASCADE);menuItem.setText("NewSubMenu");{Menumenu_1=newMenu(menuItem);menuItem.setMenu(menu_1);{MenuItemm=newMenuItem(menu_1,SWT.NONE);m.addSelectionListener(newSelectionAdapter(){@OverridepublicvoidwidgetSelected(SelectionEvente){InputDialogdlg=newInputDialog(getShell(),"新建文件","请输入文件名",null,null);if(dlg.open()==InputDialog.OK){s=dlg.getValue();};}});m.setText("new");}}}}createContents();}/***Createcontentsoftheshell.*/protectedvoidcreateContents(){setText("SWTApplication");setSize(450,300);}@OverrideprotectedvoidcheckSubclass(){//DisablethecheckthatpreventssubclassingofSWTcomponents}}
解决方案
解决方案二:
怎么回事?我运行了一下,但没看到什么结果,是不我哪里弄错了?
解决方案三:
你直接把创建Tree的代码放到dlg.open()下面就可以了if(dlg.open()==InputDialog.OK){s=dlg.getValue();if(s!=null){Treetree=formToolkit.createTree(g,SWT.NONE);tree.getParent().setLayout(newGridLayout());tree.setLayoutData(newGridData(GridData.FILL_BOTH));//tree.setBounds(0,10,85,85);formToolkit.paintBordersFor(tree);finalTreeItemroot=newTreeItem(tree,SWT.NULL);root.setText(s);TreeItemr1=newTreeItem(root,SWT.NULL);r1.setText("协议");TreeItemr2=newTreeItem(root,SWT.NULL);r2.setText("服务");TreeItemr3=newTreeItem(root,SWT.NULL);r3.setText("环境");s=null;}}
不知道你要的是不是这个效果
解决方案四:
不好意思,改了下代码,上面的有问题那个g,你可以把Group定义到类级别的,就可以访问了if(dlg.open()==InputDialog.OK){s=dlg.getValue();if(s!=null){Treetree=formToolkit.createTree(g,SWT.NONE);tree.setBounds(0,10,85,85);formToolkit.paintBordersFor(tree);finalTreeItemroot=newTreeItem(tree,SWT.NULL);root.setText(s);TreeItemr1=newTreeItem(root,SWT.NULL);r1.setText("协议");TreeItemr2=newTreeItem(root,SWT.NULL);r2.setText("服务");TreeItemr3=newTreeItem(root,SWT.NULL);r3.setText("环境");s=null;}}