Java应用程序中的活动时钟

时间:2016-11-20 16:56:45

标签: java

我做了:

import java.util.Timer;

public class Time
 {
 public static java.util.Date getDate()
 {
     java.util.Date date = new java.util.Date();
    return date;         
 }

 public static java.text.SimpleDateFormat getFormat()
 {
     java.text.SimpleDateFormat format = new  java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     return format;
 }
 }
     ..... JLabel lblNewLabel_4 = new    JLabel((Time.getFormat().format(Time.getDate())));
            lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
            lblNewLabel_4.setFont(new Font("Tahoma", Font.PLAIN, 15));
            lblNewLabel_4.setBounds(521, 13, 174, 28);
            panelMain.add(lblNewLabel_4);....

我想制作Jlabel,它会显示我试过的实时日期,但显然它不起作用。

3 个答案:

答案 0 :(得分:1)

你应该创建并运行一个执行无限循环的线程,并以固定的间隔实例化一个新的日期(这里是一秒,但根据你的需要,它可能或多或少) 从这个新日期开始,标签将更新为

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class LiveDateSwing extends JFrame {

    private JLabel label;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");

    public static void main(String[] args) {
      new LiveDateSwing();
    }

    public LiveDateSwing() {
      label = new JLabel(" ");
      add(label);
      setVisible(true);

      pack();

      Runnable runnable = new Runnable() {

        @Override
        public void run() {
          while (true) {
            Date date = getDate();
            String dateString = simpleDateFormat.format(date);
            label.setText(dateString);
            try {
              Thread.sleep(1000);
            }
            catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
    };

      Thread t = new Thread(runnable);
      t.start();
    }

    public static java.util.Date getDate() {
      java.util.Date date = new java.util.Date();
      return date;
    }
}

答案 1 :(得分:0)

这是一个简单的实时动态时钟java程序,它可能会帮助某人..很多R& D。

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Delayed;

public class timer
{

    public static void main(String[] args)
    {

    Timer timer = new Timer();

         timer.scheduleAtFixedRate(new TimerTask()
         {
            public void run()
            {
                Robot robbie;

                int second, minute, hour;
                Calendar date = Calendar.getInstance();
                second = date.get(Calendar.SECOND);
                minute = date.get(Calendar.MINUTE);
                hour = date.get(Calendar.HOUR);
                System.out.println("Current time is  " + hour + " : " + 
                minute +" : " + second);

                try
                {
                    robbie = new Robot();
                    robbie.keyPress(KeyEvent.VK_ALT);
                    robbie.keyPress(KeyEvent.VK_SHIFT);
                    robbie.keyPress(KeyEvent.VK_Q);
                    robbie.keyRelease(KeyEvent.VK_ALT);
                    robbie.keyRelease(KeyEvent.VK_SHIFT);
                    robbie.keyRelease(KeyEvent.VK_Q);
                    robbie.keyPress(KeyEvent.VK_C);
                    robbie.keyRelease(KeyEvent.VK_C);
                    robbie.keyPress(KeyEvent.VK_SHIFT);
                    robbie.keyPress(KeyEvent.VK_F10);
                    robbie.keyPress(KeyEvent.VK_R);
                    robbie.keyRelease(KeyEvent.VK_SHIFT);
                    robbie.keyRelease(KeyEvent.VK_F10);
                    robbie.keyRelease(KeyEvent.VK_R);
                } catch (AWTException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, 1 * 500, 1 * 500);
    }
}

答案 2 :(得分:0)

这是具有在applet中创建的日期的简单实时数字手表。 我尝试过,它正在工作。.

TokenValidationParameters