org.eclipse.swt.widgets.MenuItem怎么设置一个退出的功能

就是点击这个菜单的时候退出当前程序... 就是点击这个菜单的时候退出当前程序 展开
 我来答
育知同创教育
2016-02-23 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
主要是重载ApplicationWorkbenchWindowAdvisor.java中的方法:
/**
* ApplicationWorkbenchWindowAdvisor.java
*/
package com.mi.pwdgate;

import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import com.mi.pwdgate.util.SystemTrayMaster;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

private SystemTrayMaster trayMaster;
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowOpen()
*/
public void postWindowOpen() {
createSystemTray();
}

private void createSystemTray() {
trayMaster = new SystemTrayMaster();
trayMaster.createSystemTray();
}

/*
* (non-Javadoc)
* @see org.eclipse.ui.application.WorkbenchWindowAdvisor#preWindowShellClose()
* <span style="color: #ff0000;">按窗口右上角X关闭</span>

*/
public boolean preWindowShellClose() {
System.out.println("shell going to closed!");
trayMaster.minimizeWindow();
return false;
}

/*
* (non-Javadoc)
* @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowClose()
* <span style="color: #ff0000;">所有关闭窗口的事件都从这里走,收尾工作的代码在这里写</span>

*/
public void postWindowClose() {
System.out.println("closed!");
}

}

/**
* SystemTrayMaster.java
*/
package com.mi.pwdgate.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.PlatformUI;

/**
* @author moneyice 2006-5-9
*/
public class SystemTrayMaster implements SelectionListener, Listener {
private final class RestoreWindowListener extends SelectionAdapter {
public void widgetSelected(SelectionEvent e) {
restoreWindow();
}
}

private Menu menu;

private MenuItem[] menuItems = new MenuItem[0];

private RestoreWindowListener restoreWindowListener;

public SystemTrayMaster() {
this.restoreWindowListener = new RestoreWindowListener();
}

// Closes the Application
protected void closeApplication() {
PlatformUI.getWorkbench().close();
}

// click the tray
public void widgetSelected(SelectionEvent e) {
}

// double click the tray
public void widgetDefaultSelected(SelectionEvent e) {
restoreWindow();
}

// Gets system shell
private Shell getShell() {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}

// Minimizes the Window
public void minimizeWindow() {
getShell().setMinimized(true);
getShell().setVisible(false);
}

// Restores the window
protected void restoreWindow() {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getShell();
shell.open();
shell.setMinimized(false);
shell.forceActive();
shell.forceFocus();
}

public void showMenu() {
clearItems();
MenuItem openItem;
MenuItem closeItem;
openItem = new MenuItem(this.menu, SWT.PUSH);
closeItem = new MenuItem(this.menu, SWT.NONE);
closeItem.setText("Close");
closeItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
closeApplication();
}
});
this.menuItems = new MenuItem[] { openItem, closeItem };

openItem.setText("Open Password Gate");
openItem.addSelectionListener(this.restoreWindowListener);
this.menu.setVisible(true);
}

private void clearItems() {
for (int i = 0; i < this.menuItems.length; i++) {
MenuItem item = this.menuItems[i];
item.removeSelectionListener(this.restoreWindowListener);
this.menuItems[i].dispose();
}
}

public void handleEvent(Event event) {
showMenu();
}

public void createSystemTray() {
// Gets system tray
Tray tray = Display.getDefault().getSystemTray();
// Creates tray item
TrayItem item = new TrayItem(tray, SWT.NONE);
item.setText("Password Gate");
item.setToolTipText("Password Gate");
// Sets image for tray
Image image = ImageShop.get("default");
item.setImage(image);

item.addSelectionListener(this);
item.addListener(SWT.MenuDetect, this);

menu = new Menu(getShell(), SWT.POP_UP);
}
}
百度网友c2ac8041
2014-12-16 · TA获得超过880个赞
知道小有建树答主
回答量:468
采纳率:100%
帮助的人:260万
展开全部
MenuItem mExit = new MenuItem(menu_1, SWT.NONE);
mExit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.exit(0);
}
});
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
南国孤叶
2014-12-16 · TA获得超过244个赞
知道小有建树答主
回答量:343
采纳率:0%
帮助的人:190万
展开全部
System.exit(0);//关闭整个程序,就连其余线程都要死。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式