我是想让在点击程序那个关闭按钮后能出现确认消息,但是我添加的监听器总是在界面已经消失后才出现,那样不管你点击确认还是取消都将退出消息。
请问我该往哪里添加消息,或什么消息 shell.addShellListener(new ShellAdapter(){
public void shellClosed(ShellEvent e){
e.doit= MessageDialog.openConfirm(Display.getCurrent.getActiveShell(), "退出", "确认退出");
}
});
请问我该往哪里添加消息,或什么消息 shell.addShellListener(new ShellAdapter(){
public void shellClosed(ShellEvent e){
e.doit= MessageDialog.openConfirm(Display.getCurrent.getActiveShell(), "退出", "确认退出");
}
});
是什么东西要触发这个事件,就跟他本身加监听,你的程序是给shell加了监听,而且是shell被关闭的时候触发,所以就有你说的现象
解决办法:
给你自己new的Button加监听就行了!
比如说
Button closeBtn = new Button(...);
closeBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
...
MessageDialog.openConfirm(Display.getCurrent.getActiveShell(), "退出", "确认退出");
...
}
public void widgetDefaultSelected(SelectionEvent arg0) {
}
});
public void shellIconified(ShellEvent e){
Display.getCurrent().getActiveShell().setVisible(false);
}
});但是关闭还是不行,我重写shellClosed方法,可是这样是在shell已经关闭的情况下,我是想在之前有确认消息,就像eclipse那样提示是否的确认对话框后
才正式退出或不退出,那该重写那个方法呢??
不过你弹对话框的时候有问题。因此你应该象下面这样写
shell.addShellListener(new ShellAdapter(){
public void shellClosed(ShellEvent e){
e.doit= MessageDialog.openConfirm(Display.getCurrent().getActiveShell(),"退出", "确认退出");
}
});
f.setSize(100,100);
f.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
int num = JOptionPane.showConfirmDialog(f, "确定要退出吗?", "提示",
JOptionPane.OK_CANCEL_OPTION);
if (num == 0) {
System.exit(0);
}
}
});
f.setVisible(true);
import org.eclipse.swt.events.ShellListener;
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.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;public class Test { public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Image image = new Image(display, 16, 16); final Tray tray = display.getSystemTray();
final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON); final TrayItem item = new TrayItem(tray, SWT.NONE); toolTip.setText("亲爱的用户,我在这里");
toolTip.setMessage("您看见了么,我我我.....");
item.setToolTip(toolTip);
item.setImage(image); item.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event event) { Shell s = event.display.getShells()[0];
s.setVisible(true);
s.setMinimized(false);
}
}); shell.addShellListener(new ShellListener() {
public void shellDeactivated(org.eclipse.swt.events.ShellEvent e) { } public void shellActivated(org.eclipse.swt.events.ShellEvent e) { } public void shellClosed(org.eclipse.swt.events.ShellEvent e) { } public void shellDeiconified(org.eclipse.swt.events.ShellEvent e) { } public void shellIconified(org.eclipse.swt.events.ShellEvent e) {
((Shell) e.getSource()).setVisible(false);
toolTip.setVisible(true);
} }); shell.setBounds(50, 50, 300, 200);
shell.open(); while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
image.dispose();
display.dispose();
}
}
frame是你的Frame或JFrame类的一个实例。
楼主写的是swt程序吧!怎么把JFrame都搬出来了//
顺便问一句,楼主是在西安上学(xaut)的吗?
也许我们是校友俄,我也在2002届,xaut出来的!
f.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
.....
}
});
你使用Display.getCurrent().getActiveShell()方法是会出现异常的。各位,我写的是swt程序,用到的是JFACE,不是SWING!!
你使用Display.getCurrent().getActiveShell()方法是会出现异常的。各位,我写的是swt程序,用到的是JFACE,不是SWING!!
恩,我是从xaut毕业的,03级
这个方法在纯粹的SWT环境没问题,但是在jface下却是已经消失了主界面后才出现对话框的。
还有最小化方法我在之前的4楼已经回复,我现在需要的是重写关闭的,可是上述方法在Jface下并没有出现该有的结果
这句这么写:e.doit=MessageDialog.openConfirm(null,"退出","确认退出");其他的不变,就ok了!要求结账,给分!
null应该是创建一个新的窗口.(解释的不一定正确)但肯定已经解决你的问题了.请照着写你测试一下再说? ok?
给你一个实例,你看看,我已经试验成功的阿!肯定不会有问题。src------------------------------startpackage swt;import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.*;public class ShellCloseConfirm {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setVisible(true);
shell.setSize(200, 200);
shell.open();
shell.addShellListener(new ShellAdapter() {
public void shellClosed(ShellEvent e) {
e.doit = MessageDialog.openConfirm(Display.getCurrent()
.getActiveShell(), "quit", "ok");
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
src------------------------------end
看明白我说什么,OK?我早说了SWT中那样写正确,可我是要求写jface程序。import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test extends ApplicationWindow { /**
* Create the application window
*/
public Test() {
super(null);
createActions();
addToolBar(SWT.FLAT | SWT.WRAP);
addMenuBar();
addStatusLine();
} /**
* Create contents of the application window
* @param parent
*/
@Override
protected Control createContents(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
getShell().addShellListener(new ShellAdapter(){
public void shellClosed(ShellEvent e){
e.doit=MessageDialog.openConfirm(getShell(), "确定", "确认退出?");
}
});
//
return container;
} /**
* Create the actions
*/
private void createActions() {
// Create the actions
} /**
* Create the menu manager
* @return the menu manager
*/
@Override
protected MenuManager createMenuManager() {
MenuManager menuManager = new MenuManager("menu");
return menuManager;
} /**
* Create the toolbar manager
* @return the toolbar manager
*/
@Override
protected ToolBarManager createToolBarManager(int style) {
ToolBarManager toolBarManager = new ToolBarManager(style);
return toolBarManager;
} /**
* Create the status line manager
* @return the status line manager
*/
@Override
protected StatusLineManager createStatusLineManager() {
StatusLineManager statusLineManager = new StatusLineManager();
statusLineManager.setMessage(null, "");
return statusLineManager;
} /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Test window = new Test();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Configure the shell
* @param newShell
*/
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("New Application");
} /**
* Return the initial size of the window
*/
@Override
protected Point getInitialSize() {
return new Point(500, 375);
}}
你试试会是象你要求的那样不????
[Note that in order to prevent recursive calls to this method it does not call Shell#close(). As a result ShellListeners will not receive a shellClosed event. ]
在你的Test类里面覆盖ApplicationWindow 的close()方法,追加下面的代码就可以实现。 public boolean close() {
getShell().close();
return true;
}请参考:
http://help.eclipse.org/stable/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/window/ApplicationWindow.html#close()
return false;
}如上述,然后在添加监听器方法就对了。protected Control createContents(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
getShell().addShellListener(new ShellAdapter(){
public void shellClosed(ShellEvent e){
e.doit=MessageDialog.openConfirm(getShell(), "", "");
}
});
//
return container;
}
就对了。十分感谢!!
不用swt 呵呵.
JFace 哪来的啊?
无语了...
你自己去试试就知道怎么回事了。不要没试乱叫什么,不重写close。你添加监听器看看?没试不要乱叫,OK??
先去试试jface和swt的区别再来说话,OK??
我一早就说过,用swt的shell添加监听器确实可以如希望的那样,用Jface你试试,程序在26楼贴出来了。你复制看看。别乱喊什么概念之类的东西,OK??
请注意:
http://help.eclipse.org/stable/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/window/ApplicationWindow.html#close()里提示的内容,重写close方法去调用shell的close方法是有可能导致递归调用的!在某的环境下(具体我也说不清楚,只是看了url中的说明之后的感觉而已),使有可能发生recursive calls 的!
楼主很牛啊 7点多就开始一天的工作了!!!!
protected void handleShellCloseEvent()
{
setReturnCode(1);
/**
* 增加你想实现的代码(shell关闭前)
*/
close();
}