代码如下:
运行后一直提示错误: 在类 Clock 中找不到主方法, 请将主方法定义为:
public static void main(String[] args)
初学小白求解答
这是完整代码:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class Clock extends JApplet implements Runnable
{
Thread clockThread1;
Thread clockThread2;
Thread clockThread3;
boolean f1,f2,f3;
SimpleDateFormat formatter;
Date currentDate1,currentDate2,currentDate3;
String lastdate1,lastdate2,lastdate3;
public void init()
{
super.setBackground(Color.LIGHT_GRAY);
formatter=new SimpleDateFormat("hh:mm:ss",Locale.getDefault());
currentDate1=new Date();
lastdate3=lastdate2=lastdate1=formatter.format(currentDate1);
clockThread1=new Thread(this,"Clock-thread1");
clockThread2=new Thread(this,"Clock-thread2");
clockThread3=new Thread(this,"Clock-thread3");
clockThread1.start();
clockThread2.start();
clockThread3.start();
}
public void run()
{
boolean flag=true;
while(flag)
{
try
{
if (Thread.currentThread() == clockThread1){
Thread.sleep(1000);
f1 = true;
}
if (Thread.currentThread() == clockThread2){
Thread.sleep(2000);
f2 = true;
}
if (Thread.currentThread() == clockThread3){
Thread.sleep(3000);
f3 = true;
}
super.repaint();
}
catch(InterruptedException e)
{
flag=false;
}
}
}
public void paint (Graphics g)
{
g.setColor(this.getBackground());
if (f1){
currentDate1=new Date();
lastdate1=formatter.format(currentDate1);
g.fillRect(5, 0, 50, 10);
f1 = false;
}
if (f2){
currentDate2=new Date();
lastdate2=formatter.format(currentDate2);
g.fillRect(125, 0, 50, 10);
f2 = false;
}
if (f3){
currentDate3=new Date();
lastdate3=formatter.format(currentDate3);
g.fillRect(225, 0, 50, 10);
f3 =false;
}
g.setColor(getForeground());
g.drawString(lastdate1,5,10);
g.drawString(lastdate2,125,10);
g.drawString(lastdate3,225,10);
}
}
public static void main(String[] args)
这个方法你没有啊。
你可以再定义一个类Program.java
public class Program
{
public static void main(String[] args)
{
Clock c = new Clock();
c.init();
c.run();
}
}
没看到你全部的代码,所以也不确定。1.首先,执行类Clock程序,要有入口程序,也就是main函数定义,Clock类中是否定义了public static void main(String[] args)这样的函数 2.如果定义了main函数,要确定类名与java文件名是否一致,即java文件名应为Clock.java
注意java程序是定义的类,但也必须要有入口程序,即main函数,否则无法执行,主函数要定义在主类(就是和文件名相同的那个类)中。
楼主你的程序在我的eclipse java Mars上运行是没有问题的,另外,java applet中init是有相当于main的功能的,它是applet中进行初始化的入口。
要写一个方法叫
public static void main(String[] args){
//括号里写你的方法体
}
写一个方法叫
public static void main(String[] args){
}