题目:编写一个线程实现一个数字时钟的应用程序,该线程采用休眠的方式,绝大部分时间让系统使用。急啊,给我代码谢谢

解决方案 »

  1.   

    强分public class 数字时钟 {
       
       public static void main(String[] args) {
           new Thread(new Runnable() {
                public void run() {
                   一个数字时钟,该线程采用休眠的方式,绝大部分时间让系统使用;
                }
            }).start();
       }
    }
      

  2.   

    /**
     * created at 2008/01/03
     */
    package clock;import java.awt.FlowLayout;
    import java.util.Date;import javax.swing.JFrame;
    import javax.swing.JLabel;public class Clock implements Runnable { private JLabel lbvTime = null;

    public Clock() {
    JFrame frame = new JFrame();
    frame.setTitle("Clock");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(300, 100, 190, 50);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.setResizable(false);
    lbvTime = new JLabel();
    frame.add(lbvTime);
    frame.setVisible(true);
    } public void run() {
    while (true) {
    lbvTime.setText(new Date().toString());
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    break;
    }
    }
    }

    public static void main(String[] args) {
    Thread t = new Thread(new Clock());
    t.start();
    }
    }
      

  3.   

    精简版
    /**
     * created at 2008/01/03
     */
    package clock;import java.awt.FlowLayout;
    import java.util.Date;import javax.swing.JFrame;
    import javax.swing.JLabel;public class Clock {
    public static void main(String[] args) {
    new Thread(new Runnable() {
    public void run() {
    JFrame frame = new JFrame();
    frame.setTitle("Clock");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(300, 100, 190, 50);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.setResizable(false);
    JLabel lbvTime = new JLabel();
    frame.add(lbvTime);
    frame.setVisible(true);
    while (true) {
    lbvTime.setText(new Date().toString());
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    break;
    }
    }
    }
    }).start();
    }
    }
      

  4.   

    提示:可以用java.util.Timer+java.util.Timertask
      

  5.   

    最主要就是用到Thread.sleep(time)这个方法,其他的好说
      

  6.   

    6楼的   frame.add(lbvTime);
    这个语句好像有点问题改成 frame.getContentPane().add(lbvTime)
    就可以运行
      

  7.   

    frame.getContentPane()是1.4及以下版本用的
    我那个是1.5的
      

  8.   

    import java.awt.*;
    import javax.swing.*;
    import java.util.Date;public class Crystal extends JFrame implements Runnable
    {
    private JLabel jl1,jl2;
    Date d;
    Thread t;
    public Crystal()
    {
    super();
    t=new Thread(this);
    t.start();
    jl1=new JLabel("显示时间");
    jl2=new JLabel();
    JPanel jp1=new JPanel();
    JPanel jp2=new JPanel();
    jp1.setLayout(new FlowLayout());
    jp1.add(jl1);
    jp2.setLayout(new FlowLayout());
    jp2.add(jl2);
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(jp1,BorderLayout.NORTH);
    this.getContentPane().add(jp2,BorderLayout.CENTER);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public void run()
    {
    while(true)
        {
         try
         {
         t.sleep(1000);
         }
         catch(InterruptedException e)
         {
         System.out.print("线程中断");
         }
         d=new Date();
         jl2.setText(d.toString());
        }
    }
    public static void main(String args[])
    {
    Crystal c=new Crystal();
    c.setBounds(300,300,300,300);
    c.setVisible(true);
    c.setResizable(false);
    }
    }
      

  9.   

    package   clock; import   java.awt.FlowLayout; 
    import   java.util.Date; import   javax.swing.JFrame; 
    import   javax.swing.JLabel; public   class   Clock   implements   Runnable   { private   JLabel   lbvTime   =   null; public   Clock()   { 
    JFrame   frame   =   new   JFrame(); 
    frame.setTitle("Clock"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setBounds(300,   100,   190,   50); 
    frame.getContentPane().setLayout(new   FlowLayout()); 
    frame.setResizable(false); 
    lbvTime   =   new   JLabel(); 
    frame.add(lbvTime); 
    frame.setVisible(true); 
    } public   void   run()   { 
    while   (true)   { 
    lbvTime.setText(new   Date().toString()); 
    try   { 
    Thread.sleep(500); 
    }   catch   (InterruptedException   e)   { 
    e.printStackTrace(); 
    break; 


    } public   static   void   main(String[]   args)   { 
    Thread   t   =   new   Thread(new   Clock()); 
    t.start(); 

    }
      

  10.   

    import   java.awt.*; 
    import   javax.swing.*; 
    import   java.util.Date; public   class   Crystal   extends   JFrame   implements   Runnable 

    private   JLabel   jl1,jl2; 
    Date   d; 
    Thread   t; 
    public   Crystal() 

    super(); 
    t=new   Thread(this); 
    t.start(); 
    jl1=new   JLabel("显示时间"); 
    jl2=new   JLabel(); 
    JPanel   jp1=new   JPanel(); 
    JPanel   jp2=new   JPanel(); 
    jp1.setLayout(new   FlowLayout()); 
    jp1.add(jl1); 
    jp2.setLayout(new   FlowLayout()); 
    jp2.add(jl2); 
    this.getContentPane().setLayout(new   BorderLayout()); 
    this.getContentPane().add(jp1,BorderLayout.NORTH); 
    this.getContentPane().add(jp2,BorderLayout.CENTER); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } 
    public   void   run() 

    while(true) 
            { 
            try 
            { 
            t.sleep(1000); 
            } 
            catch(InterruptedException   e) 
            { 
            System.out.print("线程中断"); 
            } 
            d=new   Date(); 
            jl2.setText(d.toString()); 
            } 

    public   static   void   main(String   args[]) 

    Crystal   c=new   Crystal(); 
    c.setBounds(300,300,300,300); 
    c.setVisible(true); 
    c.setResizable(false); 

    }
      

  11.   

    public class 数字时钟 {
       
       public static void main(String[] args) {
           new Thread(new Runnable() {
                public void run() {
                   一个数字时钟,该线程采用休眠的方式,绝大部分时间让系统使用;
                }
            }).start();
       }
    }
      

  12.   

    package clock;import java.awt.FlowLayout;
    import java.util.Date;import javax.swing.JFrame;
    import javax.swing.JLabel;public class Clock {
        public static void main(String[] args) {
            new Thread(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setTitle("Clock");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setBounds(300, 100, 190, 50);
                    frame.getContentPane().setLayout(new FlowLayout());
                    frame.setResizable(false);
                    JLabel lbvTime = new JLabel();
                    frame.add(lbvTime);
                    frame.setVisible(true);
                    while (true) {
                        lbvTime.setText(new Date().toString());
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            break;
                        }
                    }
                }
            }).start();
        }
    }