【问题标题】:Eclipse - Error: Main method not found in class projectOne, please define the main method as: public static void main(String[] args)Eclipse - 错误:在类 projectOne 中找不到主方法,请将主方法定义为:public static void main(String[] args)
【发布时间】:2015-05-20 18:54:27
【问题描述】:

Eclipse

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

您好,我的程序需要帮助。问题是,它总是说请将主方法定义为public static void main(String[] args)。我怎么做?我的包、项目和类都没有问题。

代码如下:

import java.awt.*; 
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class projectOne extends JPanel
{   

    private static final long serialVersionUID = 1L;

    GameEvents gameEvents = new GameEvents();
    Timer gameTimer = new Timer(1, gameEvents);
    int i = 0; 
    int horizontalposition = 500;
    int verticalposition = 500;
    BufferedImage Picture;
    //Don't forget to declare your variables!

    projectOne()
    {
        gameTimer.start();
        this.addKeyListener(gameEvents);

        try 
        {
            Picture = ImageIO.read(getClass().getResource("Homestuck.gif"));
            //The format for this is Picture = ImageIO.read(getClass().getResource("NameOfFile.typeoffile"));
        }
        catch (IOException e)
        {
            System.out.println("Pictures failed to load");
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        g.setColor(Color.blue);
        g.fillRect(0,0,this.getWidth(), this.getHeight());
        g.setColor(Color.red);
        ///g.drawImage(Picture, horizontalposition, verticalposition, 100, 150, null);
        g.drawImage(Picture, 50, 100, 500, 600, null);

        //Here's the format you must follow when drawing simple Java Graphics objects
        //g.fillOval(horizontal location, vertical location, width, height)
    }

    public class GameEvents implements ActionListener, KeyListener
    {

        @Override
        public void actionPerformed(ActionEvent arg0) 
        {
            repaint();
        }

        @Override
        public void keyPressed(KeyEvent key) //stuff inside here happens when a key is pressed
        {
            if(key.getKeyChar()=='d')
            {
                horizontalposition=horizontalposition+20;
            }
            if(key.getKeyChar()=='s')
            {
                verticalposition=verticalposition+20;
            }
            if(key.getKeyChar()=='w')
            {
                verticalposition=verticalposition-20;
            }
            if(key.getKeyChar()=='a')
            {
                horizontalposition=horizontalposition-20;
            }
            if(horizontalposition<0)
            {
                horizontalposition=0;
            }
            //            System.out.println(key.getKeyChar());
            //            System.out.println('d');
        }

        @Override
        public void keyReleased(KeyEvent arg0) {

        }

        @Override
        public void keyTyped(KeyEvent arg0) {

        }

    }

    public static void main(String args)
    {
        JFrame f = new JFrame("Java Graphics Example Project");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        projectOne p = new projectOne();
        f.setSize(1500,700);
        f.add(p);
        f.setVisible(true);
        p.requestFocusInWindow();

    }
}

【问题讨论】:

    标签: java eclipse main


    【解决方案1】:

    main 方法的签名是错误的,您可以将其更改为

    public static void main(String args[])
    

    public static void main(String... args)
    

     public static void main(String [] args)
    

    【讨论】:

      【解决方案2】:

      改变你的

      public static void main(String args)
      

      public static void main(String[] args)
      

      【讨论】:

        【解决方案3】:

        main 方法应该有字符串数组参数。像这样纠正它。

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

        【讨论】:

          猜你喜欢
          • 2013-03-01
          • 2014-01-13
          • 2017-03-04
          • 2014-05-13
          • 1970-01-01
          • 2015-11-16
          • 1970-01-01
          • 2017-03-01
          相关资源
          最近更新 更多