java 计时器
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Miao extends JFrame implements ActionListener
{
JButton button1;
JButton button2;
JTextField tf;
JPanel panel;
Timer time;
int shu=0;
int miao=0;
int minute=0;
int hour;
int day;
int year;
int century;
JLabel label;
/**
* Method actionPerformed
*
*
* @param e
*
*/
public Miao()
{
this.xian();
this.addlistener();
}
public void xian()
{
panel = new JPanel();
time = new Timer(1,this);
label = new JLabel("0");
this.button1 = new JButton("开始");
this.button2 = new JButton("停止");
this.tf = new JTextField(30);
panel.add(label);
System.out.println ("label-width"+this.label.getWidth()+"");
System.out.println ("label-height"+this.label.getHeight()+"");
panel.add(tf);
panel.add(this.button1);
panel.add(this.button2);
this.getContentPane().add(panel);
this.setBounds(200,20,350,350);
this.setVisible(true);
this.setTitle("计数器");
}
public void addlistener()
{
this.button1.addActionListener(this);
this.button2.addActionListener(this);
// time.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
// TODO: Add your code heree.getSource()==this.button1
if(e.getSource()==this.button1)
{
System.out.println ("开始");
this.shu=0;
this.miao=0;
this.minute=0;
this.hour=0;
this.day=0;
this.year=0;
this.century=0;
this.tf.setText(0+"");
time.start();
}
if(e.getSource()==this.button2)
{
System.out.println ("停止");
time.stop();
}
if(e.getSource()==time)
{
// System.out.println ("time");
this.shu++;
// System.out.println (shu);
this.tf.setText(this.century+"世纪"+this.year+"年"+this.day+"日"+this.hour+":"+minute+":"+miao+":"+shu+"");
//label.setText(shu+"");
if(shu%100==0)
{
this.shu=0;
this.miao++;
// tf.setText(minute+":"+miao+":"+shu);
System.out.println ("label-width"+this.label.getWidth()+"");
System.out.println ("label-height"+this.label.getHeight()+"");
if(this.miao%60==0)
{
this.miao=0;
this.minute++;
if(this.minute%60==0)
{
this.minute=0;
this.hour++;
if(this.hour%24==0)
{
this.hour=0;
this.day++;
if(this.day%365==0)
{
this.year++;
this.day=0;
if(this.year%100==0)
{
this.year=0;
this.century++;
}
}
}
}
}
}
}
}
public static void main(String as[])
{
new Miao();
}
}
posted on 2011-07-02 23:47
sanmao 阅读(220)
评论(0) 编辑 收藏