java 倒计时

[复制链接]
查看11 | 回复1 | 2007-9-18 14:13:09 | 显示全部楼层 |阅读模式
利用多线程
回复

使用道具 举报

千问 | 2007-9-18 14:13:09 | 显示全部楼层
//搞定,代码自己理解哈.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);} }}}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行