Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
A package joins together, as a .jar file, the various classes and other resources (such as icons) that belong to a Java Application. Compiling the package creates a .jar file. The .jar file is the executable Java Application. Running the.jar file runs the Java Application.
Java applications need a main() method. To turn an applet into an application, add a main() method, as shown in the code below.
Example JApplet (Run Example Code)
package applicationdemo; import java.awt.*; import javax.swing.*; // To turn an applet into an application, include the main() method public class ApplicationDemo extends JApplet { // To turn an applet into an application, include the main() method public static void main(String[] args) { // application title and dimensions final String title = "Application Title"; final Dimension applicationFrameSize = new Dimension(300, 200); // make an application frame to hold the applet final JFrame applicationFrame = new JFrame(title); applicationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); applicationFrame.setSize(applicationFrameSize); // place the applet inside the application's frame final JApplet applet = new ApplicationDemo(); applicationFrame.setLayout(new BorderLayout()); applicationFrame.getContentPane().add("Center", applet); applet.init(); applicationFrame.setVisible(true); } @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { public View() { super(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Java Application Demo", 50, 50); } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.