NetBeans无法找到主类

时间:2015-01-05 14:54:16

标签: java netbeans applet

我正在尝试使用NetBeans中的applet创建程序,我遇到了问题。每当我尝试运行一个程序时它会说" Project没有主要的类设置"。我能够通过添加来解决这个问题 public static void main (String[] args)但我的计划没有做任何事情。我尝试了一个通用程序,看看它是不是我的编码,但它仍然没有用。我是否需要重置NetBeans或编码错误? 以下是我从Oracle网站获得的代码

    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    import javax.swing.JLabel;

    public class HelloWorld extends JApplet {
    //Called when this applet is loaded into the browser.
    public void init() {
    //Execute a job on the event-dispatching thread; creating this applet's GUI.
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                JLabel lbl = new JLabel("Hello World");
                add(lbl);
            }
        });
    } catch (Exception e) {
        System.err.println("createGUI didn't complete successfully");
    }
    }
    }

这是我的程序的代码:

package robot;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JApplet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.lang.Object;


public class Robot extends JApplet {
private Rectangle rect; 

public void init(){
rect = new Rectangle (0, 0, 20,20);


this.addKeyListener((KeyListener) this);
 } 

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0,0,500,500);
{
this.setSize(355, 355);
}
Graphics2D g1 = (Graphics2D) g;

g1.setColor(Color.black);
g1.fill(rect);
}


public void keyPressed(KeyEvent e)
{ 
if (e.getKeyCode()== KeyEvent.VK_UP) {
rect.setLocation ( rect.x,rect.y=+20 );
repaint();
 }
if (e.getKeyCode()== KeyEvent.VK_DOWN) {
rect.setLocation ( rect.x,rect.y=-20 );
repaint();

 }
if (e.getKeyCode()== KeyEvent.VK_RIGHT) {
rect.setLocation ( rect.x =+20, rect.y );
repaint();
 }
if (e.getKeyCode()== KeyEvent.VK_LEFT) {
rect.setLocation ( rect.x =-20,rect.y);
repaint();
 }

}


}

2 个答案:

答案 0 :(得分:1)

  

如果我右键单击我的代码然后点击"运行项目"我能够运行该程序。但每当我尝试使用运行项目按钮时,它都无法正常工作。

用于运行它的第二种方式对于applet来说并不有效。这是针对应用程序。

答案 1 :(得分:0)

您可以通过左键单击project -> run来设置主类,然后选择主类:  enter image description here