swt的Display.syncExec无法执行

valid25f 2013-03-31 01:46:32
只有一个要求 按下一个按钮显示一个进度条 5秒后让它不可见 期间程序不假死 比如仍然可以移动窗口
下面这段代码 会界面会卡死5秒 syncExec没法用?

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ProgressBar;

public class Bar extends Shell {

/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
Bar shell = new Bar(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the shell.
* @param display
*/
public Bar(final Display display) {
super(display, SWT.SHELL_TRIM);

final ProgressBar progressBar = new ProgressBar(this, SWT.NONE);//HORIZONTAL | SWT.INDETERMINATE);
progressBar.setBounds(119, 34, 170, 17);
progressBar.setVisible(false);
Button btnNewButton = new Button(this, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
progressBar.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
new Thread(new Sleep1(display)).start();
progressBar.setVisible(false);
}
});
btnNewButton.setBounds(169, 82, 72, 22);
btnNewButton.setText("New Button");

createContents();
}

/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);

}

@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}




public class Sleep1 implements Runnable{


private Display display;

public Sleep1(Display display) {
this.display=display;
}

@Override
public void run() {
display.syncExec(new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
}


...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
却天涯为客 2013-04-17
  • 打赏
  • 举报
回复
public class Bar extends Shell {

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			Bar shell = new Bar(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public Bar(final Display display) {
		super(display, SWT.SHELL_TRIM);

		final ProgressBar progressBar = new ProgressBar(this, SWT.NONE);
		progressBar.setBounds(119, 34, 170, 17);
		progressBar.setVisible(false);
		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// progressBar.setVisible(true);
				new Thread(new Sleep1(display, progressBar)).start();
			}
		});
		btnNewButton.setBounds(169, 82, 72, 22);
		btnNewButton.setText("New Button");

		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public class Sleep1 implements Runnable {

		private Display display;
		private ProgressBar progressBar;

		public Sleep1(Display display, ProgressBar progressBar) {
			this.display = display;
			this.progressBar = progressBar;
		}

		@Override
		public void run() {
			display.syncExec(new Runnable() {

				@Override
				public void run() {
					progressBar.setVisible(true); // 这句也可以放在外面, 可以省代码
				}
			});
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			display.syncExec(new Runnable() {

				@Override
				public void run() {
					progressBar.setVisible(false);

				}
			});
		}
	}

}
valid25f 2013-03-31
  • 打赏
  • 举报
回复
引用 1 楼 javafreely 的回复:
你应该用 asyncExec
我是想 按按钮->显示进度条->等5秒->进度条消失 如果asyncExec 不到一毫秒就立刻消失了 跟没显示一样
valid25f 2013-03-31
  • 打赏
  • 举报
回复
更正一下:第一个类的Thread.sleep(5000); 去掉了现象依然一样 progressBar.setVisible(true); try { Thread.sleep(5000); //这一行不要 } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } new Thread(new Sleep1(display)).start(); 就是改成下面这样:

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ProgressBar;

public class Bar extends Shell {

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			Bar shell = new Bar(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public Bar(final Display display) {
		super(display, SWT.SHELL_TRIM);
		
		final ProgressBar progressBar = new ProgressBar(this, SWT.NONE);//HORIZONTAL | SWT.INDETERMINATE);
		progressBar.setBounds(119, 34, 170, 17);
		progressBar.setVisible(false);
		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				progressBar.setVisible(true);
				new Thread(new Sleep1(display)).start();
				progressBar.setVisible(false);
			}
		});
		btnNewButton.setBounds(169, 82, 72, 22);
		btnNewButton.setText("New Button");
		
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}

地球原住民 2013-03-31
  • 打赏
  • 举报
回复
你应该用 asyncExec

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧