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();
}
}
问题如题 这是自己编的有问题 希望高手解答
急!!!! 帮我改或者重编一个都行 展开
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();
}
}
问题如题 这是自己编的有问题 希望高手解答
急!!!! 帮我改或者重编一个都行 展开
2个回答
展开全部
在你的基础上改了下 你看看 思想就是通过定时任务每一秒钟执行一次取当前时间的动作
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);
}
}
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);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
});
}
}
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/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询