Sei sulla pagina 1di 22
UNIT |61 Graph User nerfae GU) 4.5 Java Applets Objectives 1. Describe Java applets. 2. Develop a simple Java applet. Introduction ‘When Java programming was introduced in a public demonstration for the first time in 1995, ewo Java applets were shown that greatly impressed the audiences and brought about he popularity of Java. The reason is that web pages could contain only seatie texts and images until that time, but the two applets maved. Later in the same year, it was announced thar che Web browser, Netscape Navigator, would incorporate the Java technology that enabled all users in the world to enjoy the power of Java applets. Applets are classes written in Java. Similar to an image specified in a web page, ifa web page specifies an appler, che browser downloads the class from the web server and starts a Java vietual machine (JVM) for executing the downloaded class. Ifyou have installed che Java SDK to your machine, the web browser of your machine was configured for starting a JVM for executing Java applets, ‘Asan applet is written in Java, it can accept user entries and process them according, to the steps written in the program, and output the result co the user. When a web page with a Java applet is shown on the screen, a region on the page is reserved for the Java applet. You can consider it as a panel or canvas in which GUI components can be placed or where the output can be drawn graphically om i. There are several advantages of writing software as Java applets: 1. As applets are written in Java, which is cross-platform, applets embedded in web pages can be executed by various platforms. For example, the same applet can be executed on a machine running a version of Microsoft ‘Windows or UNIX without modifying a single line of program, 2. Applets can use the same rich standard software library as usual GUI applications written in Java 3. Applet classes are downloaded automatically to the client machines whenever the web browsers browse the web page that embeds an applet. ‘Therefore, no software installations are required. This feature is especially significant if the Java applet is to be executed by all staff members in a large company or by public users from all over the world. 62) WAWASAN OPEN UNIVERSITY 1ec102 Computing 4, ‘The JVM that is started co execute Java applets implements special measures to prevent the Java applets from performing operations that may violate system security, such as accessing the files stored on the compucer drives. In the following subsections, we discuss how to write Java applets and embed them in web pages to be viewed by web browsers. Developing a simple Java applet Java applets are downloaded co user web browsers as compiled class files. Therefore, ic is necessary to write the applet, place the compiled class file on the web server after compilation and specify the class name in the web page. To write Java applet, ‘you have to write a class that is a subclass of javax-swing.Japplet (or simply uapelet). ‘A anpplet object is a areame-like container to which GUI components can be added. You can apply the knowledge of building GUI that you learnt earlier in this tunic co construct a Java applee. One difference is that you usually use the pack () method of sezane to set the GUI components to their optimal sizes, but the size of an applet in a web page is specified in the web page and the vxpo2et class therefore does not have the pack () method. Defining your own applet classes To write a subclass of the sappiee class, che definition of an applet class should look like: public class ClasName extends Japplet [ // attribute public ClasNamet) ( 11 368 COI components on it ' Ifthe applet acts as an event listener object; the class should implement the suitable listener incerface, such as: public class ClasName extends Inpplet implements Listener-interfice-list ( ‘An example class sie1 Loapplit is weiten in Figure 4.58 to illustrare how to write a Java applet. uNiTa phic User interac (GU) 1] Resolve classes in java.awt, Java.awt.ovent, Javex.swing package import java-awt.*7 Amport java-aut.event.*7 import javax.swing.*7 // definition of clas Helloapplet public class Helloseplet extends JApplot implements ActionListener ( I) neceibute private JButton button ~ new JButton(*Please click me"); If Constructor public Helloapplet() ( /1 Get the content pane for adding component Container contentPane = getContentPanc() /1 Aad the JButton object to the content pane content Pane. add (button) ; /1 Register the Hlellospplet object to be the Action category /1 event handler of the JButton button .addctioniistener (this): 1 // Called if the sButton object is clicked Public void actionPerformed(ActionEvent e) { JOptionPane. showMessageDialog(this, “Hello World"); Figure 4.58 HelloAppletjava ‘The constructor of a Java applet is similar to the constructor of the definition of a usual GUI class, except thac there are no calls co the methods setDefaultCloseOperation() and pack() ‘The constructor of the He11oApplet creates a dButton object to be added to its content pane. As the default layout manager of a JApplet is Bordertayout, the button is by default added to the center region of the content pane and the JButton object will occupy the entire region of the JApptet as all other regions contain no GUI components. ‘Whenever a web browser reads 2 web page chat embeds a Java applet, it downloads the Java applet class, loads it into che JVM and creates an object for execution. ‘Therefore, unlike usual GUI program, itis not necessary to write driver program to start a Java applet. Instead, you have to prepare a hypertext mark-up language (HTML) file thac embeds the Java applet. Lets sce in the next subsection on how to prepare the HTML file

Potrebbero piacerti anche

  • WUC103 Unit2-2
    WUC103 Unit2-2
    Documento10 pagine
    WUC103 Unit2-2
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-4
    WUC103 Unit2-4
    Documento10 pagine
    WUC103 Unit2-4
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-3
    WUC103 Unit2-3
    Documento16 pagine
    WUC103 Unit2-3
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit5-1-2
    WLA105 Unit5-1-2
    Documento22 pagine
    WLA105 Unit5-1-2
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit5-3
    WLA105 Unit5-3
    Documento18 pagine
    WLA105 Unit5-3
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-1
    WUC103 Unit2-1
    Documento24 pagine
    WUC103 Unit2-1
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit4-1-2
    WLA105 Unit4-1-2
    Documento24 pagine
    WLA105 Unit4-1-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-3n4
    TCC102 Unit5-3n4
    Documento14 pagine
    TCC102 Unit5-3n4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 Summary
    TCC102 Unit3 Summary
    Documento16 pagine
    TCC102 Unit3 Summary
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit4-3
    WLA105 Unit4-3
    Documento18 pagine
    WLA105 Unit4-3
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-1
    WLA105 Unit2-1
    Documento20 pagine
    WLA105 Unit2-1
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-4
    WLA105 Unit2-4
    Documento24 pagine
    WLA105 Unit2-4
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-2
    WLA105 Unit2-2
    Documento6 pagine
    WLA105 Unit2-2
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-3
    WLA105 Unit2-3
    Documento10 pagine
    WLA105 Unit2-3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-5
    TCC102 Unit5-5
    Documento12 pagine
    TCC102 Unit5-5
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-1
    TCC102 Unit5-1
    Documento22 pagine
    TCC102 Unit5-1
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-2
    TCC102 Unit5-2
    Documento20 pagine
    TCC102 Unit5-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-2
    TCC102 Unit4-2
    Documento26 pagine
    TCC102 Unit4-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-4
    TCC102 Unit4-4
    Documento14 pagine
    TCC102 Unit4-4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 3
    TCC102 Unit3 3
    Documento14 pagine
    TCC102 Unit3 3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-1
    TCC102 Unit4-1
    Documento14 pagine
    TCC102 Unit4-1
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-3
    TCC102 Unit4-3
    Documento12 pagine
    TCC102 Unit4-3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 4
    TCC102 Unit3 4
    Documento14 pagine
    TCC102 Unit3 4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 5
    TCC102 Unit2 5
    Documento13 pagine
    TCC102 Unit2 5
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 3
    TCC102 Unit2 3
    Documento12 pagine
    TCC102 Unit2 3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 2
    TCC102 Unit3 2
    Documento20 pagine
    TCC102 Unit3 2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 1
    TCC102 Unit3 1
    Documento14 pagine
    TCC102 Unit3 1
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 Summary
    TCC102 Unit2 Summary
    Documento25 pagine
    TCC102 Unit2 Summary
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 4
    TCC102 Unit2 4
    Documento24 pagine
    TCC102 Unit2 4
    api-26781128
    Nessuna valutazione finora