2014-02-18 47 views
1

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

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(); 

    } 
} 

} 

任何答复可以理解做出一些错误。

+0

参见[如何创建一个数字时钟(http://stackoverflow.com/a/21619240/2587435) –

+0

我很遗憾地说,但你的“示例代码”让我笑。如果我可能会重申你的问题:*我想建立一个复杂的企业项目,我已经有一个Java Hello World程序。请提供其余的内容!* xD我通常会添加一些有用的链接来指导教程,但是已经有一些有用的答案。 – SebastianH

+0

将标签添加到刷新时间文本的“JOptionPane”。请参阅上面的链接。 –

回答

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); 
    }} 
+0

非常感谢,这是一个聪明的事情,增加了一个计时器 – user3314952

+0

不客气,不要忘记+1 :) lool –

+0

嘿我试图做到这一点,但没有奏效我会在新的代码评论.. – user3314952

1

“当我刚刚把它写这样它会在第二个我运行它不会被改变的日期。 “

只需添加一个JLabelJOPtionPane,只更新在TimerJLabel。不要把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); 
      } 
     }); 
    } 
}