求大神们帮忙看一下这个倒计时代码
程序代码:
package 计时器1; import java.awt.BorderLayout; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; class Show extends JFrame//倒计时面板。 { Button but1,but2,but3; JTextField jt; JPanel jp1,jp2; public void inint()//面板的初始化 { but1=new Button("开始"); but2=new Button("暂停"); but3=new Button("继续"); jt=new JTextField("00:00:00",10); jp1=new JPanel(); jp2=new JPanel(); jp1.add(jt); jp2.add(but1); jp2.add(but2); jp2.add(but3); this.setTitle("倒计时"); this.setLayout(new BorderLayout()); this.add("North",jp1); this.add("Center",jp2); this.setVisible(true); this.setBounds(200, 200, 200, 200); but1.addActionListener(new ActionListener() { //接口,计算总秒数。并且开启线程 @Override public void actionPerformed(ActionEvent e) { int second=0; String[] s=null; if(jt.getText().equals("00:00:00")) javax.swing.JOptionPane.showMessageDialog(null, "请输入计时的总时间"); else { s=jt.getText().split(":");//把字符串按照字符":"拆分,反回的值是字符串数组。 int[] count=new int[3]; for(int i=0;i<3;i++) count[i]=Integer.parseInt(s[i]);//把字符串转化为整形。 second=second+count[0]*(int)Math.pow(60, 2)+count[1]*(int)Math.pow(60, 1)+count[2];//计算总秒数 Show sh=new Show(); Mythread my=new Mythread(second,sh); Thread t=new Thread(my); t.start(); } } }); // but2.addActionListener(); } } class Mythread implements Runnable//线程 { Show sh; boolean flage=true; private int s=0; private int second=0; public Mythread(int s,Show sh) { this.s=s; this.sh=sh; } public void set(boolean flage) { this.flage=flage; } public void run() { try { while(flage==true) { if(s>=0) { display(this.s); Thread.sleep(1000); this.s--; } else break; } } catch (InterruptedException e) {} } public void display(int second)//显示倒计时 { int h=0,m=0; while(second>=60) { m=m+(second/60); second=second%60; } while(m>=60) { h=h+(m/60); m=m%60; } sh.jt.setText(h+":"+m+":"+second); } } public class JiShi2 { public static void main(String[] args) { new Show().inint(); } }请问怎么回事啊!!!
[ 本帖最后由 笔墨痕干 于 2014-11-24 14:22 编辑 ]