写了一个JAVA倒计时程序,和大家交流下。
import java.awt.Container;
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TimeTest2 extends JFrame implements Runnable{
JFrame j;
JLabel jt = new JLabel();
JLabel jt1,jt2,jt3,jt4,jt5,jt6,jt7;
Thread clocker,cl;
JButton b2;
Container c;
String ss;
long m;
public TimeTest2(){
j = new JFrame();
c =j.getContentPane();
j.setLayout(new FlowLayout());
jt1 = new JLabel("当前时间为:");
jt2 = new JLabel("离考试结束还有:");
jt3 = new JLabel();
jt4 = new JLabel("秒");
jt5 = new JLabel("离考试结束还有:");
jt6 = new JLabel("");
jt7 = new JLabel("分钟");
jt3 = new JLabel();
j.add(jt1);j.add(jt);
j.add(jt2);j.add(jt3);j.add(jt4);
j.add(jt5);j.add(jt6);j.add(jt7);
jt3.setVisible(false);
jt6.setVisible(false);
j.setResizable(false);
j.setSize(200,150);
j.setLocation(350, 250);
j.setVisible(true);
j.setDefaultCloseOperation(EXIT_ON_CLOSE);
clocker=new Thread(this);
cl=new Thread(this);
clocker.start();
j.setLocation(830, 580);
Calendar rg = Calendar.getInstance();
rg.setTime(new java.util.Date());
m =rg.getTime().getTime(); //获得开始时候的那个时间点
}
public void run(){
while(true){
SimpleDateFormat df =new SimpleDateFormat("hh:mm:ss");
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(new java.util.Date());
long l1 = rightNow.getTime().getTime () ;
long l2 = m + 2*60*60*1000; // 从开始时刻开始 加两个小时
long mm= l2-l1;
long s = mm/(1000);
long min =mm/(60*1000);
String ss =String.valueOf(s);
String mi =String.valueOf(min);
jt3.setText(ss);
jt6.setText(mi);
jt.setText(df.format(new java.util.Date()));
try{
Thread.sleep(1);
}catch(InterruptedException e){}
j.setLocation(830, 580);
jt3.setVisible(true);
jt6.setVisible(true);
if( min==118 ){
JOptionPane.showMessageDialog( null,"对不起!答题时间已到!", "提示", JOptionPane.OK_CANCEL_OPTION);
}
}
}
public static void main(String[] args) {
new TimeTest2();
}
}
我刚刚学java
昨天写了个倒计时的程序
拿出来看下,
还有没有其他的写法
交流下`
还有对我写程序的风格有什么地方需要改的也说说`
谢谢
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TimeTest2 extends JFrame implements Runnable{
JFrame j;
JLabel jt = new JLabel();
JLabel jt1,jt2,jt3,jt4,jt5,jt6,jt7;
Thread clocker,cl;
JButton b2;
Container c;
String ss;
long m;
public TimeTest2(){
j = new JFrame();
c =j.getContentPane();
j.setLayout(new FlowLayout());
jt1 = new JLabel("当前时间为:");
jt2 = new JLabel("离考试结束还有:");
jt3 = new JLabel();
jt4 = new JLabel("秒");
jt5 = new JLabel("离考试结束还有:");
jt6 = new JLabel("");
jt7 = new JLabel("分钟");
jt3 = new JLabel();
j.add(jt1);j.add(jt);
j.add(jt2);j.add(jt3);j.add(jt4);
j.add(jt5);j.add(jt6);j.add(jt7);
jt3.setVisible(false);
jt6.setVisible(false);
j.setResizable(false);
j.setSize(200,150);
j.setLocation(350, 250);
j.setVisible(true);
j.setDefaultCloseOperation(EXIT_ON_CLOSE);
clocker=new Thread(this);
cl=new Thread(this);
clocker.start();
j.setLocation(830, 580);
Calendar rg = Calendar.getInstance();
rg.setTime(new java.util.Date());
m =rg.getTime().getTime(); //获得开始时候的那个时间点
}
public void run(){
while(true){
SimpleDateFormat df =new SimpleDateFormat("hh:mm:ss");
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(new java.util.Date());
long l1 = rightNow.getTime().getTime () ;
long l2 = m + 2*60*60*1000; // 从开始时刻开始 加两个小时
long mm= l2-l1;
long s = mm/(1000);
long min =mm/(60*1000);
String ss =String.valueOf(s);
String mi =String.valueOf(min);
jt3.setText(ss);
jt6.setText(mi);
jt.setText(df.format(new java.util.Date()));
try{
Thread.sleep(1);
}catch(InterruptedException e){}
j.setLocation(830, 580);
jt3.setVisible(true);
jt6.setVisible(true);
if( min==118 ){
JOptionPane.showMessageDialog( null,"对不起!答题时间已到!", "提示", JOptionPane.OK_CANCEL_OPTION);
}
}
}
public static void main(String[] args) {
new TimeTest2();
}
}
我刚刚学java
昨天写了个倒计时的程序
拿出来看下,
还有没有其他的写法
交流下`
还有对我写程序的风格有什么地方需要改的也说说`
谢谢
...全文


Java SE
