Topic: 请教:Shell退出时的问题

  Print this page

1.请教:Shell退出时的问题 Copy to clipboard
Posted by: snail
Posted on: 2005-02-13 16:48

我想在一个类似写字板的程序里,添加一个功能
就是在退出时,询问是否保存
shell.addShellListener(new ShellAdapter(){
public void shellClosed(ShellEvent e){
int flag = 0;
if (!saved ){
MessageBox saveMessage=
new MessageBox(shell, SWT.YES | SWT.NO |
SWT.CANCEL | SWT.ICON_WARNING);
saveMessage.setMessage("还未保存,是否保存?");
flag = saveMessage.open();
if (flag == SWT.YES) {
try {
save();
}catch (IOException event){
event.printStackTrace();}}
if (flag == SWT.NO);
if (flag == SWT.CANCEL) {
//这里如何写,使MessageBox退出时,程序不要关闭
};
}
}});
用shell.wait();错误提示不是用户线程
请问有什么办法使shell可以中止退出的过程

2.Re:请教:Shell退出时的问题 [Re: snail] Copy to clipboard
Posted by: wdh113
Posted on: 2005-02-13 23:24

if (flag == SWT.CANCEL) {
e.doit = false;
}

3.Re:请教:Shell退出时的问题 [Re: snail] Copy to clipboard
Posted by: snail
Posted on: 2005-02-14 00:35

通过了~~
HOHO,原来是这样di~~~~
嗯,多谢多谢

4.Re:请教:Shell退出时的问题 [Re: snail] Copy to clipboard
Posted by: VirusCamp
Posted on: 2005-02-28 12:35

/*
* Created on 2004-10-23
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package ve.examples;

/*******************************************************************************
* Copyright Coffee 2003, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
import java.io.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

public class SimpleSWTTextEditor {

private org.eclipse.swt.widgets.Shell sShell = null; // @jve:decl-index=0:visual-constraint="19,7"

private Text textArea = null;

private Button button = null;

private Button button1 = null;

private Button button2 = null;

private boolean hasChanged = false;

private static final String title = "Simple SWT Text Editor";

private static final String NEW_LINE = System.getProperty("line.separator");

public static void main(String[] args) {
/* Before this is run, be sure to set up the following in the launch configuration
* (Arguments->VM Arguments) for the correct SWT library path.
* The following is a windows example:
* -Djava.library.path="installation_directory\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86"
*/
org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display
.getDefault();
SimpleSWTTextEditor thisClass = new SimpleSWTTextEditor();
thisClass.createSShell();
thisClass.sShell.open();

while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

/**
* This method initializes sShell
*/
private void createSShell() {
sShell = new org.eclipse.swt.widgets.Shell();
org.eclipse.swt.layout.GridLayout gridLayout2 = new GridLayout();
org.eclipse.swt.layout.GridData gridData3 = new org.eclipse.swt.layout.GridData();
org.eclipse.swt.layout.GridData gridData4 = new org.eclipse.swt.layout.GridData();
org.eclipse.swt.layout.GridData gridData5 = new org.eclipse.swt.layout.GridData();
org.eclipse.swt.layout.GridData gridData6 = new org.eclipse.swt.layout.GridData();
textArea = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL
| SWT.BORDER);
button = new Button(sShell, SWT.NONE);
button1 = new Button(sShell, SWT.NONE);
button2 = new Button(sShell, SWT.NONE);
sShell.setText(title);
sShell.setLayout(gridLayout2);
gridLayout2.numColumns = 3;
gridLayout2.makeColumnsEqualWidth = false;
gridData3.horizontalSpan = 3;
gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData3.grabExcessHorizontalSpace = true;
gridData3.grabExcessVerticalSpace = true;
textArea.setLayoutData(gridData3);
button.setText("Load File");
button.setLayoutData(gridData4);
button1.setText("Save File");
button1.setLayoutData(gridData5);
button2.setText("Exit");
button2.setLayoutData(gridData6);
gridData4.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
gridData4.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData4.grabExcessHorizontalSpace = true;
gridData5.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData5.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData6.grabExcessHorizontalSpace = true;
sShell.setSize(new org.eclipse.swt.graphics.Point(393, 279));
textArea.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
if (!hasChanged) {
sShell.setText(title + " *"); //$NON-NLS-1$
hasChanged = true;
}
}
});
button2
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
doExit();
}
});
button1
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
saveFile();
}
});
button
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
loadFile();
}
});
sShell.addShellListener(new org.eclipse.swt.events.ShellAdapter() {
public void shellClosed(org.eclipse.swt.events.ShellEvent e) {
e.doit = doExit();
}
});
}

private void loadFile() {
FileDialog dialog = new FileDialog(sShell, SWT.OPEN);
String result = dialog.open();
if (result != null) {
File f = new File(result);
try {
BufferedReader br = new BufferedReader(new FileReaderRose);
StringBuffer buff = new StringBuffer();
String line = br.readLine();
while (line != null) {
buff.append(line + NEW_LINE);
line = br.readLine();
}
textArea.setText(buff.toString());
br.close();
sShell.setText(title);
hasChanged = false;
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

private void saveFile() {
FileDialog dialog = new FileDialog(sShell, SWT.SAVE);
String result = dialog.open();
if (result != null) {
File f = new File(result);
try {
BufferedWriter bw = new BufferedWriter(new FileWriterRose);
String text = textArea.getText();
bw.write(text);
bw.close();
sShell.setText(title);
hasChanged = false;
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

private boolean doExit() {
if (hasChanged) {
MessageBox mb = new MessageBox(sShell, SWT.ICON_QUESTION | SWT.YES
| SWT.NO | SWT.CANCEL);
mb.setText("Save Changes?");
mb.setMessage("File has been changed. Save before exit?");
int state = mb.open();
if (state == SWT.YES) {
saveFile();
} else if (state == SWT.CANCEL) {
return false;
}
}
sShell.close();
sShell.dispose();
return true;
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923