I am trying to convert a working simple Java application to an applet. The application consists of a main.java and a gooey.java
Main.java
package hellow_convert;
import javax.swing.JApplet;
public class main extends JApplet {
public static void main(String[] args) {
gooey gui = new gooey();
}
public void init()
{
gooey gui = new gooey();
}
public void stop() {}
}
gooey.java
package hellow_convert;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class gooey {
public JFrame f = new JFrame();
private JPanel pnlNorth = new JPanel();
private JButton btnNorth = new JButton("North");
private JMenuBar mb = new JMenuBar(); // MenuBar
private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar
private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry
public gooey(){
f.setJMenuBar(mb);
mb.add(mnuFile);
mb.add(mnuHelp);
pnlNorth.add(btnNorth);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(pnlNorth, BorderLayout.NORTH);
f.setBounds(100, 100, 200, 100);
}
}
It looks like this.

I just cant seem to make it run as an applet. When I run it in debug, an applet window opens, and then, the JFrame window pops up (just as in the application). As an application, it runs as expected, but how do I get controls into the Applet window?
I’m new to this. Any help is appreciated!
Well, JApplet is a Swing container itself and thus you’d have to use the applet’s content pane in your class
gooey, instead of always using a JFrame (which is a desktop window and wouldn’t not work with an applet).Try and pass either the JApplet or the JFrame to the
gooey()constructor, instead of creating the JFrame in that class.Edit: your constructor might look like this:
}
Then call it like:
or
you could also do this in your main class, since it is already an applet instance
The
setBounds(...)should only be called when dealing with a JFrame.