帮忙看一下java编程详解中的时钟代码??
错误1:14:Clock是公共的,应在文件中声明
2:110 无法访问clock
3错误的类文件.\Clock.java
文件不包含类clock
请删除该文件或确保该文件位于正确的类路径子目录中/**
* 时钟
*
* Sample Applet application
*
* @author
* @version 1.00 08/04/21
*/
import java.awt.*;
import java.applet.Applet;
import java.util.*;public class CLock extends Applet implements Runnable { Thread thisThread;
Color faceColor,borderColor,minuteColor,hourColor,secondColor;
public void init() {
//读取颜色
faceColor=readColor(getParameter("faceCol"));
borderColor=readColor(getParameter("bordercol"));
minuteColor=readColor(getParameter("minutecol"));
hourColor=readColor(getParameter("hourcol"));
secondColor=readColor(getParameter("secondcol"));
}
//this method creates a color based on a string
//this string is assumed to be " r g b"
public Color readColor(String aColor){
if(aColor == null){
return Color.black;
}
int r;
int g;
int b;
//break the string apart into each number
StringTokenizer st=new StringTokenizer(aColor,",");
try{
r=Integer.valueOf(st.nextToken()).intValue();
g=Integer.valueOf(st.nextToken()).intValue();
b=Integer.valueOf(st.nextToken()).intValue();
return new Color(r,g,b);
}
catch(Exception e){
System.out.println("An exception occured :"+e);
return Color.black;
}
}
public void start(){
thisThread=new Thread(this);
thisThread.start();
}
public void run(){
while(true){
repaint();
try{
thisThread.sleep(1000);
}catch(Exception e){}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g) {
//fillclock face
g.setColor(faceColor);
g.fillOval(0,0,100,100);
g.setColor(borderColor);
g.drawOval(0,0,100,100);
//get the current time
Calendar d=Calendar.getInstance();
//draw the minute hand
g.setColor(minuteColor);
double angle=(((double)(90-d.get(Calendar.MINUTE)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50),50+(int)(Math.cos(angle)*50));
//draw the hour hand
g.setColor(hourColor);
angle=((((double)18-d.get(Calendar.HOUR_OF_DAY)+(double)d.get(Calendar.MINUTE)/60))/12)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*40),50+(int)(Math.cos(angle)*40));
g.drawString("Welcome to Java!!", 50, 60 ); //draw the secon hand
g.setColor(secondColor);
angle=(((double)(90-d.get(Calendar.SECOND)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50),50+(int)(Math.cos(angle)*50));
} static boolean inApplet=true;
public static void main(String args[]){
/*set a boolean flag to show if you are in an applet or not*/
inApplet=false;
/*Create a Frame to place our application in*/
/*YOu can change the string value to show your desired label*/
/*for the frame*/
Frame myFrame=new Frame("Clock as Application");
/*Create a clock instance*/
Clock myApp=new Clock(); /*Add the current application to the Frame*/
/*Tag:*/ myFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
}); myFrame.add("center", myApp);
/*Resize the frame to the desired size ,and make it visible*/
myFrame.setSize(100,130);
myFrame.show();
/*run the methods the browser normally would*/
myApp.init();
myApp.start();
}
错误1:14:Clock是公共的,应在文件中声明
2:110 无法访问clock
3错误的类文件.\Clock.java
文件不包含类clock
请删除该文件或确保该文件位于正确的类路径子目录中/**
* 时钟
*
* Sample Applet application
*
* @author
* @version 1.00 08/04/21
*/
import java.awt.*;
import java.applet.Applet;
import java.util.*;public class CLock extends Applet implements Runnable { Thread thisThread;
Color faceColor,borderColor,minuteColor,hourColor,secondColor;
public void init() {
//读取颜色
faceColor=readColor(getParameter("faceCol"));
borderColor=readColor(getParameter("bordercol"));
minuteColor=readColor(getParameter("minutecol"));
hourColor=readColor(getParameter("hourcol"));
secondColor=readColor(getParameter("secondcol"));
}
//this method creates a color based on a string
//this string is assumed to be " r g b"
public Color readColor(String aColor){
if(aColor == null){
return Color.black;
}
int r;
int g;
int b;
//break the string apart into each number
StringTokenizer st=new StringTokenizer(aColor,",");
try{
r=Integer.valueOf(st.nextToken()).intValue();
g=Integer.valueOf(st.nextToken()).intValue();
b=Integer.valueOf(st.nextToken()).intValue();
return new Color(r,g,b);
}
catch(Exception e){
System.out.println("An exception occured :"+e);
return Color.black;
}
}
public void start(){
thisThread=new Thread(this);
thisThread.start();
}
public void run(){
while(true){
repaint();
try{
thisThread.sleep(1000);
}catch(Exception e){}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g) {
//fillclock face
g.setColor(faceColor);
g.fillOval(0,0,100,100);
g.setColor(borderColor);
g.drawOval(0,0,100,100);
//get the current time
Calendar d=Calendar.getInstance();
//draw the minute hand
g.setColor(minuteColor);
double angle=(((double)(90-d.get(Calendar.MINUTE)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50),50+(int)(Math.cos(angle)*50));
//draw the hour hand
g.setColor(hourColor);
angle=((((double)18-d.get(Calendar.HOUR_OF_DAY)+(double)d.get(Calendar.MINUTE)/60))/12)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*40),50+(int)(Math.cos(angle)*40));
g.drawString("Welcome to Java!!", 50, 60 ); //draw the secon hand
g.setColor(secondColor);
angle=(((double)(90-d.get(Calendar.SECOND)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50),50+(int)(Math.cos(angle)*50));
} static boolean inApplet=true;
public static void main(String args[]){
/*set a boolean flag to show if you are in an applet or not*/
inApplet=false;
/*Create a Frame to place our application in*/
/*YOu can change the string value to show your desired label*/
/*for the frame*/
Frame myFrame=new Frame("Clock as Application");
/*Create a clock instance*/
Clock myApp=new Clock(); /*Add the current application to the Frame*/
/*Tag:*/ myFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
}); myFrame.add("center", myApp);
/*Resize the frame to the desired size ,and make it visible*/
myFrame.setSize(100,130);
myFrame.show();
/*run the methods the browser normally would*/
myApp.init();
myApp.start();
}
前面加一句
import java.awt.event.*;