我就是想一点按钮,然后就显示剩余的时间代码: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");
//
}}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【edisonkun】截止到2008-07-07 17:27:48的历史汇总数据(不包括此帖):
    发帖的总数量:0                        发帖的总分数:0                        
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:---------------------结分的百分比:---------------------
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   


    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");
            //
        }
    }
      

  3.   

    你的代码是正确的呀?你怎么会不好用呢?请检查包是否都导入了,特别是common-beanutils包.shell.getDisplay().asyncExec(new Runnable(){表示创建一个新的线程用来计数(就是显示出来的倒计时)
      

  4.   

    因为shell.getDisplay().asyncExec(new Runnable(){}) 是处理界面的线程,你再里面写了一个死循环也就是说你的界面就一直死掉了,所以你应该启动一个线程处理逻辑只有在改界面值的时候用一下shell.getDisplay().asyncExec(new Runnable(){}) 这样才能保证界面不会死掉,不然就会一直处在界面处理里面界面线程执行不完就卡死了。
      

  5.   

    那意思就是不用shell.getDisplay().asyncExec(new Runnable(){}) 了?直接用3楼的 new Thread()就可以啦?一般 shell.getDisplay().asyncExec(new Runnable(){})什么时候用啊?跟直接用 new thread有什么区别吗
      

  6.   

    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封装起来
      

  7.   

    long endTime = startTime + 30 * 60 * 1000; // 从开始时刻开始
                            // 加半小时
                            long nowTime, leftTime;
                            int ii = 0;
                            while(true)
      

  8.   

    6楼那样是对的,其实shell.getDisplay()和Display.getDefault()基本上是一个display,除非你new 了一个新的,在这个新的上面生成了这个shell。Display的asyncExec是用在对界面操作的时候,而且是在其他线程中对界面操作的时候,所以为什么你一般的程序是不会用到这个的,只有在启动新的线程并且操作界面,比如获取值或者修改值的时候才会用到。你这种情况下应该需要启动新的线程,还要用到这个asyncExec方法因为你让此线程sleep了,如果不启动新的线程你的界面应该也会死掉或者是一种一卡一卡的情况你可以试一下。
      

  9.   

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