import java.awt.*; import java.util.*; import javax.swing.*; import javax.swing.WindowConstants;class stop extends Canvas implements Runnable { boolean bStart = true; String ss, mii; public stop() { setSize(80, 30); new Thread(this).start(); } public void paint(Graphics g) { Calendar rightNow = Calendar.getInstance(); int h = rightNow.get(Calendar.HOUR_OF_DAY); int m = rightNow.get(Calendar.MINUTE); int s = rightNow.get(Calendar.SECOND); int mi = rightNow.get(Calendar.MILLISECOND); mii = String.valueOf(mi); ss = String.valueOf(s); if (s < 10 && s >= 0) { ss = "0" + ss; } String hh = String.valueOf(h); if (h < 10 && h >= 0) { hh = "0" + hh; } String mm = String.valueOf(m); if (m < 10 && m >= 0) { mm = "0" + mm; } String display = new String(hh + ":" + mm + ":" + ss + " " + mii); g.drawRect(0, 0, 112, 28); g.setColor(Color.red); g.drawString(display, 30, 20); } public void run() { while (bStart) { try { Thread.sleep(1); } catch (Exception e) { e.printStackTrace(); } repaint(); } } }public class text { public static void main(String[] args) { JFrame f = new JFrame("text"); f.add(new stop()); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.setResizable(false); f.pack(); f.setLocation(500, 200); f.setVisible(true); } }
private Format format; TimerTask task = new TimerTask() { private Date date = new Date(); public void run() { if ( isShowing() ) { date.setTime( System.currentTimeMillis() ); String time = format.format( date ); setText( time ); } } }; new Timer().schedule( task, 0, 300 );文本按钮中显示系统时间。 我想你在API中查看Timer你会有收获的。
package cn.com.chengang.myswt;import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date;import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label;import cn.com.common.util.CommonUtil;/** * @author chengang 2006-4-19 */ public class TimeLabel { private Label timeLabel; private long flashSpaceTime = 480; //闪动间隔时间 private boolean progressStop = false; //处理是否停止的标志 private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); public TimeLabel(Composite comp, int style) { timeLabel = new Label(comp, style); timeLabel.setText("sssssssssssssssssss"); //监听Dispose事件,在其销毁时停掉线程 timeLabel.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { progressStop=true; } }); new ShowTimeThread().start(); } public void dispose() { progressStop = true; } private Display getDisplay() { return Display.getDefault(); } private class ShowTimeThread extends Thread { public void run() { while (!progressStop) { String str = dateFormat.format(new Date()); setText(str); CommonUtil.sleep(flashSpaceTime);//闪动间隔 //CommonUtil是我自己写的一个常用工具方法类,sleep的代码如下 //public class CommonUtil { // public static void sleep(long millis) { // try { // Thread.sleep(millis); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } } } private void setText(final String time) { getDisplay().asyncExec(new Runnable() { public void run() { if(!progressStop) timeLabel.setText(time); } }); } } public void setLayoutData(GridData data) { timeLabel.setLayoutData(data); } public void setDateFormate(DateFormat format) { dateFormat = format; }}
import java.util.*;
import javax.swing.*;
import javax.swing.WindowConstants;class stop extends Canvas implements Runnable { boolean bStart = true;
String ss, mii; public stop() {
setSize(80, 30);
new Thread(this).start();
} public void paint(Graphics g) {
Calendar rightNow = Calendar.getInstance();
int h = rightNow.get(Calendar.HOUR_OF_DAY);
int m = rightNow.get(Calendar.MINUTE);
int s = rightNow.get(Calendar.SECOND);
int mi = rightNow.get(Calendar.MILLISECOND);
mii = String.valueOf(mi);
ss = String.valueOf(s);
if (s < 10 && s >= 0) {
ss = "0" + ss;
}
String hh = String.valueOf(h);
if (h < 10 && h >= 0) {
hh = "0" + hh;
}
String mm = String.valueOf(m);
if (m < 10 && m >= 0) {
mm = "0" + mm;
}
String display = new String(hh + ":" + mm + ":" + ss + " " + mii);
g.drawRect(0, 0, 112, 28);
g.setColor(Color.red);
g.drawString(display, 30, 20);
} public void run() {
while (bStart) {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
}
}public class text {
public static void main(String[] args) {
JFrame f = new JFrame("text");
f.add(new stop());
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setResizable(false);
f.pack();
f.setLocation(500, 200);
f.setVisible(true); }
}
private Format format;
TimerTask task = new TimerTask() {
private Date date = new Date();
public void run() {
if ( isShowing() ) {
date.setTime( System.currentTimeMillis() );
String time = format.format( date );
setText( time );
}
}
};
new Timer().schedule( task, 0, 300 );文本按钮中显示系统时间。
我想你在API中查看Timer你会有收获的。
package cn.com.chengang.myswt;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;import cn.com.common.util.CommonUtil;/**
* @author chengang 2006-4-19
*/
public class TimeLabel {
private Label timeLabel;
private long flashSpaceTime = 480; //闪动间隔时间
private boolean progressStop = false; //处理是否停止的标志
private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); public TimeLabel(Composite comp, int style) {
timeLabel = new Label(comp, style);
timeLabel.setText("sssssssssssssssssss");
//监听Dispose事件,在其销毁时停掉线程
timeLabel.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
progressStop=true;
}
});
new ShowTimeThread().start();
} public void dispose() {
progressStop = true;
} private Display getDisplay() {
return Display.getDefault();
} private class ShowTimeThread extends Thread {
public void run() {
while (!progressStop) {
String str = dateFormat.format(new Date());
setText(str);
CommonUtil.sleep(flashSpaceTime);//闪动间隔
//CommonUtil是我自己写的一个常用工具方法类,sleep的代码如下
//public class CommonUtil {
// public static void sleep(long millis) {
// try {
// Thread.sleep(millis);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
} private void setText(final String time) {
getDisplay().asyncExec(new Runnable() {
public void run() {
if(!progressStop)
timeLabel.setText(time);
}
});
}
} public void setLayoutData(GridData data) {
timeLabel.setLayoutData(data);
} public void setDateFormate(DateFormat format) {
dateFormat = format;
}}