java 实时显示系统时间 急!!!

importjavax.swing.*;importjava.awt.*;importjava.util.TimerTask;importjava.util.Timer;... import javax.swing.*;
import java.awt.*;
import java.util.TimerTask;
import java.util.Timer;
import java.util.*;
import java.text.*;
import java.util.Date;

public class Time extends JFrame {
JLabel systemTime;
public Time(){

setTitle("sbgaowenchao");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

systemTime = new JLabel("系统时间:");
Container cp = getContentPane();
systemTime.setBounds(20,20,200,200);
cp.setLayout(null);
cp.add(systemTime);

setSize(800,800);

}
public static void main(String[] args){

TimerTask task = new TimerTask() {
JButton logoutbutton;

public void run() {

Calendar st = Calendar.getInstance();
String sdate;
Date ddate;
ddate=st.getTime();
sdate=(new SimpleDateFormat("yyyy-MM-dd")).format(ddate);
systemTime.setText(sdate);

}
};

Time s = new Time();
s.show();

}
}

问题如题 这是自己编的有问题 希望高手解答
急!!!! 帮我改或者重编一个都行
展开
 我来答
pingia
推荐于2016-03-25 · TA获得超过569个赞
知道小有建树答主
回答量:697
采纳率:100%
帮助的人:768万
展开全部
在你的基础上改了下 你看看 思想就是通过定时任务每一秒钟执行一次取当前时间的动作
import javax.swing.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import java.awt.*;
import java.util.Timer;
import java.util.*;
import java.text.*;
import java.util.Date;

public class Time extends JFrame {
JLabel systemTime;
public Time(){

setTitle("sbgaowenchao");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

systemTime = new JLabel("系统时间:");
Container cp = getContentPane();
systemTime.setBounds(20,20,200,200);
cp.setLayout(null);
cp.add(systemTime);

setSize(800,800);

setVisible(true); //add by me

}

JButton logoutbutton;
public static void main(String[] args){

TimerTask task = new TimerTask() {

Time time = new Time();

public void run() {

//comment by me
/* Calendar st = Calendar.getInstance();
String sdate;
Date ddate;
ddate=st.getTime();*/

//add by me below:

String sdate;
sdate=(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date());
time. systemTime.setText(sdate);

}
};

Timer t = new Timer();

t.scheduleAtFixedRate(task, new Date(), 1000);

}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zhaotao_king
2010-06-02 · TA获得超过2455个赞
知道大有可为答主
回答量:863
采纳率:0%
帮助的人:1148万
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.awt.geom.*;
import java.util.*;
class Clock extends Canvas implements ActionListener
{
Date date;
Timer secondtime;
int hour,munite,second;
Line2D secondLine,muniteLine,hourLine;
int a,b,c;
double pointsx[]=new double[60];
double pointsy[]=new double[60];
double pointmx[]=new double[60];
double pointmy[]=new double[60];
double pointhx[]=new double[60];
double pointhy[]=new double[60];
public Clock()
{
secondtime=new Timer(1000,this);
pointsx[0]=0;
pointsy[0]=-100;
pointmx[0]=0;
pointmy[0]=-90;
pointhx[0]=0;
pointhy[0]=-70;
double angle=6*Math.PI/180;
for(int i=0;i<59;i++)
{
pointsx[i+1]=pointsx[i]*Math.cos(angle)-Math.sin(angle)*pointsy[i];
pointsy[i+1]=pointsy[i]*Math.cos(angle)+Math.sin(angle)*pointsx[i];
pointmx[i+1]=pointmx[i]*Math.cos(angle)-Math.sin(angle)*pointmy[i];
pointmy[i+1]=pointmy[i]*Math.cos(angle)+Math.sin(angle)*pointmx[i];
pointhx[i+1]=pointhx[i]*Math.cos(angle)-Math.sin(angle)*pointhy[i];
pointhy[i+1]=pointhy[i]*Math.cos(angle)+Math.sin(angle)*pointhx[i];
}
for(int i=0;i<60;i++)
{
pointsx[i]=pointsx[i]+120;
pointsy[i]=pointsy[i]+120;
pointmx[i]=pointmx[i]+120;
pointmy[i]=pointmy[i]+120;
pointhx[i]=pointhx[i]+120;
pointhy[i]=pointhy[i]+120;
}
secondLine=new Line2D.Double(0,0,0,0);
muniteLine=new Line2D.Double(0,0,0,0);
hourLine=new Line2D.Double(0,0,0,0);
secondtime.start();

}
public void paint(Graphics g)
{
for(int i=0;i<60;i++)
{
int m=(int)pointsx[i];
int n=(int)pointsy[i];
if(i%5==0)
{
g.setColor(Color.red);
g.fillOval(m-4,n-4,8,8);
}
else
{
g.setColor(Color.cyan);
g.fillOval(m-2,n-2,4,4);
}
}
g.fillOval(115,115,10,10);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.red);
g2d.draw(secondLine);
BasicStroke bs=new BasicStroke(3f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
g2d.setStroke(bs);
g2d.setColor(Color.blue);
g2d.draw(muniteLine);
bs=new BasicStroke(6f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
g2d.setStroke(bs);
g2d.setColor(Color.green);
g2d.draw(hourLine);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==secondtime)
{
date=new Date();
String s=date.toString();
hour=Integer.parseInt(s.substring(11,13));
munite=Integer.parseInt(s.substring(14,16));
second=Integer.parseInt(s.substring(17,19));
int h=hour%12;
a=second;
b=munite;
c=h*5+munite/12;
secondLine.setLine(120,120,(int)pointsx[a],(int)pointsy[a]);
muniteLine.setLine(120,120,(int)pointmx[b],(int)pointmy[b]);
hourLine.setLine(120,120,(int)pointhx[c],(int)pointhy[c]);
repaint();
}
}
}
public class zhongbiao
{
public static void main(String args[])
{
Frame frm=new Frame();
frm.add(new Clock(),BorderLayout.CENTER);
frm.setSize(500,500);
frm.validate();
frm.setVisible(true);
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}

参考资料: http://jminggood163.blog.163.com/blog/static/97428491200810183033302/

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式