SWT对话框自动关闭
来源:互联网 发布:mysql 查询重复记录 编辑:程序博客网 时间:2024/06/06 03:18
SWI程序在运行的过程中,通常会出现处理某一事件的时候,需要等待一段时间,处理结束以后,主界面再响应其他的事件。在这种情况下,一般会通过显示一个提示对话框,说明该操作正在运行,请稍候,处理结束后,关闭对话框,主界面可用。
如果仅仅是弹出一个对话框,此时主程序会等待对话框的结束,然后执行对话框后面的代码,需要对话框自己适当时自己关闭,可以考虑采用多线程的方式。
我们在对话框代码中,
(1)继承了Dialog类并实现了Runnable接口;
(2)打开对话框,然后启动时间处理线程;
(3)当处理线程执行run()完毕后,关闭对话框。
在我的工程中,点击按钮以后,发送消息给客户端,客户端进行处理,间隔一段时间,
向服务端返回处理结果,在这个过程中,服务端主界面进行等待,弹出对话框(里面线程对于socket端口进行read处理),当read到内容以后,线程关闭对话框,主线程继续进行。
如果仅仅是弹出一个对话框,此时主程序会等待对话框的结束,然后执行对话框后面的代码,需要对话框自己适当时自己关闭,可以考虑采用多线程的方式。
我们在对话框代码中,
(1)继承了Dialog类并实现了Runnable接口;
(2)打开对话框,然后启动时间处理线程;
(3)当处理线程执行run()完毕后,关闭对话框。
在我的工程中,点击按钮以后,发送消息给客户端,客户端进行处理,间隔一段时间,
向服务端返回处理结果,在这个过程中,服务端主界面进行等待,弹出对话框(里面线程对于socket端口进行read处理),当read到内容以后,线程关闭对话框,主线程继续进行。
- package gui;
- import java.io.DataInputStream;
- import java.io.IOException;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.ShellAdapter;
- import org.eclipse.swt.events.ShellEvent;
- import org.eclipse.swt.widgets.Dialog;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.Shell;
- /**
- * ShowProcessDialog implements the Runnable to read the check echo result in
- * other thread.
- */
- public class ShowProcessDialog extends Dialog implements Runnable {
- /**
- * reads the socket to get the echo result
- */
- private Thread tRead;
- protected Object result;
- protected Shell shell;
- protected Display display = getParent().getDisplay();
- private DataInputStream dataIn;
- private String echoStr;
- /**
- * Create the dialog
- *
- * @param parent
- * @param style
- */
- public ShowProcessDialog(Shell parent, int style) {
- super(parent, style);
- }
- /**
- * Create the dialog
- *
- * @param parent
- */
- public ShowProcessDialog(Shell parent, DataInputStream in) {
- this(parent, SWT.NONE);
- dataIn = in;
- }
- /**
- * Open the dialog
- *
- * @return the result
- */
- public Object open() {
- createContents();
- //start the thread to read the Agent echo result
- tRead = new Thread(this);
- tRead.start();
- shell.open();
- shell.layout();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- return result;
- }
- /**
- * Create contents of the dialog
- */
- protected void createContents() {
- shell = new Shell(getParent(), SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
- shell.setSize(345, 181);
- shell.setText("运行提示");
- final Label label = new Label(shell, SWT.NONE);
- label.setAlignment(SWT.CENTER);
- label.setText("正在执行监测,请稍候...");
- label.setBounds(55, 70, 204, 44);
- }
- /**
- * closes the dialog, because read Thread do this, it's
- * other thread, so should close dialog through dialog
- * thread using syncExec.
- */
- public void close() {
- display.getDefault().syncExec(new Runnable() {
- public void run() {
- shell.close();
- shell.dispose();
- }
- });
- }
- public void run() {
- try {
- System.out.println("wait for reading...");
- echoStr = dataIn.readUTF();
- } catch (IOException e) {
- System.out.println("read from Agent exception.");
- }
- close();
- }
- public String getEcho() {
- return echoStr;
- }
- }
- SWT对话框自动关闭
- swt 关闭对话框 关闭dialog
- 自动关闭的对话框
- 自动关闭MessageBox()对话框
- 自动关闭MessageBox()对话框
- C#自动关闭对话框
- 自动关闭对话框
- 自动关闭MessageBox对话框
- 自动的关闭对话框 MessageBox
- webbrowse 自动关闭弹出对话框
- 一种自动关闭对话框的简单方法
- 一种自动关闭对话框的简单方法
- 创建可以定时自动关闭的对话框
- 【WM编程】自动关闭MessageBox()对话框
- Android,对话框定时自动关闭的实现
- VC 定时自动关闭MessageBox弹出对话框
- Android对话框定时自动关闭的实现
- qt 对话框关闭以及自动释放内存
- C#继承详解
- Tapestry 单选实例
- 医改就是个屁----《循环营销》医药版
- 谈软件测试用例的可重用性
- 全国出国培训备选人员外语考试BFT成绩可以免考职称英语。QQ:1002756451 电话020-88535077
- SWT对话框自动关闭
- GridView多表头类
- Oracle 常用SQL查询列表
- TLC2543驱动程序
- 项目管理师_备考3
- 向C#的String类添加按字节截取字符串的扩展方法
- C#日期格式化
- wxString与std::string之间的转换
- 从终端输入一组数据进行进栈、出栈操作并将其结果打印出来(采用顺序栈的方式)