在java中,如何在标签中每秒显示一下当前系统时间

如题所述

你好,这个很容易实现,开启一个子线程,不断地对标签设置当前的时间

new Thread() {
public void run() {
try {
while (true) {
jLabel1.setText(new Date().toLocaleString());//显示当前时间
Thread.sleep(1000);//暂停一秒
}
} catch (Exception e) {
}
}
}.start();
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-08-27

通过new Date获取当前的日期与时间

示例:

public static void main(String[] args){ 
Date now = new Date(); //获取当前时间
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//格式化当前日期时间,显示如2015/06/27 14:22:22
}

本回答被网友采纳
第2个回答  2011-11-24
你说的是在jsp中吗?
如果是的话在jsp页面有设置<meta http-equiv="refresh" content="1">,
每秒刷新一次,就行了!!
<%Date date = new Date(); %>
<%=date %>//这样获取当前时间
希望对你有帮助。。。
第3个回答  2011-11-19
public class MyDesktopPane extends JFrame implements ActionListener {

final static JDesktopPane desktopPane = new JDesktopPane();

public MyDesktopPane() {
super("MyDesktopPane.java:DesktopPane测试");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("新增窗口");
JMenuItem menultem = new JMenuItem("内部框架窗口");
menu.add(menultem);
menuBar.add(menu);
setJMenuBar(menuBar);
getContentPane().add(desktopPane);
menultem.addActionListener(this);
setSize(350, 200);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
JInternalFrame inFrame = new JInternalFrame("内部框架(圆环)", true, true, true, true);
Container c = inFrame.getContentPane();
CirclePanel circlePanel = new CirclePanel();
JLabel label = new JLabel("圆环");
c.add(circlePanel, BorderLayout.CENTER);
c.add(label, BorderLayout.WEST);
int w = circlePanel.getImageWidthHeight().width + 150;
int h = circlePanel.getImageWidthHeight().height + 50;
inFrame.setSize(w, h);
inFrame.reshape(100, 50, w, h);
inFrame.setOpaque(true);
desktopPane.add(inFrame);
inFrame.setVisible(true);
}

public static void main(String args[]) {
MyDesktopPane app = new MyDesktopPane();
app.addWindowListener(new MyWindowListener());
}
}

class CirclePanel extends JPanel {

private ImageIcon imglcon;

public CirclePanel() {
try {
imglcon = new ImageIcon(new URL(""));
} catch (MalformedURLException ex) {
Logger.getLogger(CirclePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void paint(Graphics g) {
g.drawImage(imglcon.getImage(), 0, 0, this);
}

public Dimension getImageWidthHeight() {
return new Dimension(imglcon.getIconWidth(), imglcon.getIconHeight());
}
}

class MyWindowListener extends WindowAdapter {

public void windowClosing(WindowEvent e) {
System.exit(1);
}
}
相似回答