java如何连续输出三句话_Java怎么写倒计时 12秒的倒计时 到7的时候输出一句话 到1的时候停下然后输出一句话 数字要像...

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);

}

}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值