Why do I get NoSuchMethodError: main in my Applet? - java

I need help in my Java applet.
import java.awt.*;
import java.applet.*;
public class Screen extends Applet{
public void init(){
setSize(300,300);
setBackground(Color.BLACK);
}
}
This error keeps popping up when I run it.
Exception in thread "main" java.lang.NoSuchMethodError: main

You are trying to run the applet as an application using
java Screen
That won't work, because an applet is not an application, and doesn't normally have a main() method, which is what java Screen will try to run.
There are a few solutions:
Run it using the appletviewer tool, which comes with the JDK.
Run it in the browser by embedding it in an HTML page.
Make it an "app-applet".
It's been almost ten years since I've actually written an applet so I don't remember the details around #3, but IIRC it involves adding a main() method to the applet and having that main() method launch the applet. I'm sure you can find it on Google.

Create a new swing form. Since your class extends the default 'applet' you can simply add it to a swing application like this Screen x = new Screen(); then add it to your JFrame YourJframe.add(x); Hope this helped! (I could use an upvote lol :))

Related

Applet not working properly on my device only, program is error free

/*
<applet code =game height = 400 width =400 >
</applet>
*/
import java.awt.* ;
import java.awt.event.* ;
import java.applet.* ;
public class game extends Applet {
public void paint(Graphics g){
System.out.println("done");
}
}
This is my code. I haven't used the repaint function in program but still the output is:
done
done
That is, 'done' is printed twice.
First of all, the paintComponent(Graphics g){...} function is called lots and lots of times (when you create the JPanel, when you resize it...) and you don't really know when it's being called. It's not a good practice to write code which isn't intended to draw stuff in that function (unless you're debugging that part of the code). It could cause your app to be very laggy.
Instead, try to write that piece of code in other method and call it at the end of the JPanel constructor(or introduce it directly), that way you'll know when the constructor has ended building up the JPanel. (If that's your purpose).
As a sidenote: check out this swing tutorial, it's going to help you clearly understand how swing works.
Select as answer if it'd helped you. :D

Getting Java Applet to play as standalone Application

I've spent the last few days putting together a game that runs as an Applet, but I would also like to have it available as an Application.
I've tried placing the applet in a frame, but I only get a black screen (and the occasional menu screen) when doing so. The applet still runs and the sound still plays, but there is nothing else.
I would like to not have to break down the code and rewrite it for a standalone application, as it's time consuming, but if there's no other way, then I'll do what I have to do. The program is a little complex in that the "main" class (containing all the media information, game related events, etc.) is an extension of an Applet class (containing the KeyListener, init() event, run() event, etc.) so maybe I am just trying to place the wrong thing in a frame?
Here is a link to the src folder for the game. I have tried putting both the AWPASG class and the Game class in a frame and seen the same results for both.
Any help would be greatly appreciated.
Source Code/Media
http://www.mediafire.com/?4eslqqr4aoh33j1
That's because your class design is not very good. You've bunched everything together in 2 classses, isntead of separating at least the UI Widgets (panels, etc, etc) from the UI Container.
Also you're using the old style AWT Applet and Panel. You should use the new Swing JApplet and JPanel (and all opthers JXxx components)
In short, try isolating your GUI building (painting) into a class that extends JPanel (or contains a JPanel). Then you can have 2 separate startup classes, one that puts your game JPanel inside a JApplet and one that puts it into a JFrame (and thus gives you the possibility to start it as a Desktop application).
Also you might want to separate your GAME init logic from the Applet init lifecycle, your game should be able to initialize without caring about the underlieing GUI technology.

Making a hello world app in a Form in Intellij

I'm trying to make a hello world form in Intellij. I've created the form, but the question now is what code to make in main() to make the form run and show up?
PS: all the tutorials around seem to only focus on "how to do forms on intellij" not in "how to actually make it run, then".
Thanks
Go to the class with the same name as the form.
Press the keyboard shortcut for "Generate". It's Ctrl+N on Mac OS X, Alt+Ins on Windows. Alternatively, from the menu, select menu Code → Generate.
Select "Form main()".
Now the main method is written and inserted for you. It will look something like this:
public static void main(String[] args) {
JFrame frame = new JFrame("MyForm");
frame.setContentPane(new MyForm().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
I just did my first Intellij Swing App.
Steve McLeod has the right instructions, however, when I tried to generate the main method using Alt+Insert => Generate main, I received an error message about one of my panels not being bound. So I clicked on the gui designer page (.form), selected my top panel, and gave it a name.
Everything else was named for me, but for some reason, the panel name was blank. Once I did that, I was able to switch over to the form .java class, press "Alt+Insert" and generate a constructor (not required, but needed).
From there, I followed Steve's advice to generate a main method. One thing that threw me off was the expectation that my Intellij generated .java class would extend or implement something swing related - it didn't. Swing only shows up in the Intellij generated main method (besides the private variables).
Check this tut while it is realy step-by-step:
JetBrains JavaFX HelloWorld

Is it not possible to call another JInternalFrame or JPanel [saved as NewJIF.java]from the main JFrame[saved as MainJFrame.java] in Netbeans?

I have created a MainJFrame.java in the package myproject using New project->java->java aplication and NewJIF.java in the same package.
When a JButton clicked in MainJFrame.java i want JInterFrame to open using
new NewJIF().setVisible(true); inside actionPerformed() method.
But this is not working ...and caught some people saying this is impossible in netbeans to call another java class using swing framework...
I'm sure it is possible. You can add any code you want to an ActionListener. So there is no reason you can't do this.
I suggest you read the section from the Swing tutorial on How to Use Internal Frames. Understand how the code in the ActionListener works there and then incorporate the concepts and code in to your application.

error in running a simple swing program

i just started learning swings. And thought of trying out a simple program, but i can't run it.
import java.awt.*;
import javax.swing.*;
class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
}
I am getting the following error:
Exception in thread "main" java.lang.NoSuchMethodError: main
My Question: Do we need to have a main method in every class we want to run? Can't JVM run any class which doesnt have a main method. Here i don't require a main class i think, cuz this paintComponent method should be called by the system, right?
P.S: I am using plain vanilla cmd to compile and run.
java is rather simple, when you give it a class file it will load it and try execute a program. Java programs are defined to start in the "public static void main(String... args)" method. So a class file missing this function has no valid entry point for a program.
To make java call your paintComponent() method you have to add an instance of your class to a toplevel container like JFrame or for web applications an JApplet. (Applets don't use a main method as they are executed as part of a web page and not a standalone app.)
Example:
import javax.swing.*
public class MyDrawPanel{
public static void main(String... args)
{
JFrame frame = new JFrame(200,200);//A window with 200x200 pixel
MyDrawPanel mdp = new MyDrawPanel();//Panel instance
frame.add(mdp);//Add the panel to the window
frame.setVisible(true);//Display all
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit when the window is closed
}
}
As vodkhang said, you need a "main" method. Make sure it looks just like this:
public static void main(String[] args)
{
// your code here.
// this example will use your panel:
// create a new MyDrawPanel
MyDrawPanel panel = new MyDrawPanel();
// create a frame to put it in
JFrame f = new JFrame("Test Frame");
f.getContentPane().add(panel);
// make sure closing the frame ends this application
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// show the frame
f.setSize(100,100);
f.setVisible(true);
}
Yes, every Java program that you want to run needs a main method with exactly this signature:
public static void main(String[] args)
You can run java code from within other systems (like web servers and etc without a "main") but to simply run it, the main is the entry point. Put it where-ever you want to start a program running.
When running, make sure you get the class name right to help it find your main method. In your case, if you are running java by hand in the same directory as your MyDrawPanel.class file you would do this:
java -cp . MyDrawPanel
If you are running from inside a developer tool, then it will provide a way to run the class you are looking at.
Do we need to have a main method in
every class we want to run?
You need a class with a main method to start a JVM.
Can't JVM run any class which doesnt have a main method.
Not initially.
Here i don't require a main class i think, cuz this paintComponent method should be called by the system, right?
Wrong. It's true that the paintComponent() method will eventually be called by "the system", specifically the Swing Event Dispatch Thread. But that needs to be started first, which happens implicitly when you create a window and make it visible. And that in turn can only happen in a main method.
You need a main method in the class that you want to run the program for. It is mandatory. How can the JVM know which method they should call to start if you have multiple methods. They can guess but most of the time, the guess may go wrong. So, provide a simple main method will help

Categories