简体   繁体   中英

How can i resolve the Exception in thread “main” java.lang.NoSuchMethodError: main

I have this code. Eclipse tells me that the syntax is correct but when I run the program it gives me this error:

Exception in thread "main" java.lang.NoSuchMethodError: main

What's wrong?

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame {

    private static final long serialVersionUID = 1L;
    public void main(String[] args){
        JFrame Main = new JFrame("TEST");
        Main.setSize(600, 600);
        Main.setLocationRelativeTo(null);
        Main.setVisible(true);
        Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Adding JPanel     
        JPanel panel = new JPanel();
        Main.add(panel);
//JPanel settings
        panel.setLayout(null);
        panel.setBackground(Color.GREEN);
//Adding JButton
        JButton button = new JButton("Button 1");
        JButton button2 = new JButton("Button2");
        panel.add(button);
        panel.add(button2);   
//Button action
        button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        JPanel panel = new JPanel();
        JPanel panel2 = new JPanel();
        Main.this.getContentPane().remove(panel);
        Main.this.getContentPane().add(panel2);
        Main.this.getContentPane().validate();      
    }
});

//JButton settings
        button.setBounds(70, 160, 200, 200);
        button2.setBounds(320, 160, 200, 200);  
    }
}

Your main method is not static , and you should make it static . Check this to see why

public static void main(String [] args)

Your main method should be static

  public  static  void main(String[] args){
  ----

}

And see why ?? Why is the Java main method static?

该类需要一个具有以下特征的方法:

public static void main(String[])

You have to make your main method static:

public static void main(String[] args) {

}
public static void main(String[] args)

instead of

public void main(String[] args)

public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details.

static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.

void means that the method has no return value. If the method returned an int you would write int instead of void.

The combination of all three of these is most commonly seen on the main method which most tutorials will include.

您的main方法必须是静态的(即,类绑定而不是实例绑定),因为在启动应用程序时,没有可以调用您的类的main方法的类实例。

该类需要一个签名如下的方法:

public static void main(String[])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM