在这个小例子中,当关闭窗口后,eclipse的console的Terminate还是红色的,这说明什么问题?
是因为窗口关闭了,但进程没有被关闭?
如何使得窗口关闭,进程自然结束?import org.eclipse.swt.widgets.*;
public class TestGUIThread {
private static int counter;
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell();
shell.setText("Counter");
shell.open();
new Thread() {
private Runnable cmd = new Runnable() {
public void run() {
shell.setText(String.valueOf(counter++));
}
};
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
return;
}
display.asyncExec(cmd);
}
}
}.start();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
是因为窗口关闭了,但进程没有被关闭?
如何使得窗口关闭,进程自然结束?import org.eclipse.swt.widgets.*;
public class TestGUIThread {
private static int counter;
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell();
shell.setText("Counter");
shell.open();
new Thread() {
private Runnable cmd = new Runnable() {
public void run() {
shell.setText(String.valueOf(counter++));
}
};
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
return;
}
display.asyncExec(cmd);
}
}
}.start();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货