请高手帮看看为什么在出来的 textField这个里面不能连续显示当前的时间?若要显示的话应该怎么修改?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import java.util.*;
public class FieldEventFrame extends JFrame
{
JPanel contentPane=(JPanel) this.getContentPane();
//JLabel label1=new JLabel("商品单价");
JTextField textField=new JTextField(" ");
public FieldEventFrame(){
contentPane.setLayout(new GridLayout(4,1,4,4));
contentPane.add(textField);
setSize(250,150);
setVisible(true);
textField.addActionListener(new TextFrame());
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){public void run(){
FieldEventFrame frame=new FieldEventFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
class TextFrame extends Thread implements ActionListener
{
public void run(){
while(true){
Date date = new Date();
Calendar c=Calendar.getInstance();
TimeZone timeZone = c.getTimeZone();
c.setTime(date);
String k=c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH) + 1)+"月"+
c.get(Calendar.DATE)+"日"+c.get(Calendar.HOUR)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND);
textField.setText(k);
}
}
public void actionPerformed(ActionEvent e)
{
TestTime tm=new TestTime();
tm.start();
}
}
} |
|