• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating own grpahic widget  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, im about to go through O'Reilly's Java Head First book. At page 364, there's a creation of an own widget, that i can't get to run. Compilation is ok, but when executing .class then i get the following Exception:

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

Here's the code, can somebody help me out? Thanks in advance, Patrick

import java.awt.*;
import javax.swing.*;

public class Animation {
public void main (String[] args) {
JFrame frame = new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);MeinZeichenPanel mzp = new MeinZeichenPanel();
frame.getContentPane().add(mzp);
frame.setSize(300,300);
frame.setVisible(true);
}


class MeinZeichenPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
}
}
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, the main method needs to be static. If you add that to the method definition, it should run fine.
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback, task done.
The compiler tells me then:
Animation.java:8: non-static variable this cannot be referenced from a static context
MeinZeichenPanel mzp = new MeinZeichenPanel();
1 error
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MeinZeichenPanel is a non-static inner class. You can't instantiate it without having an instance of Animation.

You can either make it static, or extract it out of Animation and make it a top level class.
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
i've instantiated the widget from a new non static method and it works.. Great, thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!