当前位置: 首页 > 知识库问答 >
问题:

错误:类中找不到Main方法,请将Main方法定义为:public static void Main(String[]args)

沈枫涟
2023-03-14
Main method not found in class p16, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application
//button events
//on pressing a button color changes
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
 <applet code = p16 width= 140 height=140>
 </applet>
 */
public class p16 extends Applet implements ActionListener {

    String msg = "";
    Button first, second;

    public void init() {
        first = new Button("yes");
        second = new Button("no");
        add(first);
        add(second);
        first.addActionListener(this);
        second.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        String str = ae.getActionCommand();
        if (str.equals("yes")) {
            first.setBackground(Color.red);
            second.setBackground(Color.white);
            msg = "pressed yes";
        } else {
            second.setBackground(Color.blue);
            first.setBackground(Color.white);
            msg = "pressed no";
        }
        repaint();
    }

    public void paint(Graphics g) {
        g.drawString(msg, 13, 12);
    }
}

共有1个答案

赵灼光
2023-03-14

在Netbeans IDE上:您可以在application文件夹中右键单击类并选择run file,但如果您在工具栏中按run按钮,则会出现相同的错误

在Eclipce上:它可以从两个方向运行

  • 从工具栏运行按钮
  • 右键单击application文件夹中的类,并作为java applet运行
 类似资料: