Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.

Similar presentations


Presentation on theme: "Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet."— Presentation transcript:

1 Creating Applets

2 What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet is a GUI application. An applet is a GUI application. Think of an applet as a GUI program that runs under the control of a web browser instead of a window. Think of an applet as a GUI program that runs under the control of a web browser instead of a window. If you know how to create a GUI application using the JFrame class, it is easy to create an applet. If you know how to create a GUI application using the JFrame class, it is easy to create an applet.

3 To create an applet you must: To create an applet you must: Step 1. Create your applet. Step 1. Create your applet. Step 2. Create a web page that displays the applet. Step 2. Create a web page that displays the applet.

4 Step 1 – Creating an Applet. Step 1 – Creating an Applet. Creating an applet is easy. Creating an applet is easy. All we have to do is take a GUI application and convert it. All we have to do is take a GUI application and convert it. Before we do that, however, we have to know the differences between a GUI app and an applet. Before we do that, however, we have to know the differences between a GUI app and an applet.

5 Differences Between GUI Windows and Applets: Differences Between GUI Windows and Applets: GUI Window Applications Applets -Uses the JFrame class -Uses the JApplet class -Uses a constructor to build the GUI components -Uses the init( ) method to build the GUI components. You don’t need a constructor -Uses a static main method to create an instance of the class. -The web browser automatically creates an instance of the class. -The following methods are usually used with a constructor: -setTitle -setTitle -setSize -setSize -set DefaultCloseOperation -set DefaultCloseOperation -setVisible -setVisible -You don’t have to use those methods because the web browser: -already has a title. -already has a title. -and HTML file set the size of an applet. -and HTML file set the size of an applet. -provides an exit button. -provides an exit button. -makes the applet visible. -makes the applet visible.

6 To convert a GUI application to an applet: To convert a GUI application to an applet: Change “extends JFrame” to “extends JApplet” Change “extends JFrame” to “extends JApplet” Get rid of your Exit Button and the Event Listener for the Exit Button. Get rid of your Exit Button and the Event Listener for the Exit Button. Get rid of the application’s dimensions (the Height and Width). Get rid of the application’s dimensions (the Height and Width). Change the Constructor to the init( ) method. Change the Constructor to the init( ) method. Get rid of the setTitle( ) method, the setSize( ) method, the SetDefaultCloseOperation( ) method, and the setVisible( ) method. Get rid of the setTitle( ) method, the setSize( ) method, the SetDefaultCloseOperation( ) method, and the setVisible( ) method. Get rid of your public static main( ) method. Get rid of your public static main( ) method.

7 public Countdown( ) { //Set the window title. setTitle("Countdown Calculator"); //Set the size of the window setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //Specify what happens when the close button is clicked setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Build the panel and add it to the frame. //This is a method in this class buildPanel(); //Add the panel to the frame's content pane add(panel); //Display the window setVisible(true); } Before: After: public void init( ) { //You don't set the window title for an Applet. //setTitle("Countdown Calculator"); //You don't do the following because the web page sets the size //setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //You don't do the following because an applet does not close //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Build the panel and add it to the frame. //This is a method in this class buildPanel(); //Add the panel to the frame's content pane add(panel); //You do not have to set the applet to visible //setVisible(true); }

8 Step 2 – Creating a Web Page that displays an applet. Step 2 – Creating a Web Page that displays an applet. To display an applet, we need to create a web page. To display an applet, we need to create a web page. Creating a simple web page is easy: Creating a simple web page is easy: Create a notepad file. Create a notepad file. Change the filename extention from “.txt” to “.html” Change the filename extention from “.txt” to “.html” Put the code in the next slide in the HTML file…. Put the code in the next slide in the HTML file….

9 Simple HTML Page Java Applet! The following is a java applet:

10 Simple HTML Page Java Applet! The following is a java applet: This is the line of code in your HTML file that calls the “.class” file. Here is where you set the width and the height of your applet.

11 Remember, we have to compile our source file (Countdown.java) to create a “.class” file. When compiled, an applet may have more than one class file. Remember, we have to compile our source file (Countdown.java) to create a “.class” file. When compiled, an applet may have more than one class file. For example, our demo for Countdown.java created 3 files: For example, our demo for Countdown.java created 3 files: Countdown$1.class Countdown$1.class Countdown$CalcButtonListener.class Countdown$CalcButtonListener.class Countdown.class Countdown.class These files usually go into a folder called “classes.” These files usually go into a folder called “classes.” You must tell the HTML file where: You must tell the HTML file where: The main class file is  “Coundown.class” The main class file is  “Coundown.class” The directory for all of the class files  “classes” The directory for all of the class files  “classes”

12

13 In Netbeans, an HTML file will be created for you in the project’s “build” folder. This HTML file will reference classes inside the “classes” folder. In Netbeans, an HTML file will be created for you in the project’s “build” folder. This HTML file will reference classes inside the “classes” folder.

14 The Netbean’s HTML file default width is 350 and the default height is 200. You will need to CHANGE those to ensure that your Applet is formatted correctly. The Netbean’s HTML file default width is 350 and the default height is 200. You will need to CHANGE those to ensure that your Applet is formatted correctly.

15 Misc: Misc: Sorry, but you can’t create programs using the Netbeans GUI Builder and then convert them into Applets. Bummer! Sorry, but you can’t create programs using the Netbeans GUI Builder and then convert them into Applets. Bummer!


Download ppt "Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet."

Similar presentations


Ads by Google