JDK下demo下有个applet是弄时钟的 去看看吧

解决方案 »

  1.   

    我把你的改了一下
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.text.SimpleDateFormat;public class JClockFrame {
    public static void main(String[] args){
    SimleFrame simpleFrame = new SimleFrame();
    simpleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    simpleFrame.show();
    }
    }class SimleFrame extends JFrame{
    public SimleFrame(){
    setSize(300,80);
    setTitle("JClock");
    ClockPanel panel = new ClockPanel();
    Container contentPane = getContentPane();
    contentPane.add(panel);
    }
    }class ClockPanel extends JPanel {
    Date curDate;
    public ClockPanel()

    curDate= new Date(System.currentTimeMillis());
    javax.swing.Timer oneSecondTimer = new javax.swing.Timer(1000,new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    curDate=new Date(System.currentTimeMillis());
    repaint();
    }

    });

    oneSecondTimer.start();
    }
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss  ");

    String str = formatter.format(curDate);

    g.drawString(str,X,Y);
    }
    public static final int X=50;
    public static final int Y=50;
    }