java 动态显示时间
importjava.awt.EventQueue;importjava.awt.event.ActionEvent;importjava.awt.event.Actio...
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class timetext {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
timetext window = new timetext();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public timetext() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
String s=String.format("%tY-%tm-%td %tH:%tM:%tS",new Date(),new Date(),new Date(),new Date(),new Date(),new Date());
JLabel lblNewLabel = new JLabel(s);
lblNewLabel.setBounds(158, 116, 137, 15);
frame.getContentPane().add(lblNewLabel);
frame.setVisible(true);
}
}
能够显示时间,怎么添加代码,使时间能动态显示,即每秒或每分更新一次 展开
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class timetext {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
timetext window = new timetext();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public timetext() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
String s=String.format("%tY-%tm-%td %tH:%tM:%tS",new Date(),new Date(),new Date(),new Date(),new Date(),new Date());
JLabel lblNewLabel = new JLabel(s);
lblNewLabel.setBounds(158, 116, 137, 15);
frame.getContentPane().add(lblNewLabel);
frame.setVisible(true);
}
}
能够显示时间,怎么添加代码,使时间能动态显示,即每秒或每分更新一次 展开
2013-05-24
展开全部
javax.swing.Timer ,很容易的,一个事件,在事件里面显示你的时间就可以了。
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TimerShow extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TimerShow();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private JLabel lblNewLabel;
public TimerShow() {
super("time");
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
lblNewLabel = new JLabel(" 简单时钟 ", JLabel.CENTER);
lblNewLabel.setBounds(0, (300-35-35)/2, 440, 35);
getContentPane().add(lblNewLabel);
setVisible(true);
new Timer(delay, taskPerformer).start();
}
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s=String.format("%tY-%<tm-%<td %<tH:%<tM:%<tS",new Date());
lblNewLabel.setText(s);
}
};
}
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TimerShow extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TimerShow();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private JLabel lblNewLabel;
public TimerShow() {
super("time");
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
lblNewLabel = new JLabel(" 简单时钟 ", JLabel.CENTER);
lblNewLabel.setBounds(0, (300-35-35)/2, 440, 35);
getContentPane().add(lblNewLabel);
setVisible(true);
new Timer(delay, taskPerformer).start();
}
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s=String.format("%tY-%<tm-%<td %<tH:%<tM:%<tS",new Date());
lblNewLabel.setText(s);
}
};
}
展开全部
利用死循环和线程,让线程在循环中每sleep1秒,重新获取下系统时间就是动态显示时间了。
while(true){
Date date=new Date(System.currentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(date);
//每一秒刷新下时间
try {
Thread.sleep(1000);//sleep是以ms为单位
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while(true){
Date date=new Date(System.currentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(date);
//每一秒刷新下时间
try {
Thread.sleep(1000);//sleep是以ms为单位
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
额 加个定时器 在触发事件中 时间间隔1秒 然后+datetime.now
追问
请说详细点,我不太懂
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询