java一个时钟 Applet 小程序,要求显示当前时间并逐秒进行更新。但我编的程序无法更新,求大神指教 10
packageexp3;importjava.awt.*;importjava.util.*;importjavax.swing.*;importjava.text.*;...
package exp3;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.text.*;
import java.math.*;
/** -----------一个时钟 Applet 小程序,它显示当前时间并逐秒进行更新-----------*/
public class ShowTimeSample extends ThreadAppletSample {
/**
*
*/
private static final long serialVersionUID = 1L;
Date timenow;
public void run() {
Thread current = Thread.currentThread();
while (thisThread == current) {
try {
Thread.currentThread();
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
repaint();
}
}
int x1,y1,x2,y2,x3,y3,l1=45,l2=30,l3=20;
int a,b,c;
public void paint(Graphics g){
timenow = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy 年 MM 月 dd 日 hh:mm:ss a");
String s_time_msg = sdf.format(timenow);
g.drawString(s_time_msg,10,10);
g.drawOval(50, 50, 100, 100);
a=timenow.getSeconds()*6;
b=timenow.getMinutes()*6;
c=timenow.getHours()*30;
x1=(int) (100+l1*Math.sin(3.14*2*a/360));
y1=(int) (100-l1*Math.cos(3.14*2*a/360));
x2=(int) (100+l2*Math.sin(3.14*2*b/360));
y2=(int) (100-l2*Math.cos(3.14*2*b/360));
x3=(int) (100+l3*Math.sin(3.14*2*c/360));
y3=(int) (100-l3*Math.cos(3.14*2*c/360));
System.out.println(x1+" "+y1+" "+a);
//画秒针
g.drawLine(100, 100, x1, y1);
//画分针
g.setColor(Color.BLUE);
g.drawLine(100, 100, x2, y2);
//画时针
g.setColor(Color.RED);
g.drawLine(100, 100, x3, x3);
}
public void update(Graphics g)
{
paint(g);
}
}
class ThreadAppletSample extends JApplet implements Runnable {
private static final long serialVersionUID = 1L;
Thread thisThread;
public void start() {
if ( thisThread == null ) {
thisThread = new Thread(this);
thisThread.start();
}
}
public void stop() {
if ( thisThread != null ) {
thisThread = null;
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
} 展开
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.text.*;
import java.math.*;
/** -----------一个时钟 Applet 小程序,它显示当前时间并逐秒进行更新-----------*/
public class ShowTimeSample extends ThreadAppletSample {
/**
*
*/
private static final long serialVersionUID = 1L;
Date timenow;
public void run() {
Thread current = Thread.currentThread();
while (thisThread == current) {
try {
Thread.currentThread();
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
repaint();
}
}
int x1,y1,x2,y2,x3,y3,l1=45,l2=30,l3=20;
int a,b,c;
public void paint(Graphics g){
timenow = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy 年 MM 月 dd 日 hh:mm:ss a");
String s_time_msg = sdf.format(timenow);
g.drawString(s_time_msg,10,10);
g.drawOval(50, 50, 100, 100);
a=timenow.getSeconds()*6;
b=timenow.getMinutes()*6;
c=timenow.getHours()*30;
x1=(int) (100+l1*Math.sin(3.14*2*a/360));
y1=(int) (100-l1*Math.cos(3.14*2*a/360));
x2=(int) (100+l2*Math.sin(3.14*2*b/360));
y2=(int) (100-l2*Math.cos(3.14*2*b/360));
x3=(int) (100+l3*Math.sin(3.14*2*c/360));
y3=(int) (100-l3*Math.cos(3.14*2*c/360));
System.out.println(x1+" "+y1+" "+a);
//画秒针
g.drawLine(100, 100, x1, y1);
//画分针
g.setColor(Color.BLUE);
g.drawLine(100, 100, x2, y2);
//画时针
g.setColor(Color.RED);
g.drawLine(100, 100, x3, x3);
}
public void update(Graphics g)
{
paint(g);
}
}
class ThreadAppletSample extends JApplet implements Runnable {
private static final long serialVersionUID = 1L;
Thread thisThread;
public void start() {
if ( thisThread == null ) {
thisThread = new Thread(this);
thisThread.start();
}
}
public void stop() {
if ( thisThread != null ) {
thisThread = null;
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
} 展开
1个回答
2016-06-10
展开全部
这是线程,不是自动启动,,,,调用的地方,有没有启动这个线程呢。。。。。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询