4个回答
展开全部
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* This class is a simple JFrame implementation to explain how to
* display time dynamically on the JSwing-based interface.
* @author Edison
*
*/
public class TimeFrame extends JFrame
{
/*
* Variables
*/
private JPanel timePanel;
private JLabel timeLabel;
private JLabel displayArea;
private String DEFAULT_TIME_FORMAT = "HH:mm:ss";
private String time;
private int ONE_SECOND = 1000;
public TimeFrame()
{
timePanel = new JPanel();
timeLabel = new JLabel("CurrentTime: ");
displayArea = new JLabel();
configTimeArea();
timePanel.add(timeLabel);
timePanel.add(displayArea);
this.add(timePanel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(new Dimension(200,70));
this.setLocationRelativeTo(null);
}
/**
* This method creates a timer task to update the time per second
*/
private void configTimeArea() {
Timer tmr = new Timer();
tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);
}
/**
* Timer task to update the time display area
*
*/
protected class JLabelTimerTask extends TimerTask{
SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
@Override
public void run() {
time = dateFormatter.format(Calendar.getInstance().getTime());
displayArea.setText(time);
}
}
public static void main(String arg[])
{
TimeFrame timeFrame=new TimeFrame();
timeFrame.setVisible(true);
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* This class is a simple JFrame implementation to explain how to
* display time dynamically on the JSwing-based interface.
* @author Edison
*
*/
public class TimeFrame extends JFrame
{
/*
* Variables
*/
private JPanel timePanel;
private JLabel timeLabel;
private JLabel displayArea;
private String DEFAULT_TIME_FORMAT = "HH:mm:ss";
private String time;
private int ONE_SECOND = 1000;
public TimeFrame()
{
timePanel = new JPanel();
timeLabel = new JLabel("CurrentTime: ");
displayArea = new JLabel();
configTimeArea();
timePanel.add(timeLabel);
timePanel.add(displayArea);
this.add(timePanel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(new Dimension(200,70));
this.setLocationRelativeTo(null);
}
/**
* This method creates a timer task to update the time per second
*/
private void configTimeArea() {
Timer tmr = new Timer();
tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);
}
/**
* Timer task to update the time display area
*
*/
protected class JLabelTimerTask extends TimerTask{
SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
@Override
public void run() {
time = dateFormatter.format(Calendar.getInstance().getTime());
displayArea.setText(time);
}
}
public static void main(String arg[])
{
TimeFrame timeFrame=new TimeFrame();
timeFrame.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议使用线程来做。
使用Thread.sleep(1000);即可埋昌停止一秒。
class TimeThread implements Runnable{
private JLabel label;
public TimeThread(JLabel l){
label=l;
}
public void run(){
while(true){
Thread.sleep(1000);
setTime();
}
}
private void setTime(){
//在这里弯睁扒设置label的值即可
}
}
调用的时候,你要在时钟的主界面生成后,使用
new Thread(new TimeThread(label)).start();即可
其早宽中label为你显示时间的JLabel
使用Thread.sleep(1000);即可埋昌停止一秒。
class TimeThread implements Runnable{
private JLabel label;
public TimeThread(JLabel l){
label=l;
}
public void run(){
while(true){
Thread.sleep(1000);
setTime();
}
}
private void setTime(){
//在这里弯睁扒设置label的值即可
}
}
调用的时候,你要在时钟的主界面生成后,使用
new Thread(new TimeThread(label)).start();即可
其早宽中label为你显示时间的JLabel
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加入这段代码到你的页面文件里面:
<拍拆script type="text/javascript">
var TimeOne = document.getElementById('time');
function loadDate(){
var date = new Date();
var msg = "";
msg += ""+date.getFullYear();
msg += "年"+date.getMonth();
msg += "尺派月"+date.getDate();
msg += "袭困枣日"+date.getHours();
msg += "时"+date.getMinutes();
msg += "分"+date.getSeconds()+"秒";
TimeOne.innerHTML = msg;
}
setInterval(loadDate,1000);
</script>
<拍拆script type="text/javascript">
var TimeOne = document.getElementById('time');
function loadDate(){
var date = new Date();
var msg = "";
msg += ""+date.getFullYear();
msg += "年"+date.getMonth();
msg += "尺派月"+date.getDate();
msg += "袭困枣日"+date.getHours();
msg += "时"+date.getMinutes();
msg += "分"+date.getSeconds()+"秒";
TimeOne.innerHTML = msg;
}
setInterval(loadDate,1000);
</script>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Date date = new Date(System.currentTimeMillis());
for ( int i = 0; i < 100; i++ ) {
Thread.sleep(500);
date.setTime(System.currentTimeMillis());
System.out.println("Current time is: " + date.toString());
}
for ( int i = 0; i < 100; i++ ) {
Thread.sleep(500);
date.setTime(System.currentTimeMillis());
System.out.println("Current time is: " + date.toString());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询