EclipseRCP開發3-建立自訂菜單

來源:互聯網
上載者:User

import org.eclipse.jface.action.Action;import org.eclipse.ui.IWorkbenchWindow;import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;public class AboutAction extends Action implements IWorkbenchAction {private IWorkbenchWindow workbenchWindow;public AboutAction(IWorkbenchWindow window){if(window==null){throw new IllegalArgumentException();}this.workbenchWindow=window;}public void run(){if(workbenchWindow !=null){AboutDialog ad=new AboutDialog(workbenchWindow.getShell());ad.open();}}@Overridepublic void dispose() {}}

在上面的run方法中我用到了一個AboutDialog,這個類是一個表單類,具體實現為:

import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Dialog;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Shell;public class AboutDialog extends Dialog{protected Shell shell;private int result;public AboutDialog(Shell parent) {this(parent,SWT.NONE);}public AboutDialog(Shell parent,int style){super(parent,style);}public int open(){createContents();shell.open();shell.layout();Display display=getParent().getDisplay();while(!shell.isDisposed()){if(!display.readAndDispatch())display.sleep();}return result;}protected void createContents(){shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);shell.setSize(400, 150);shell.setText("關於 ");Label label2 =new Label(shell,SWT.NONE);label2.setText("自訂菜單");label2.setBounds(70, 50, 300,20);}}

下面就是和前面部落格講解的那樣註冊Action和實現FillMenuBar。

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {private IWorkbenchAction aboutAction;public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {super(configurer);}protected void makeActions(IWorkbenchWindow window) {aboutAction = new AboutAction(window);aboutAction.setId("test.about");aboutAction.setText("關於");register(aboutAction);}protected void fillMenuBar(IMenuManager menuBar) {menuBar.add(createHelpMenu());}private MenuManager createHelpMenu() {MenuManager menu = new MenuManager("About");menu.add(aboutAction);return menu;}}

實現這些代碼之後我們就可以運行了。之後就會出現一下的表單:



這樣自訂菜單就完成了。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.