倒计时程序的问题

zplora 2006-05-15 11:15:52
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class countdown
{
JFrame frm;
JPanel p;
JTextField txthour,txtmin,txtsec;
JButton btnOK;

public countdown()
{
frm= new JFrame("倒计时");
p = new JPanel();
btnOK = new JButton("开始");
Mylst my = new Mylst();
btnOK.addActionListener(my);
txthour = new JTextField(3);
txtmin = new JTextField(3);
txtsec = new JTextField(3);
p.add(txthour);
p.add(txtmin);
p.add(txtsec);
p.add(btnOK);





frm.getContentPane().add(p);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(300,300);
frm.setVisible(true);

}

public static void main(String agrs[])
{
new countdown();

}
class Mylst implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
oneth one = new oneth();
one.start();

}
}
class oneth extends Thread
{

public void run()
{
String sec = txtsec.getText();
int se=Integer.parseInt(sec);
String min=txtmin.getText();
int mi=Integer.parseInt(min);
String hor = txthour.getText();
int hr = Integer.parseInt(hor);

int secleft = 59 - se;
int minleft = 59 - mi;
int hourleft = 23 - hr;




if(secleft<0)
{
secleft = 60+ secleft;
minleft = minleft -1;
}
if(minleft<0)
{
minleft = 60 + minleft;
hourleft = hourleft - 1;
}
if(hourleft<0)
{
return;
}

try
{
sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("错误");
}
}
}

}
本人刚刚接触java,遇到好多不懂的问题!!
最近学拉线程,我编拉一个倒计时的程序,可是
输入秒,分,时,后不会执行~!请问,我这个程序存在什么问题
...全文
93 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
pdvv 2006-05-16
  • 打赏
  • 举报
回复
稍作修改:
class oneth extends Thread
{
public void run()
{
String sec = txtsec.getText();
int se=Integer.parseInt(sec);
String min=txtmin.getText();
int mi=Integer.parseInt(min);
String hor = txthour.getText();
int hr = Integer.parseInt(hor);

int n = hr * 3600 + mi * 60 + se;

while (n > 0) {
n--;
hr = n / 3600;
mi = (n - hr * 3600) / 60;
se = n - (hr * 3600) - mi * 60;

txthour.setText(new Integer(hr).toString());
txtmin.setText(new Integer(mi).toString());
txtsec.setText(new Integer(se).toString());

try {
sleep(1000);
} catch (InterruptedException e) {
System.out.println("退出");
}

btnOK.setText(new Integer(n).toString());
}
}
}

62,610

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧