package thread;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Time extends JFrame{
Scanner s = new Scanner(System.in);
JFrame jf;
Container cn;
JPanel jp1;
JPanel jp2;
JPanel jp3;
JLabel j0;
JTextField tx1;
JTextField tx2;
JTextField tx3;
JLabel j1;
JLabel j2;
JLabel j3;
JTextField tx4;
JTextField tx5;
JTextField tx6;
JTextField tx7;
JTextField tx8;
JTextField tx9;
JLabel j4;
JLabel j5;
JButton jb2;
public Time(){
jf = new JFrame();
cn = jf.getContentPane();
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
j0 = new JLabel("倒计时器",JLabel.CENTER);
jp1.setBackground(Color.red);
jp2.setBackground(Color.GREEN);
jp3.setBackground(Color.blue);
tx1 = new JTextField(2);
tx2 = new JTextField(2);
tx3 = new JTextField(2);
j1 = new JLabel("小时");
j2 = new JLabel("分钟");
j3 = new JLabel("秒");
tx4 = new JTextField(2);
tx5 = new JTextField(2);
tx6 = new JTextField(2);
tx7 = new JTextField(2);
tx8 = new JTextField(2);
tx9 = new JTextField(2);
j4 = new JLabel(":");
j5 = new JLabel(":");
jb2 = new JButton("开始");
init();
}
class TT implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
int hour = Integer.parseInt(tx1.getText());
int minute = Integer.parseInt(tx2.getText());
int second = Integer.parseInt(tx3.getText());
int time = hour * 3600 + minute * 60 + second;
while (time >= 0) {
hour = time / 3600;
int a1 = (int) (hour / 10);
int a2 = (int)(hour % 10);
minute = (time - hour * 3600) / 60;
int a3 = (int) (minute / 10);
int a4 = (int) (minute % 10);
second = time - hour * 3600 - minute * 60;
int a5 = (int) (second / 10);
int a6 = (int)(second % 10);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tx4.setText(String.valueOf(a1));
tx5.setText(String.valueOf(a2));
tx6.setText(String.valueOf(a3));
tx7.setText(String.valueOf(a4));
tx8.setText(String.valueOf(a5));
tx9.setText(String.valueOf(a6));
tx4.repaint();
tx5.repaint();
tx6.repaint();
tx7.repaint();
tx8.repaint();
tx9.repaint();
time--;
}
}
}
private void init() {
cn.setLayout(new BorderLayout());
cn.add(jp1,BorderLayout.NORTH);
cn.add(jp2,BorderLayout.SOUTH);
cn.add(jp3,BorderLayout.CENTER);
jp1.add(j0);
jp2.setLayout(new FlowLayout());
jp2.add(tx1);
jp2.add(j1);
jp2.add(tx2);
jp2.add(j2);
jp2.add(tx3);
jp2.add(j3);
jp3.add(tx4);
jp3.add(tx5);
jp3.add(j4);
jp3.add(tx5);
jp3.add(tx6);
jp3.add(j4);
jp3.add(tx6);
jp3.add(tx7);
jp3.add(j5);
jp3.add(tx8);
jp3.add(tx9);
jp3.add(jb2);
jf.setVisible(true);
cn.setLocation(300, 400);
jf.setSize(300, 200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void getTime(){
jb2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
TT t = new TT();
Thread tt = new Thread(t);
tt.start();
}
});
}
public static void main(String[] args) {
Time t = new Time();
t.getTime();
}
}
终于把昨天没有实现的功能实现了,突然有了一点满足感,但是缺点还是很多,比如界面很丑,而且功能不是很丰富,希望自己明天可以再接再厉继续完善。加油
倒计时器(2)
最新推荐文章于 2024-04-27 18:24:51 发布