0

I have a working application, but I need to convert it to an applet. My main method isn't located in my frame class so I can't just extend JApplet and change my main method to init(). Is there an easy way to "wrap" an applet around an application.

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
Zyvo
  • 11
  • 1
  • 4

1 Answers1

1

I would separate out the guts of your UI creation, then call it either from main() or init(). See the below example:

public class Test extends Applet {
    private JPanel mainPanel;

    // run as application
    public static void main(String[] args) {
        Test test = new Test();
        test.createUI();

        JFrame frame = new JFrame();
        frame.add(test.mainPanel);
        frame.pack();
        frame.setVisible(true);
    }


    // run as applet
    public void init() {
        createUI();
        add(mainPanel);
    }


    // create your UI here
    private void createUI() {
        mainPanel = new JPanel();
        mainPanel.add(new JButton("Test"));
    }
}
martinez314
  • 12,162
  • 5
  • 36
  • 63
  • So this somewhat worked, but now when I run the program it doesn't set the correct size, which really doesn't matter because when on a webpage it'll be the size I set in the HTML code. The only problem is that when I go to export it through eclipse, it can't find a main class. It finds a main class when I want to run it, but not when I try to export it. – Zyvo Mar 22 '13 at 13:09
  • @RiFFRaFF If you run it from Eclipse, it should automatically create a Run Configuration. You should find this Run Configuration when you try to Export it, which is what points it to the main class. Can't think of a reason why this wouldn't work. – martinez314 Mar 22 '13 at 17:57