java 倒计时

写一个JAVA倒计时程序输入分和秒按下button就跟秒表那样2:302:292:28………………0:020:010:00结束了弹个对话框importjava.awt.*... 写一个JAVA倒计时程序

输入分和秒 按下button就跟秒表那样
2:30
2:29
2:28
……
……
……
0:02
0:01
0:00
结束了弹个对话框

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Clock
{
public static void main(String[] args)
{
ClockFrame f = new ClockFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

}

}

class ClockFrame extends JFrame
{
ClockFrame()
{
setTitle("倒计时");
setSize(320, 120);

ClockPanel p = new ClockPanel();
add(p);
}
}

class ClockPanel extends JPanel
{
private JButton b;
private JLabel lfen,lmiao,l;
private JTextField tf,tm;
private Date now;
private GregorianCalendar calendar;
private Thread th;

ClockPanel()
{
b = new JButton("开始倒计时");
lfen = new JLabel("分");
lmiao = new JLabel("秒");
l = new JLabel("倒计时:3分56秒");
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)
{

}
});

add(tf);
add(lfen);
add(tm);
add(lmiao);
add(b);
add(l);

}
public void run() {

try
{
while(true)
{
display();
Thread.sleep(10);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

public void display()
{
/*
* 显示倒计时
*/
}
}
展开
 我来答
方杭尽
2007-09-18 · TA获得超过1万个赞
知道大有可为答主
回答量:1296
采纳率:0%
帮助的人:660万
展开全部
//搞定,代码自己理解哈.

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);
}
}
}
}
忆天_sky
2007-09-18 · TA获得超过1077个赞
知道小有建树答主
回答量:1592
采纳率:0%
帮助的人:472万
展开全部
利用多线程
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式