Search code examples
javahtmlapplet

html not appearing for Applet


I tried creating an html file for my applet, but nothing appears.

this is the code to my applet

public class TimeSet extends Applet{

    public TimeSet(){
        //set the title
        frame = new JFrame();
        frame.setTitle("2 hour time set");

        //specify what happens when the close button is clicked
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //set main panel where all other panels will exist in
        mainPanel = new JPanel(new GridLayout(4,1));
        add(mainPanel);

        timePanel();
        ammountPanel();
        durationPanel();
        buttonSet();

        setVisible(true);

    }

and this is the code i'm using for my htm

<Html>
<Head>
<Title>TimeSet</Title>
</Head>

<Body>
<Applet Code="TimeSet.class" width=200 Height=100>
</Applet>
</Body>
</Html>

is there a problem with my code or is there a different process? the html file is in the same folder as the TimeSet.class file. When I run the applet, it works fine


Solution

  • There are two entry point methods, init() and start(), which Java will call on behalf of your Applet. In your case, try adding an init() method to your Applet, which in turn calls your TimeSet() method:

    public class TimeSet extends Applet{
    
        public void init () {
            TimeSet();
        }
    
        public void TimeSet(){
            //set the title
            frame = new JFrame();
            frame.setTitle("2 hour time set");
    
            //specify what happens when the close button is clicked
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //set main panel where all other panels will exist in
            mainPanel = new JPanel(new GridLayout(4,1));
            add(mainPanel);
    
            timePanel();
            ammountPanel();
            durationPanel();
            buttonSet();
    
            setVisible(true);
        }
    }
    

    By the way, as others will likely comment here, Applet is a deprecated technology and you should not plan on using it in a release product.