java代码小时倒计时_[求助]如何做出两个小时的倒计时?

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.Toolkit;

public class TimeFrame extends JFrame implements Runnable,ActionListener {

private JLabel lbl;

private JPanel p;

private Thread t;

private JButton editButton,startButton;

private DialogFrame dialog;

private int hour = 2,minute = 0,second = 0;

public TimeFrame() {

p = new JPanel();

editButton = new JButton(\"修改时间\");

startButton = new JButton(\"开始倒计时\");

editButton.addActionListener(this);

startButton.addActionListener(this);

lbl = new JLabel(hour + \" : \" + minute + \" : \" + second);

p.add(lbl);

p.add(editButton);

p.add(startButton);

getContentPane().add(p);

}

public void run() {

try {

while(true) {

lbl.setText(hour + \" : \" + minute + \" : \" + second);

if(second == 0) {

second = 59; //如果秒为0,则改为59,否则自减

if(minute == 0) {

minute = 59; //如果分为0,则改为59,否则自减

if(hour == 0) {

startButton.setText(\"开始倒计时\");

break; //如果小时为0,则退出,并发出\"吡\"的声音

}

else

hour--;

}

else

minute--;

}

else

second--;

t.sleep(1000);

}

Toolkit.getDefaultToolkit().beep();

}

catch(Exception e) {

}

}

public void actionPerformed(ActionEvent e) {

Object o = e.getSource();

if(o == editButton) {

dialog = new DialogFrame(this);

}

else if(o == startButton) {

String text = startButton.getText();

if(\"开始倒计时\".equals(text)) {

t = new Thread(this);

t.start();

startButton.setText(\"停止倒计时\");

}

else {

t.stop();

startButton.setText(\"开始倒计时\");

}

}

}

public static void main(String[] args) {

TimeFrame f = new TimeFrame();

f.setTitle(\"倒计时\");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(200,200);

f.setVisible(true);

}

public void setTime(int hour,int min,int sec) {

this.hour = hour;

this.minute = min;

this.second = sec;

lbl.setText(hour + \" : \" + minute + \" : \" + second);

}

}

class DialogFrame extends JDialog implements ActionListener {

private JTextField hourText,minText,secText;

private JButton okButton,canceButton;

private TimeFrame f;

public DialogFrame(TimeFrame f) {

super(f,\"修改时间\");

this.f = f;

hourText = new JTextField(5);

minText = new JTextField(5);

secText = new JTextField(5);

okButton = new JButton(\"OK\");

canceButton = new JButton(\"Cance\");

okButton.addActionListener(this);

canceButton.addActionListener(this);

setLayout(new FlowLayout());

add(hourText);

add(minText);

add(secText);

add(okButton);

add(canceButton);

setSize(200,100);

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

Object o = e.getSource();

if(o == okButton) {

try {

int hour = Integer.valueOf(hourText.getText());

int min = Integer.valueOf(minText.getText());

int sec = Integer.valueOf(secText.getText());

f.setTime(hour,min,sec);

}

catch(Exception ex) {

//如果输入的不是数字,在这里进行处理

}

dispose();

}

else if(o == canceButton) {

dispose();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值