Java Applications

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);
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>