当前位置: 代码迷 >> Java相关 >> [求助]如何做出两个小时的倒计时?
  详细解决方案

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

热度:254   发布时间:2006-05-31 23:32:00.0
[求助]如何做出两个小时的倒计时?
如何做出两个小时的倒计时来啊?好像考试系统的倒计时那样?
搜索更多相关的解决方案: 倒计时  

----------------解决方案--------------------------------------------------------
2个小时就是7200秒,你每一秒减1不就好了……
如果一定要做成时钟的样式,那么你就多几个判断吧,比如初始为2:00:00,判断秒如果为00,则分减1,如果分为00,则小时减1,如果全部为0就发出一声什么声音就好了……
----------------解决方案--------------------------------------------------------

用线程 作个图形界面 ... 不错的题目 希望大家实践实践


----------------解决方案--------------------------------------------------------

谢谢大家的回复!


----------------解决方案--------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class count
{
public static void main(String args[])
{
watch w=new watch();
Thread t=new Thread(w);
t.start();

}

}
class watch extends JFrame implements Runnable
{
int hour,minute,second,t;
JLabel a=new JLabel();
public watch()
{


super("倒计时");
Container c=getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
c.setLayout(new BorderLayout());
c.add(a,"North");
//System.out.println(hour+":"+minute+":"+second);
show();
}
public void paint(Graphics g)
{

g.setColor(Color.magenta);
g.drawString(Integer.toString(hour),100,10);
g.drawString(Integer.toString(minute),100,13);
g.drawString(Integer.toString(second),100,16);
}
public void run()
{
hour=2;
minute=00;
second=00;
t=hour+minute+second;
boolean a=true;

while(a)
{ if(t==0)
a=false;
second--;
if(second==00)
{
minute--;
second=60;
}
else if(minute==00)
{
hour--;
minute=60;
}
repaint();
try{
Thread.sleep(1000);
}catch(InterruptedException e){e.toString();}

}
}
}
这个程序是我刚写的,但是结果不能在面板上显示(还不知道写得对不对),求各位高手指导下。谢谢!
----------------解决方案--------------------------------------------------------

程序代码:

import javax.swing.*;
import java.awt.Toolkit;

public class TimeFrame extends JFrame implements Runnable {
private JLabel lbl;

private JPanel p;

private Thread t;

public TimeFrame() {
p = new JPanel();

lbl = new JLabel();

p.add(lbl);
getContentPane().add(p);

t = new Thread(this);

t.start();
}

public void run() {
int hour = 0;
int minute = 0;
int second = 5;

try {
while(true) {
lbl.setText(String.valueOf(hour + \" : \" + minute + \" : \" + second));

if(second == 0) {
second = 59; //如果秒为0,则改为59,否则自减
if(minute == 0) {
minute = 59; //如果分为0,则改为59,否则自减
if(hour == 0) {
break; //如果小时为0,则退出,并发出\"吡\"的声音
}
else
hour--;
}
else
minute--;
}
else
second--;

t.sleep(1000);
}

Toolkit.getDefaultToolkit().beep();
}
catch(Exception e) {

}
}

public static void main(String[] args) {
TimeFrame f = new TimeFrame();

f.setTitle(\"倒计时\");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(100,100);
f.setVisible(true);
}
}



本人很久没写过swing了……所以一下子连线程都忘记怎么用了……呵呵,这个程序写得不是很好,但功能实现了……


----------------解决方案--------------------------------------------------------
飘飘叶子,谢谢啊!
----------------解决方案--------------------------------------------------------

不好意思啊,我还想问下你Toolkit.getDefaultToolkit().beep()这条语句有什么作用啊?


----------------解决方案--------------------------------------------------------
做就做出来了,但是我想加上一些功能,就是能够让使用者在界面上可以随意设置要倒计的时间,但是还是做不出来呢,还是要在源程序里改才可以实现,请教大家可以怎样实现呢?
----------------解决方案--------------------------------------------------------

程序代码:

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



修改时间的那个对话框里面的3个文本框的顺序分别是小时/分钟/秒,不高兴再添加Label了……自己看得懂就可以了……
程序里面没有对异常进行处理,只是捕捉了一下,所以还是完整填写比较好……


----------------解决方案--------------------------------------------------------
  相关解决方案