刷新的Java日期

时间:2014-02-18 16:40:24

标签: java swing date

我想在JFrame中创建一个java.util.Date显示,并始终刷新以查看新日期。

package pro;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Date {

public static Timer t;


public static void main(String [] args){

    time();

}
public static Timer time(){

t = new Timer(1000, new ActionListener(){

@SuppressWarnings("deprecation")

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, new java.util.Date().toGMTString());

        }

    });     

    t.setRepeats(true);

    t.start();

    return t;
}
}

我想我在计时器方法中犯了一些错误

我也尝试过编辑

package pro;

import java.awt.FlowLayout;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class date {

private static JLabel l;
private static Date d = new Date();
private static JFrame f;

@SuppressWarnings("deprecation")

public static void main(String [] args){

    f = new JFrame("Date program");

    f.setVisible(true);
    f.pack();
    f.revalidate();
    f.setLayout(new FlowLayout(FlowLayout.LEFT));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    l = new JLabel(d.toGMTString());


    f.add(l);
    while(true){

    l.revalidate();

    }
}

}

任何回复都将不胜感激。

2 个答案:

答案 0 :(得分:1)

  

“当我这样写它时,它会在第二个时间内获取日期而不会被更改。”

只需向JLabel添加JOPtionPane,然后只更新JLabel中的Timer即可。不要将JOptionPane放在Timer代码中。这是一个例子

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Clock {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                final Date date = new Date();
                final JLabel timeLabel = new JLabel(date.toString());
                Timer timer = new Timer(1000, new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        date.setTime(System.currentTimeMillis());
                        timeLabel.setText(date.toString());
                    }
                });
                timer.start();
                JOptionPane.showMessageDialog(null, timeLabel);
            }
        });
    }
}

答案 1 :(得分:0)

使用计时器执行此操作

t = new javax.swing.Timer(1000, new ActionListener() {
          public void actionPerformed(ActionEvent e) {


JOptionPane.showMessageDialog(null , new SimpleDateFormat("HH:mm:ss", Locale.FRANCE).format(new Date()));
}
       });
        t.start();

<强> EDITED

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class pro6 extends JFrame{
public  static Timer t;
public JLabel jlabel4;
public pro6() {
        System.out.println("heloo");
this.setLayout(null);
       jlabel4 = new JLabel();
       jlabel4.setBounds(0, 0, 100, 30);
       this.add(jlabel4);
       this.pack();

t = new Timer(1000, new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        jlabel4.setText(new SimpleDateFormat("HH:mm:ss", Locale.FRANCE).format(new Date()));

        }

    });

 t.start();

 setSize(new Dimension(200, 60));

 setDefaultCloseOperation(DISPOSE_ON_CLOSE);

}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       new pro6().setVisible(true);
    }}