主题:线程的一个问题
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class c13_1 extends Thread {
int pauseTime;
String name;
int year,month,date,hour,minute,second;
public c13_1(int hTime, String hStr)
{
pauseTime = hTime;
name = hStr;
}
public void run() {
Calendar now;
for(int i=1;i<500;i++){
try {
now=Calendar.getInstance(); //取系统时间
year=now.get(Calendar.YEAR); //取年值
month=now.get(Calendar.MONTH)+1; //取月值
date=now.get(Calendar.DATE); //取日期值
hour=now.get(Calendar.HOUR_OF_DAY); //取小时值
minute=now.get(Calendar.MINUTE); //取分值
second=now.get(Calendar.SECOND); //取秒值
Thread.sleep(pauseTime);
}
catch(Exception e)
{
System.out.println("线程错误:"+e);
}
}
}
}
public class c13_5
{
public static void main(String argz [] )
{
box box=new box();
box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
box.setVisible(true);
}
}
class box extends JFrame
{
public box()
{
Container cp=getContentPane();
JButton bu1=new JButton("dd");
cp.setLayout(new BorderLayout(10,10));
setSize(950,700);
final c13_1 timer = new c13_1(1000,"a");
final JLabel label=new JLabel();
cp.setLayout(new GridLayout(2,1));
cp.add(label);
cp.add(bu1);
timer.start();
bu1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
label.setText(" "+timer.year+" 年"+timer.month+"月 "+timer.date+"日"+timer.hour+"小时"+timer.minute+"分"+timer.second+"秒" );
}
});
}
}
这个程序是点一次鼠标,LABEL上就显示当前的时间.这样有很麻烦呀,请问有没有什么方法能不用点鼠标,就直接显示当前时间的呀?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class c13_1 extends Thread {
int pauseTime;
String name;
int year,month,date,hour,minute,second;
public c13_1(int hTime, String hStr)
{
pauseTime = hTime;
name = hStr;
}
public void run() {
Calendar now;
for(int i=1;i<500;i++){
try {
now=Calendar.getInstance(); //取系统时间
year=now.get(Calendar.YEAR); //取年值
month=now.get(Calendar.MONTH)+1; //取月值
date=now.get(Calendar.DATE); //取日期值
hour=now.get(Calendar.HOUR_OF_DAY); //取小时值
minute=now.get(Calendar.MINUTE); //取分值
second=now.get(Calendar.SECOND); //取秒值
Thread.sleep(pauseTime);
}
catch(Exception e)
{
System.out.println("线程错误:"+e);
}
}
}
}
public class c13_5
{
public static void main(String argz [] )
{
box box=new box();
box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
box.setVisible(true);
}
}
class box extends JFrame
{
public box()
{
Container cp=getContentPane();
JButton bu1=new JButton("dd");
cp.setLayout(new BorderLayout(10,10));
setSize(950,700);
final c13_1 timer = new c13_1(1000,"a");
final JLabel label=new JLabel();
cp.setLayout(new GridLayout(2,1));
cp.add(label);
cp.add(bu1);
timer.start();
bu1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
label.setText(" "+timer.year+" 年"+timer.month+"月 "+timer.date+"日"+timer.hour+"小时"+timer.minute+"分"+timer.second+"秒" );
}
});
}
}
这个程序是点一次鼠标,LABEL上就显示当前的时间.这样有很麻烦呀,请问有没有什么方法能不用点鼠标,就直接显示当前时间的呀?