
import javautil;
import javaawt;
import javaawtevent;
import javaapplet;
public class Clock extends Applet {
private final Panel pnlTop = new Panel();
private final Panel pnlBot = new Panel();
private final Label lblDate = new Label();
private final Label lblTime = new Label();
private final Label lblWatch = new Label();
private final Button btnGo = new Button("开始");
private final Button btnReset = new Button("重置");
private final Label lblSplit = new Label();
private final Button btnSplit = new Button("定点");
private final Button btnSplitReset = new Button("定点重置");
private final Button btnLapAdd = new Button("冲线");
private final Button btnLapReset = new Button("冲线重置");
private final javaawtList lstLaps = new javaawtList();
private final UpdateClockThread ucThread = new UpdateClockThread();
private final StopwatchThread swThread = new StopwatchThread();
private class btnGoListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((btnGogetLabel()equals("开始")) ||
(btnGogetLabel()equals("继续"))) {
// Start the clock!
swThreadgo();
btnGosetLabel("停止");
btnGosetBackground(Colorred);
} else if (btnGogetLabel()equals("停止")) {
// Stop the clock!
swThreadnoGo();
btnGosetLabel("继续");
btnGosetBackground(Colorgreen);
}
}
}
private class btnResetListener implements ActionListener {
/ Actually run when the button gets clicked
@param e The event
/
public void actionPerformed(ActionEvent e) {
swThreadreset();
btnGosetLabel("开始");
btnGosetBackground(Colorgreen);
}
}
/ Listens to the Split button
@version CS2136 - Term D'00 - Assignment 5
@author Peter Cooper Jr
/
private class btnSplitListener implements ActionListener {
/ Actually run when the button gets clicked
@param e The event
/
public void actionPerformed(ActionEvent e) {
lblSplitsetText(lblWatchgetText());
}
}
/ Listens to the Split Reset button
@version CS2136 - Term D'00 - Assignment 5
@author Peter Cooper Jr
/
private class btnSplitResetListener implements ActionListener {
/ Actually run when the button gets clicked
@param e The event
/
public void actionPerformed(ActionEvent e) {
lblSplitsetText("");
}
}
/ Listens to the Lap Add button
@version CS2136 - Term D'00 - Assignment 5
@author Peter Cooper Jr
/
private class btnLapAddListener implements ActionListener {
/ Actually run when the button gets clicked
@param e The event
/
public void actionPerformed(ActionEvent e) {
swThreadaddLap();
}
}
/ Listens to the Lap Reset button
@version CS2136 - Term D'00 - Assignment 5
@author Peter Cooper Jr
/
private class btnLapResetListener implements ActionListener {
/ Actually run when the button gets clicked
@param e The event
/
public void actionPerformed(ActionEvent e) {
swThreadresetLap();
}
}
/ A thread that updates the current date & time
@version CS2136 - Term D'00 - Assignment 5
@author Peter Cooper Jr
/
private class UpdateClockThread extends Thread {
/ The actual work of the thread
/
public void run() {
while (true) {
Calendar now = CalendargetInstance();
String month = IntegertoString(nowget(CalendarMONTH)+1);
String date = IntegertoString(nowget(CalendarDAY_OF_MONTH));
String year = IntegertoString(nowget(CalendarYEAR));
String hour = IntegertoString(nowget(CalendarHOUR));
if (hourequals("0")) hour = "12";
String minute = IntegertoString(nowget(CalendarMINUTE));
if (minutelength() == 1) minute = "0" + minute;
String second = IntegertoString(nowget(CalendarSECOND));
if (secondlength() == 1) second = "0" + second;
String ampm = nowget(CalendarAM_PM) == CalendarAM
"AM" : "PM";
lblDatesetText(month + "/" + date + "/" + year);
lblTimesetText(hour + ":" + minute + ":" + second
+ " " + ampm);
try {
sleep(500);
} catch (InterruptedException e) {}
}
}
}
private class StopwatchThread extends Thread {
/ Whether or not stopwatch is running /
private boolean going = false;
/ Stores elapsed milliseconds of previous runs /
private long prevElapsed = 0;
/ Stores beginning time of this run /
private Date startDate = new Date();
/ Current lap number /
private int lapNum = 0;
/ Elapsed time at end of last lap /
private long lastLapTime = 0;
/ Returns elapsed time in milliseconds
@return The elapsed time
/
private long elapsedTime() {
return prevElapsed +
(going new Date()getTime() - startDategetTime() : 0);
}
/ Changes the number of elapsed milliseconds into a string
@param time Number of elapsed milliseconds
@return The elapsed time as a string
/
private String msToString(long time) {
String ms, sec, min;
if (time % 10 >= 5) //round to nearest hundredth
time += 5;
ms = LongtoString(time % 1000);
while (mslength() < 3)
ms = "0" + ms;
ms = mssubstring(0, mslength() - 1);
time /= 1000;
sec = LongtoString(time % 60);
if (seclength() == 1) sec = "0" + sec;
time /= 60;
min = LongtoString(time);
return min + ":" + sec + "" + ms;
}
public void go() {
startDate = new Date();
going = true;
}
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
public void reset() {
going = false;
prevElapsed = 0;
lastLapTime = 0;
}
public void addLap() {
long elapsed = elapsedTime();
lstLapsadd("冲线 " + IntegertoString(++lapNum)+ " -- " +
"用时: " + msToString(elapsed) + " -- " +
"冲线时间: " + msToString(elapsed - lastLapTime));
lastLapTime = elapsed;
}
/ Resets the lap list
/
public void resetLap() {
lstLapsremoveAll();
lapNum = 0;
lastLapTime = 0;
}
/ Main code of the thread
/
public void run() {
while (true) {
lblWatchsetText(msToString(elapsedTime()));
yield();
}
}
}
public void init() {
setLayout(new GridLayout(2,1));
setBackground(ColorlightGray);
setForeground(Colorblack);
pnlTopsetLayout(new GridLayout(4,4));
pnlTopadd(new Label("日期:"));
pnlTopadd(lblDate);
pnlTopadd(new Label("时间:"));
pnlTopadd(lblTime);
pnlTopadd(new Label("计时:"));
//lblWatchsetBackground(Colorblack);
lblWatchsetForeground(Colorblue);
pnlTopadd(lblWatch);
pnlTopadd(btnGo);
btnGosetBackground(Colorgreen);
pnlTopadd(btnReset);
pnlTopadd(new Label("定点:"));
pnlTopadd(lblSplit);
pnlTopadd(btnSplit);
pnlTopadd(btnSplitReset);
pnlTopadd(new Label("冲线时间:"));
pnlTopadd(new Label());
pnlTopadd(btnLapAdd);
pnlTopadd(btnLapReset);
pnlBotsetLayout(new GridLayout(1,1));
pnlBotadd(lstLaps);
add(pnlTop);
add(pnlBot);
btnGoaddActionListener(new btnGoListener());
btnResetaddActionListener(new btnResetListener());
btnSplitaddActionListener(new btnSplitListener());
btnSplitResetaddActionListener(new btnSplitResetListener());
btnLapAddaddActionListener(new btnLapAddListener());
btnLapResetaddActionListener(new btnLapResetListener());
swThreadsetDaemon(true);
ucThreadsetDaemon(true);
swThreadstart();
ucThreadstart();
}
public static void main(String[] args) {
Clock applet = new Clock();
Frame aFrame = new Frame("计时器");
aFrameaddWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Systemexit(0);
}
});
aFrameadd(applet, BorderLayoutCENTER);
aFramesetSize(400, 200);
appletinit();
appletstart();
aFramesetVisible(true);
}
}
import javaawtColor;
import javautil;
import javaawt;
import javaapplet;
public class Clock extends Applet implements Runnable
{
Thread timer=null;
Label label;
int lastxs=50,lastys=30,lastxm=50,lastym=30,lastxh=50,lastyh=30;
public void init()
{
label=new Label(" ");
setBackground(Colorwhite);
add(label);
}
public void paint(Graphics g)
{
int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter;
Date rightnow=new Date();
String today=rightnowtoLocaleString();
labelsetText(today);
s=rightnowgetSeconds();
m=rightnowgetMinutes();
h=rightnowgetHours();
xcenter=100;
ycenter=80;
xs=(int)(Mathcos(s314f/30-314f/2)45+xcenter);
ys=(int)(Mathsin(s314f/30-314f/2)45+ycenter);
xm=(int)(Mathcos(m314f/30-314f/2)45+xcenter);
ym=(int)(Mathsin(m314f/30-314f/2)45+ycenter);
xh=(int)(Mathcos((h30+m2)314f/180-314f/2)30+xcenter);
yh=(int)(Mathsin((h30+m2)314f/180-314f/2)30+ycenter);
gsetFont(new Font("TimesToman",FontPLAIN,14));
gsetColor(Colororange);
gfill3DRect(xcenter-50,ycenter-50,100,100,true);
gsetColor(ColordarkGray);
gdrawString("12",xcenter-5,ycenter-37);
gdrawString("3",xcenter+40,ycenter+3);
gdrawString("6",xcenter-3,ycenter+45);
gdrawString("9",xcenter-45,ycenter+3);
gsetColor(Colororange);
if(xs!=lastxs||ys!=lastys)
{
gdrawLine(xcenter,ycenter,lastxs,lastys);
}
if(xm!=lastxm||ym!=lastym)
{
gdrawLine(xcenter,ycenter-1,lastxm,lastym);
gdrawLine(xcenter-1,ycenter,lastxm,lastym);
}
if(xh!=lastxh||yh!=lastyh)
{
gdrawLine(xcenter,ycenter-1,lastxh,lastyh);
gdrawLine(xcenter-1,ycenter,lastxh,lastyh);
}
gsetColor(Colorred);
gdrawLine(xcenter,ycenter,xs,ys);
gdrawLine(xcenter,ycenter-1,xm,ym);
gdrawLine(xcenter-1,ycenter,xm,ym);
gdrawLine(xcenter,ycenter-1,xh,yh);
gdrawLine(xcenter-1,ycenter,xh,yh);
lastxs=xs;
lastys=ys;
lastxm=xm;
lastym=ym;
lastxh=xh;
lastyh=yh;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);
timerstart();
}
}
public void stop()
{
timer=null;
}
public void run()
{
while(timer!=null)
{
try
{
Threadsleep(1000);
}catch(InterruptedException ie){}
repaint();
}
timer=null;
}
public void update(Graphics g)
{
paint(g);
}
}
/ 每3秒运行一次 /
Timer timer = new Timer();
TimerTask tt = new TimerTask() {
public void run() {
/ 投放炸d的 *** 作 /
new Thread() {
public void run() {
try {
Threadsleep(5000);
}catch (Exception e) { }
/ 爆炸的 *** 作 /
}
}start();
}
}; timerschedule(tt, 0, 3000);
以上就是关于JAVA计时器的JAVA代码全部的内容,包括:JAVA计时器的JAVA代码、怎样用JAVA写一个10钟倒计时程序、JAVA计时器等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)