Java 如何在标签(SWT)上设置运行时间以及日期名称
我想在Swt标签上显示日期及其名称以及运行时间。 这是我的密码:-Java 如何在标签(SWT)上设置运行时间以及日期名称,java,swt,Java,Swt,我想在Swt标签上显示日期及其名称以及运行时间。 这是我的密码:- Label DateLbl = new Label(shell, SWT.NONE); DateLbl.setBounds(0,0,100,50); DateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Calendar cal1 = Calendar.getInstance(); Date
Label DateLbl = new Label(shell, SWT.NONE);
DateLbl.setBounds(0,0,100,50);
DateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar cal1 = Calendar.getInstance();
DateFormatSymbols dfs1 = new DateFormatSymbols();
String weekdays1[] = dfs1.getWeekdays();
int day1 = cal1.get(Calendar.DAY_OF_WEEK);
String nameOfDay1 = weekdays1[day1];
DateLbl.setText(" "+dateFormat1.format(cal1.getTime()) + " \n" +" "+nameOfDay1);
它显示时间、日期和日期名称,但我希望运行秒、分钟和小时(标签上完整的虚拟时钟)
这是上述代码的o/p-26/03/2014 12:57:01
星期三
每次启动应用程序时,时间都会更新
有谁能建议在SWT标签上进行秒、分、小时跑步吗 你的问题不清楚?你想要一个时钟每秒更新当前时间吗 如果是这样的话,你需要每秒产生一个线程来睡眠和醒来。然后,线程以任何适合swt的线程安全方式更新用户界面,并返回睡眠状态 对于睡眠线程,可以使用a或a。各有所长
要更新用户界面…我不知道swt,但希望您需要找到Swing中使用的等效工具。您可以使用
java.util.Timer
类定期运行代码:
Timer timer = new Timer("clock timer", true);
timer.schedule(new UpdateTimerTask(), 1000l, 1000l); // Run once a second
private class UpdateTimerTask extends TimerTask
{
@Override
public void run()
{
// Timer task runs in a background thread,
// so use Display.asyncExec to run SWT code in UI thread
Display.getDefault().asyncExec(new Runnable()
{
@Override
public void run()
{
// TODO update the label
}
});
}
}
SWT具有默认的本机计时器支持。使用下面的方法
org.eclipse.swt.widgets.Display.timerExec(int, Runnable)
有关更多信息,请阅读文档