只有一个要求 按下一个按钮显示一个进度条 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();
}
}});
}
}
下面这段代码 会界面会卡死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();
}
}});
}
}
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
}
}
如果asyncExec 不到一毫秒就立刻消失了 跟没显示一样
* 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); }
});
}
}}