[求助]怎样设置动态时间和静态时间
怎样往applet小程序中加载秒在走动的时间和静止的时间import java.awt.*;
import java.applet.*;
import java.util.*;
public class Shijianriqi extends Applet
{
public void paint(Graphics g)
{
Date riqi=new Date();
GregorianCalendar rili=new GregorianCalendar();
rili.setTime(riqi);
String shijian=rili.get(Calendar.HOUR)+":"+rili.get(Calendar.MINUTE)+":"+rili.get(Calendar.SECOND);
String sriqi=rili.get(Calendar.MONTH)+"/"+rili.get(Calendar.DATE)+"/"+rili.get(Calendar.YEAR);
showStatus(" "+"当前时间"+shijian+" "+"当前日期"+sriqi);
}
}
时间为什么是静止的?
----------------解决方案--------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class Clock extends JPanel implements Runnable{
private JLabel jl;
private DateFormat df;
public Clock(){
jl=new JLabel();
jl.setHorizontalAlignment(JLabel.CENTER);
df=DateFormat.getDateTimeInstance();
new Thread(this).start();
this.setLayout(new BorderLayout());
this.add(jl,BorderLayout.SOUTH);
}
public void run(){
while(true){
try{
Thread.sleep(1000);
}
catch(InterruptedException ie){
ie.printStackTrace();
}
jl.setText(df.format(new Date()));
repaint();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Calendar cal=Calendar.getInstance();
//得到当前的时间信息
int hour=cal.get(Calendar.HOUR);
int minute=cal.get(Calendar.MINUTE);
int second=cal.get(Calendar.SECOND);
//得到当前的面板的大小信息
int width=this.getWidth();
int height=this.getHeight();
//钟的那个圆盘取两个之中小的那个
int small=width<height?width:height;
int diameter=(int)(small*0.8);
int radius=diameter/2;
//确定中心点
Point center=new Point(width/2,height/2);
//确定时针,分钟,秒针的长度
int secondLength=(int)(radius*0.8);
int minuteLength=(int)(secondLength*0.8);
int hourLength=(int)(minuteLength*0.8);
//确定时针,分针,秒针的另一端坐标
int secondX=center.x+(int)(secondLength*Math.sin(second*2*Math.PI/60.0));
int secondY=center.y-(int)(secondLength*Math.cos(second*2*Math.PI/60.0));
int minuteX=center.x+(int)(minuteLength*Math.sin(minute*2*Math.PI/60.0));
int minuteY=center.y-(int)(minuteLength*Math.cos(minute*2*Math.PI/60.0));
int hourX=center.x+(int)(hourLength*Math.sin((minute/60.0+hour)*Math.PI/6.0));
int hourY=center.y-(int)(hourLength*Math.cos((minute/60.0+hour)*Math.PI/6.0));
Graphics2D g2d=(Graphics2D)g;
//画表盘和刻度
g.drawOval(center.x-radius,center.y-radius,diameter,diameter);
for(int i=0;i<60;i++){
int x2=center.x+(int)(radius*Math.sin(i*2*Math.PI/60.0));
int y2=center.y-(int)(radius*Math.cos(i*2*Math.PI/60.0));
if(i%5==0){
int x1=center.x+(int)((secondLength+1)*Math.sin(i*2*Math.PI/60.0));
int y1=center.y-(int)((secondLength+1)*Math.cos(i*2*Math.PI/60));
g2d.setStroke(new BasicStroke(2.5f));
g2d.drawLine(x1,y1,x2,y2);
}
else{
int x1=center.x+(int)((secondLength+10)*Math.sin(i*2*Math.PI/60.0));
int y1=center.y-(int)((secondLength+10)*Math.cos(i*2*Math.PI/60));
g2d.setStroke(new BasicStroke(0.8f));
g2d.drawLine(x1,y1,x2,y2);
}
}
//画时针,分针,秒针
g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(3.0f));
g2d.drawLine(center.x,center.y,hourX,hourY);
g2d.setColor(Color.BLUE);
g2d.setStroke(new BasicStroke(1.5f));
g2d.drawLine(center.x,center.y,minuteX,minuteY);
g2d.setColor(Color.MAGENTA);
g2d.setStroke(new BasicStroke(1.0f));
g2d.drawLine(center.x,center.y,secondX,secondY);
}
public static void main(String args[]){
JFrame jf=new JFrame("时钟");
jf.getContentPane().add(new Clock(),BorderLayout.CENTER);
jf.setBounds(300,300,300,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
----------------解决方案--------------------------------------------------------
没看懂
我刚开始学 java
怎样把我写的修改成时间是走动的
----------------解决方案--------------------------------------------------------
在java当中,但凡要实现动的,最好的方法就是用线程来实现
上面的例子就是用线程实现的
你的时间为什么不走动,是因为它只取得了一次当前系统的时候,但是后来却没有变化了
你要使得程序每一秒都去读一下系统的时间,再重画就可以实现时间走动的功能了
----------------解决方案--------------------------------------------------------
大哥帮我改一下啊!
帮我把代码写出来啊 你说的我看的不是很明白
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.util.*;
public class Shijianriqi extends Applet implements Runnable
{
public void init(){
new Thread(this).start();
}
public void paint(Graphics g)
{
Date riqi=new Date();
GregorianCalendar rili=new GregorianCalendar();
rili.setTime(riqi);
String shijian=rili.get(Calendar.HOUR)+":"+rili.get(Calendar.MINUTE)+":"+rili.get(Calendar.SECOND);
String sriqi=rili.get(Calendar.MONTH)+"/"+rili.get(Calendar.DATE)+"/"+rili.get(Calendar.YEAR);
showStatus(" "+"当前时间"+shijian+" "+"当前日期"+sriqi);
}
public void run(){
try{
Thread.sleep(1000);
}
catch(InterruptedException ie){
System.out.println(ie);
}
repaint();
}
}
我只是在你的代码基础上加了线程处理,至于你原来的代码是不是对的,我没有检查
----------------解决方案--------------------------------------------------------
我以前那是对的
你写的这个只走一秒就停了呀!!!!
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.util.*;
public class Shijianriqi extends Applet implements Runnable
{
public void init(){
new Thread(this).start();
}
public void paint(Graphics g)
{
Date riqi=new Date();
GregorianCalendar rili=new GregorianCalendar();
rili.setTime(riqi);
String shijian=rili.get(Calendar.HOUR)+":"+rili.get(Calendar.MINUTE)+":"+rili.get(Calendar.SECOND);
String sriqi=rili.get(Calendar.MONTH)+"/"+rili.get(Calendar.DATE)+"/"+rili.get(Calendar.YEAR);
showStatus(" "+"当前时间"+shijian+" "+"当前日期"+sriqi);
}
public void run(){
while(true){
try{
Thread.sleep(1000);
}
catch(InterruptedException ie){
System.out.println(ie);
}
repaint();
}
}
}
现在可以了,是我忘了加一个无限循环
----------------解决方案--------------------------------------------------------