java写个倒记时 哎,刚开始....不会...只知道要用TIMER 和TIMERTASK......汗,看了API没感觉...晕 就写个简单的倒记时,如1分钟倒记时....希望高人指点一二,怎么有些人看了API就会了....我看了n 久都没感觉 郁闷啊...还是不会... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你这个倒记时要实现什么功能?是在窗口上持续显示60、59、58...还是类似于一个sleep,执行60秒,然后返回就可以? 哎,应该是60 59 58 我现在用了个比较麻烦的......参考了一个闹钟的代码 final Action taskPerformer = new AbstractAction() { public void actionPerformed(ActionEvent evt) { //显示时间 long time2=System.currentTimeMillis()+time;//这里的time是倒记时的总时间 Date d=new Date(time2-System.currentTimeMillis()-second); //获得倒记时的时间 label.setText(sdf.format(d)); label.repaint(); second+=1000;//一秒一秒的减 } }; button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { //startTime.setTime(new Date().getTime()); timer=new Timer(1000, taskPerformer); timer.start(); } }); button_1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { if(timer!=null&&timer.isRunning()) timer.stop(); else if(!timer.isRunning()) timer.restart(); } });哎,感觉不太好..... 汗,就是利用second 每次运行的时候就是设定的倒记时总时间time减去second 因为每次运行时second都加了一秒 哎,感觉不理想 不过我没办法了希望大家指点一下 import java.awt.FlowLayout;import java.awt.event.*;import java.text.SimpleDateFormat;import java.util.Calendar;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.JTextField;import javax.swing.JOptionPane;public class TimeCount extends JFrame implements ActionListener,Runnable{ private JButton start; private JTextField in; private JLabel lbNow, lbNowTitle, lbLeftSecTitle, lbLeftSec, lbLeftMinTitle, lbLeftMin,lbMin; private Thread clocker,time; long startTime,endTime; long nowTime, leftTime, leftSec, leftMin; boolean flag=false; public TimeCount() { this.setLayout(new FlowLayout()); this.setResizable(false); this.setSize(200, 170); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocation(830, 580); initUI(); clocker = new Thread(this,"clock"); clocker.start(); time=new Thread(this,"ti"); time.start(); } private void initUI() { lbNowTitle = new JLabel("当前时间为:"); lbNow = new JLabel(); lbLeftSecTitle = new JLabel("离结束还有:"); lbLeftSec = new JLabel("未设置"); lbLeftMinTitle = new JLabel("离结束还有:"); lbLeftMin = new JLabel("未设置"); lbMin = new JLabel("倒计时(单位:秒)"); in = new JTextField(5); start = new JButton("Start"); start.addActionListener(this); this.add(lbNowTitle); this.add(lbNow); this.add(lbLeftSecTitle); this.add(lbLeftSec); this.add(lbLeftMinTitle); this.add(lbLeftMin); this.add(lbMin); this.add(in); this.add(start); } public void actionPerformed(ActionEvent e){ if(e.getSource()==start){ Calendar startCalendar = Calendar.getInstance(); startTime = startCalendar.getTime().getTime(); // 获得开始时候的那个时间点 endTime = startTime + Long.parseLong(in.getText()) * 1000; // 从开始时刻开始 加 flag = true; } } public void run() { SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); Calendar now; while(((Thread.currentThread()).getName()).equals("clock")) { now = Calendar.getInstance(); nowTime = now.getTime().getTime(); lbNow.setText(dateFormat.format(now.getTime())); } while(((Thread.currentThread()).getName()).equals("ti")) while(flag){ leftTime = endTime - nowTime; leftSec = leftTime / 1000; leftMin = leftTime / (60 * 1000); lbLeftSec.setText(leftSec + " 秒"); //若后面不加字符,可以lbLeftSec.setText(leftSec + ""); 不用String.valueOf lbLeftMin.setText(leftMin + " 分钟"); if(leftSec == 0) { flag=false; JOptionPane.showMessageDialog(this, "对不起!时间已到!", "提示", JOptionPane.OK_OPTION); break; } try { Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { new TimeCount(); }}这个还可以自己可以对见面修改 哦没有注意这是JAVA写的 多线程的socket代码,有几个问题未解决 byte和char的getbytes问题请教高手 同步 synchronized 的问题 输出汉字的uincode编码 (高手請進)nio是非阻滯的,但是不是能同時處理多個請求呢? 请教在JSP文件中加入JApplet的方法(在JApplet中可能带有Swing组件) 怎么实现 变维数组 就是用 变量 n 来确定 数组的维数,并遍历数组 Applet显示中文是乱码。请大家帮帮忙。 新手关于异常类的问题 接口的常量为什么必须是static?而方法缺不能是static? Java反射机制中class属性的问题 JAVA显示图片问题
我现在用了个比较麻烦的......参考了一个闹钟的代码 final Action taskPerformer = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
//显示时间
long time2=System.currentTimeMillis()+time;//这里的time是倒记时的总时间
Date d=new Date(time2-System.currentTimeMillis()-second); //获得倒记时的时间
label.setText(sdf.format(d));
label.repaint();
second+=1000;//一秒一秒的减
}
}; button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
//startTime.setTime(new Date().getTime());
timer=new Timer(1000, taskPerformer);
timer.start();
}
});
button_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(timer!=null&&timer.isRunning())
timer.stop();
else if(!timer.isRunning())
timer.restart();
}
});哎,感觉不太好.....
哎,感觉不理想 不过我没办法了
希望大家指点一下
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JOptionPane;public class TimeCount extends JFrame implements ActionListener,Runnable
{
private JButton start;
private JTextField in; private JLabel lbNow, lbNowTitle, lbLeftSecTitle, lbLeftSec, lbLeftMinTitle, lbLeftMin,lbMin; private Thread clocker,time;
long startTime,endTime;
long nowTime, leftTime, leftSec, leftMin;
boolean flag=false; public TimeCount()
{
this.setLayout(new FlowLayout());
this.setResizable(false);
this.setSize(200, 170);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(830, 580);
initUI();
clocker = new Thread(this,"clock");
clocker.start();
time=new Thread(this,"ti");
time.start();
} private void initUI()
{
lbNowTitle = new JLabel("当前时间为:");
lbNow = new JLabel();
lbLeftSecTitle = new JLabel("离结束还有:");
lbLeftSec = new JLabel("未设置");
lbLeftMinTitle = new JLabel("离结束还有:");
lbLeftMin = new JLabel("未设置");
lbMin = new JLabel("倒计时(单位:秒)");
in = new JTextField(5);
start = new JButton("Start");
start.addActionListener(this);
this.add(lbNowTitle);
this.add(lbNow);
this.add(lbLeftSecTitle);
this.add(lbLeftSec);
this.add(lbLeftMinTitle);
this.add(lbLeftMin);
this.add(lbMin);
this.add(in);
this.add(start);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==start){
Calendar startCalendar = Calendar.getInstance();
startTime = startCalendar.getTime().getTime(); // 获得开始时候的那个时间点
endTime = startTime + Long.parseLong(in.getText()) * 1000; // 从开始时刻开始 加
flag = true;
}
} public void run()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
Calendar now;
while(((Thread.currentThread()).getName()).equals("clock"))
{
now = Calendar.getInstance();
nowTime = now.getTime().getTime();
lbNow.setText(dateFormat.format(now.getTime()));
}
while(((Thread.currentThread()).getName()).equals("ti"))
while(flag){
leftTime = endTime - nowTime;
leftSec = leftTime / 1000;
leftMin = leftTime / (60 * 1000);
lbLeftSec.setText(leftSec + " 秒"); //若后面不加字符,可以lbLeftSec.setText(leftSec + ""); 不用String.valueOf
lbLeftMin.setText(leftMin + " 分钟");
if(leftSec == 0)
{ flag=false;
JOptionPane.showMessageDialog(this, "对不起!时间已到!", "提示", JOptionPane.OK_OPTION);
break;
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
} public static void main(String[] args)
{
new TimeCount();
}
}
这个还可以
自己可以对见面修改
没有注意
这是JAVA写的