package com.test;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.*;
public class TimeMain extends JFrame implements ActionListener{
JPanel jp1,jp2;
JLabel jbl1,jbl2,jblZong,jblHour,jblMin,jblSec,jblZong2,jblHour2,jblMin2,jblSec2;
JTextField jtf1,jtf2,jtfHour,jtfMin,jtfSec,jtfHour2,jtfMin2,jtfSec2;
JButton jb1,jb2;
public TimeMain(){
jp1=new JPanel();//new GridLayout(2, 2));
jbl1=new JLabel("当前时间:",JLabel.CENTER);
jbl2=new JLabel("倒计时间:",JLabel.CENTER);
jblZong=new JLabel("设定初始时间:",JLabel.CENTER);
jblHour=new JLabel("时");
jblMin=new JLabel("分");
jblSec=new JLabel("秒");
jblZong2=new JLabel("设定终止时间:",JLabel.CENTER);
jblHour2=new JLabel("时");
jblMin2=new JLabel("分");
jblSec2=new JLabel("秒");
jtf1=new JTextField(15);
jtf2=new JTextField(15);
jtfHour=new JTextField(3);
jtfMin=new JTextField(3);
jtfSec=new JTextField(3);
jtfHour2=new JTextField(3);
jtfMin2=new JTextField(3);
jtfSec2=new JTextField(3);
jp1.add(jbl1);
jp1.add(jtf1);
jp1.add(jbl2);
jp1.add(jtf2);
jp1.add(jblZong);
jp1.add(jtfHour);
jp1.add(jblHour);
jp1.add(jtfMin);
jp1.add(jblMin);
jp1.add(jtfSec);
jp1.add(jblSec);
jp1.add(jblZong2);
jp1.add(jtfHour2);
jp1.add(jblHour2);
jp1.add(jtfMin2);
jp1.add(jblMin2);
jp1.add(jtfSec2);
jp1.add(jblSec2);
jp2=new JPanel();
jb1=new JButton("开始");
jb1.addActionListener(this);
jb2=new JButton("开始倒计时");
jb2.addActionListener(this);
jp2.add(jb1);
//jp2.add(jb2);
this.add(jp1);
this.add(jp2,"South");
this.setSize(300,200);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TimeMain tt=new TimeMain();
}
class TimeZheng extends Thread{
int hour;
int hour2;
int min;
int min2;
int sec;
int sec2;
public TimeZheng(int hour,int min,int sec,int hour2,int min2,int sec2){
this.hour=hour;
this.hour2=hour2;
this.min2=min2;
this.min=min;
this.sec2=sec2;
this.sec=sec;
}
public void run(){
while(true){
//Date date=new Date();
jtf1.setText(hour+":"+min+":"+sec);
if(hour==hour2&&min==min2&&sec==sec2){
break;
}
if(sec==59){
sec=0;
if(min==59){
min=0;
if(hour==24){
hour=0;
}else{
hour++;
}
}else{
min++;
}
}else{
sec++;
}
//System.out.println("当前时间 "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
class TimeFan extends Thread{
int hour;
int min;
int sec;
public TimeFan(int hour,int min,int sec){
this.hour=hour;
this.min=min;
this.sec=sec;
}
public void run(){
while(true){
jtf2.setText(hour+":"+min+":"+sec);
if(hour==0&&min==0&&sec==0){
break;
}
if(sec==0){
sec=59;
}else{
sec--;
}
if(sec==59){
if(min==0){
min=59;
if(hour==0){
hour=24;
}else{
hour--;
}
}else{
min--;
}
}
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb1){
int temp1,temp2;
if(jtfHour.getText().equals("")||jtfMin.getText().equals("")||jtfSec.getText().equals("")||
jtfHour2.getText().equals("")||jtfMin2.getText().equals("")||jtfSec2.getText().equals("")){
JOptionPane.showMessageDialog(this, "请输入完整时间......");
}else{
int hour=Integer.parseInt(jtfHour.getText().trim().toString());
int min=Integer.parseInt(jtfMin.getText().trim().toString());
int sec=Integer.parseInt(jtfSec.getText().trim().toString());
int hour2=Integer.parseInt(jtfHour2.getText().trim().toString());
int min2=Integer.parseInt(jtfMin2.getText().trim().toString());
int sec2=Integer.parseInt(jtfSec2.getText().trim().toString());
new TimeZheng(hour,min,sec,hour2,min2,sec2).start();
temp1=hour*3600+min*60+sec;
temp2=hour2*3600+min2*60+sec2;
if(temp1>temp2){
JOptionPane.showMessageDialog(this, "错误:终止时间小于初始时间!");
}else{
temp2=temp2-temp1;
}
new TimeFan(temp2/3600,(temp2%3600)/60,(temp2%3600)%60).start();
}
}
}
}
- 1
- 2
前往页