我就是想一点按钮,然后就显示剩余的时间代码: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");
//
}}
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");
//
}}
楼主【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
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");
//
}
}
建议使用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封装起来
// 加半小时
long nowTime, leftTime;
int ii = 0;
while(true)
因为SWT的机制是不允许主线程以外的线程对SWT组件进行这种操作,所以如果你是在别的线程中执行该类代码,那必须用
Display.getDefault().asyncExec(new Runnable(){})
把相应的代码封装起来shoujiao-------------------------------------------------------------
Quietly through .....