0

I was trying to start a frame GUI (desktop window) without creating it from an Applet object.

I got a compile error.

My question is, the only way to create a desktop gui frame is with a main method, like desktop application?

Or is there a way to create a frame like creating an applet with the init(), start(), paint(), etc… methods?

my code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


class PilotinoGui extends Frame {
    PilotinoGui(String title) {
        super(title);
    }
    public void paint(Graphics g) {
        g.drawString("This is stringggg", 10, 40);
    }

}

Error message:

Error: Main method not found in class
zetariemann.com.pilotino.PilotinoGui, please define the main method as:
       public static void main(String[] args)
Azad
  • 5,047
  • 20
  • 38
Carlo Luther
  • 2,402
  • 7
  • 46
  • 75
  • http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/main.html – m0s Jul 30 '13 at 22:13
  • 1) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). 2) Paint to a `BufferedImage`, display it in a `JLabel`. Call `label.repaint()` if it changes. E.G. as seen in [this answer](http://stackoverflow.com/a/10628553/418556). .. – Andrew Thompson Jul 31 '13 at 18:19
  • .. 3) Don't extend frame or other top level containers. Instead create & use an instance of one. 4) For deploying Java desktop apps., the best option is usually to install the app. using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). JWS works on Windows, OS X & *nix. – Andrew Thompson Jul 31 '13 at 18:20

3 Answers3

4

When you compile run the code, the compiler searchs for main method, that means without main method you can't go to runtime because of the creation of the objects, maybe your code compiles while compiling time, but that's something else.

So, it's not possible to run an aplication without main method.

Azad
  • 5,047
  • 20
  • 38
  • well, but this does not apply to applets, they don't have main methods. Are applets an exception? – Carlo Luther Jul 30 '13 at 22:18
  • 3
    @Luther: Yes, because Applet is not an application, read about [**Life Cycle of an Applet**](http://docs.oracle.com/javase/tutorial/deployment/applet/lifeCycle.html), [it gets loaded from a file and then it proceeds along its lifecycle through initialization, starting, operating, stopping, and finally being destroyed](http://stackoverflow.com/questions/8610475/java-applet-runs-without-a-main-method) – Azad Jul 30 '13 at 22:24
  • 1
    **Applets differ from stand-alone Java applications in that they do not need to implement a main method.** http://stackoverflow.com/questions/8610475/java-applet-runs-without-a-main-method – jsedano Jul 30 '13 at 22:24
  • @anakata: Yes, exactly – Azad Jul 30 '13 at 22:26
  • 2
    *"When you compile the code, the compiler searchs for main method"* ITYM *"When you **run** the code, the **runtime** searchs for main method"* ..mostly because, the JDK compiler would issue no warning. ;) – Andrew Thompson Jul 31 '13 at 18:14
  • 1
    Azad, this is principle of OOP that Java based on that, also, its about how compiler maintain the code during compiling and execution –  Jul 31 '13 at 18:43
1

Add

public static void main(String[] args) {
 new PilotinoGui ().setVisible(true);
}

as the main method in the class

David Xu
  • 5,555
  • 3
  • 28
  • 50
0

As pointed out in the other answers, you need to have a main method:

public static void main(String []args)
{
    PilotinoGui pg = new PilontinoGui();
    pg.setVisible(true);
}

I would recommend using JFrame instead of Frame.

import javax.swing.JFrame;

An extended version of java.awt.Frame that adds support for the JFC/Swing component architecture. You can find task-oriented documentation about using JFrame in The Java Tutorial, in the section How to Make Frames.

Documentation here: http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html

jsedano
  • 4,088
  • 2
  • 20
  • 31