2016-03-26 回答
//搞定,代码自己理解哈.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class clock extends jframe
{
private dialog dialog;
public static void main(string[] args)
{
clock f = new clock();
f.setdefaultcloseoperation(jframe.exit_on_close);
f.setvisible(true);
}
class mydialog extends dialog implements windowlistener,actionlistener
{
jlabel label;
jpanel panel1,panel2;
jbutton button;
public mydialog(frame owner, string title, boolean modal) {
super(owner, title, modal);
// todo auto-generated constructor stub
label=new jlabel("时间到!");
button=new jbutton("确定");
panel1=new jpanel();
panel2=new jpanel();
panel1.setlayout(new borderlayout());
panel1.add("center",label);
panel2.add("center",button);
this.add("center",panel1);
this.add("south",panel2);
this.setsize(200,200);
this.setresizable(false);
this.addwindowlistener(this);
button.addactionlistener(this);
}
public void windowopened(windowevent e) {
// todo auto-generated method stub
}
public void windowclosing(windowevent e) {
// todo auto-generated method stub
this.setvisible(false);
}
public void windowclosed(windowevent e) {
// todo auto-generated method stub
}
public void windowiconified(windowevent e) {
// todo auto-generated method stub
}
public void windowdeiconified(windowevent e) {
// todo auto-generated method stub
}
public void windowactivated(windowevent e) {
// todo auto-generated method stub
}
public void windowdeactivated(windowevent e) {
// todo auto-generated method stub
}
public void actionperformed(actionevent e) {
// todo auto-generated method stub
this.setvisible(false);
}
}
clock()
{
settitle("倒计时");
setsize(320, 120);
dialog=new mydialog(this,"提示:",true);
clockpanel p = new clockpanel();
add(p);
}
class clockpanel extends jpanel
{
private jbutton b;
private boolean onetime = true;;
private jlabel lfen, lmiao, l;
private jtextfield tf, tm;
clockpanel() {
b = new jbutton("开始倒计时");
lfen = new jlabel("分");
lmiao = new jlabel("秒");
l = new jlabel("00:00");
tf = new jtextfield(3);
tm = new jtextfield(3);
l.setfont(new font("宋体", font.bold, 30));
l.setforeground(color.red);
b.addactionlistener(new actionlistener() {
public void actionperformed(actionevent arg0) {
if (onetime) {
if (tf.gettext().trim().equals("")) {
tf.settext("00");
}
if (tm.gettext().trim().equals("")) {
tm.settext("00");
}
new changelabel(tf.gettext().trim() + ":"
+ tm.gettext().trim()).start();
}
}
});
add(tf);
add(lfen);
add(tm);
add(lmiao);
add(b);
add(l);
}
class changelabel extends thread // 运行秒针线程
{
private int minitues;
private string sminitues;
private int sound;
private string ssound;
private string labeltime;
public changelabel(string time) {
// todo auto-generated constructor stub
onetime = false;
this.minitues = integer.parseint(time.substring(0, time
.indexof(':')));
this.sound = integer
.parseint(time.substring(time.indexof(':') + 1));
}
private long time1;
private long time2;
public void run() {
time1 = system.currenttimemillis();
while (true) {
time2 = system.currenttimemillis();
while (!(minitues == 0 & sound == 0) && time2 >= time1 + 1000) {
time1 = time2;
if (sound == 0) {
sound = 59;
minitues--;
} else {
sound--;
}
labeltime = this.gettime();
display();
}
if (minitues == 0 && sound == 0) {
dialog.setvisible(true);
onetime = true;
break;
}
}
}
private string gettime() {
if (minitues < 10)
this.sminitues = "0" + minitues;
else
this.sminitues = "" + minitues;
if (sound < 10)
this.ssound = "0" + sound;
else
this.ssound = "" + sound;
return this.sminitues + ":" + this.ssound;
}
public void display() {
/*
* 显示倒计时
*/
l.settext(this.labeltime);
}
}
}
}