Question

I'm about to start an applet project and I'm just wondering if the class that extends JApplet has to be your main class. I know that all the code from the applet is called using the init method, but I was wondering if I could have a Main class that doesn't extend JApplet, and then a second class dedicated to the Applet with the init method in it and that extends JApplet. I'm wondering if the applet would still work and be invoked by the init method if there was also a main method in the Main class. Basically I'm wondering can you have both a class with the init method and a class with a main method.

Was it helpful?

Solution 2

Do you have to extend JApplet in your main class?

I'm just wondering if the class that extends JApplet has to be your main class.

No, it doesn't have to ... in either case.

An application that is designed to run as an applet does not need a static void main(String[]) method. You can include one if it makes sense for your application, and if you do, the method does not need to be a method of your JApplet subclass. But it may be, if you want.

So what you are proposing to do (with two classes) can be made to work, and (IMO) seems like a good approach.

OTHER TIPS

Yeah a hybrid class is definitely possible. You can just wrap your content in a subclass of JPanel and then call it from your starting class as follows:

public class StartingClass extends JApplet {

    public static void main(String[] args) {
        JFrame window = new JFrame("Application Name");
        MyPanel content = new MyPanel();
        window.setContentPane(content);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.pack();
        window.setVisible(true);
    }

    public void init() {
        MyPanel content = new MyPanel();
        setContentPane(content);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top