shell.getDisplay().asyncExec(new Runnable(){}) 具体是怎么用的啊?为什么我的代码执行老是程序无响应

edisonkun 2008-07-07 05:27:28
我就是想一点按钮,然后就显示剩余的时间

代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class test2 {

protected Shell shell;

/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
test2 window = new test2();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
shell.setSize(500, 375);
shell.setText("SWT Application");

final Label label = new Label(shell, SWT.NONE);
label.setLayoutData(new GridData(245, SWT.DEFAULT));
label.setText("Label");

final Button button = new Button(shell, SWT.NONE);
button.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
shell.getDisplay().asyncExec(new Runnable(){
public void run(){
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
Calendar startCalendar = Calendar.getInstance();
//label.setText("死亡时间:" + dateFormat.format(startCalendar.getTime()));
Calendar now ;
long startTime = startCalendar.getTime().getTime(); // 获得开始时候的那个时间点
long endTime = startTime + 30 * 60 * 1000; // 从开始时刻开始 加半小时
long nowTime, leftTime, leftSec, leftMin;
int ii = 0;
while(true){
now = Calendar.getInstance();
nowTime = now.getTime().getTime();
leftTime = endTime - nowTime;
leftSec = leftTime / 1000;
leftMin = leftTime / (60 * 1000);

label.setText(leftSec+"秒," + leftMin+"分");

if(leftSec == 0)
{
label.setText("请从新记录时间!");
break;
}

try
{
Thread.sleep(1000);
}
catch(InterruptedException e1)
{
e1.printStackTrace();
}
}


}



});







}//1
});
button.setText("button");
//
}

}


...全文
937 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChenxLoveJava 2010-08-29
  • 打赏
  • 举报
回复
受教了 谢谢大家哈!!!!
guanhy 2008-12-04
  • 打赏
  • 举报
回复
大家说得比较详细,受教了……
hardtoregistration 2008-07-09
  • 打赏
  • 举报
回复
主要作用:在非主线程中对SWT组件进行可能引起界面刷新的操作时使用
因为SWT的机制是不允许主线程以外的线程对SWT组件进行这种操作,所以如果你是在别的线程中执行该类代码,那必须用
Display.getDefault().asyncExec(new Runnable(){})
把相应的代码封装起来

shoujiao

-------------------------------------------------------------
Quietly through .....
xustar603 2008-07-08
  • 打赏
  • 举报
回复
6楼那样是对的,其实shell.getDisplay()和Display.getDefault()基本上是一个display,除非你new 了一个新的,在这个新的上面生成了这个shell。Display的asyncExec是用在对界面操作的时候,而且是在其他线程中对界面操作的时候,所以为什么你一般的程序是不会用到这个的,只有在启动新的线程并且操作界面,比如获取值或者修改值的时候才会用到。你这种情况下应该需要启动新的线程,还要用到这个asyncExec方法因为你让此线程sleep了,如果不启动新的线程你的界面应该也会死掉或者是一种一卡一卡的情况你可以试一下。
  • 打赏
  • 举报
回复
long endTime = startTime + 30 * 60 * 1000; // 从开始时刻开始
// 加半小时
long nowTime, leftTime;
int ii = 0;
while(true)
craky 2008-07-07
  • 打赏
  • 举报
回复
shell.getDisplay().asyncExec(new Runnable(){})
建议使用Display.getDefault().asyncExec(new Runnable(){})代替

主要作用:在非主线程中对SWT组件进行可能引起界面刷新的操作时使用
因为SWT的机制是不允许主线程以外的线程对SWT组件进行这种操作,所以如果你是在别的线程中执行该类代码,那必须用
Display.getDefault().asyncExec(new Runnable(){})
把相应的代码封装起来,比如二楼中的下面片段


Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
label.setText(leftSec + "秒," + leftMin + "分");
}
});


因为label.setText(leftSec + "秒," + leftMin + "分");是在new Thread这个新线程中执行的,而且setText方法必须刷新界面,所以必须把这一行用Display.getDefault().asyncExec封装起来
edisonkun 2008-07-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xustar603 的回复:]
因为shell.getDisplay().asyncExec(new Runnable(){}) 是处理界面的线程,你再里面写了一个死循环也就是说你的界面就一直死掉了,所以你应该启动一个线程处理逻辑只有在改界面值的时候用一下shell.getDisplay().asyncExec(new Runnable(){}) 这样才能保证界面不会死掉,不然就会一直处在界面处理里面界面线程执行不完就卡死了。
[/Quote]
那意思就是不用shell.getDisplay().asyncExec(new Runnable(){}) 了?直接用3楼的 new Thread()就可以啦?

一般 shell.getDisplay().asyncExec(new Runnable(){})什么时候用啊?跟直接用 new thread有什么区别吗
xustar603 2008-07-07
  • 打赏
  • 举报
回复
因为shell.getDisplay().asyncExec(new Runnable(){}) 是处理界面的线程,你再里面写了一个死循环也就是说你的界面就一直死掉了,所以你应该启动一个线程处理逻辑只有在改界面值的时候用一下shell.getDisplay().asyncExec(new Runnable(){}) 这样才能保证界面不会死掉,不然就会一直处在界面处理里面界面线程执行不完就卡死了。
xiao94226498 2008-07-07
  • 打赏
  • 举报
回复
你的代码是正确的呀?你怎么会不好用呢?请检查包是否都导入了,特别是common-beanutils包.shell.getDisplay().asyncExec(new Runnable(){表示创建一个新的线程用来计数(就是显示出来的倒计时)
craky 2008-07-07
  • 打赏
  • 举报
回复

package craky;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Test2
{
protected Shell shell;

/**
* Launch the application
* @param args
*/
public static void main(String[] args)
{
try
{
Test2 window = new Test2();
window.open();
}
catch(Exception e)
{
e.printStackTrace();
}
}

/**
* Open the window
*/
public void open()
{
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}

/**
* Create contents of the window
*/
protected void createContents()
{
shell = new Shell();
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
shell.setSize(500, 375);
shell.setText("SWT Application");

final Label label = new Label(shell, SWT.NONE);
label.setLayoutData(new GridData(245, SWT.DEFAULT));
label.setText("Label");

final Button button = new Button(shell, SWT.NONE);
button.addMouseListener(new MouseAdapter()
{
public void mouseDown(final MouseEvent e)
{
new Thread()
{
public void run()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
Calendar startCalendar = Calendar.getInstance();
// label.setText("死亡时间:" +
// dateFormat.format(startCalendar.getTime()));
Calendar now;
long startTime = startCalendar.getTime().getTime(); // 获得开始时候的那个时间点
long endTime = startTime + 30 * 60 * 1000; // 从开始时刻开始
// 加半小时
long nowTime, leftTime;
int ii = 0;
while(true)
{
now = Calendar.getInstance();
nowTime = now.getTime().getTime();
leftTime = endTime - nowTime;
final long leftSec = leftTime / 1000;
final long leftMin = leftTime / (60 * 1000);

Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
label.setText(leftSec + "秒," + leftMin + "分");
}
});

if(leftSec == 0)
{
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
label.setText("请从新记录时间!");
}
});

break;
}

try
{
Thread.sleep(1000);
}
catch(InterruptedException e1)
{
e1.printStackTrace();
}
}

}
}.start();

}// 1
});
button.setText("button");
//
}
}

62,614

社区成员

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

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