applets
play

APPLETS Write an HTML documents to host an applet Understand simple - PowerPoint PPT Presentation

APPLETS Write an HTML documents to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple Swing applet and use a JLabel Add JTextField and JButton components to Swing applet Using the


  1. APPLETS • Write an HTML documents to host an applet • Understand simple applets • Use Labels with simple AWT applets • Write a simple Swing applet and use a JLabel • Add JTextField and JButton components to Swing applet • Using the setLocation() and setEnabled() methods 1

  2. Write an HTML documents to host an Applet (1) • As you know, applications are stand-alone programs. • In contrast, applets are programs that are called from within another application . • You run applets within a page on the internet, an intranet, or a local computer from within another program called Applet Viewer , which comes with the Java Developer’s kit . 2

  3. Write an HTML documents to host an Applet (2) • To view an applet, it must be called from within another document written in HTML – Hypertext Markup Language . • HTML , is a simple language used to create Web pages for the Internet. • HTML , contain many commands that allow you to format text on a Web pages , import graphic images and link your page to other Web pages . 3

  4. When you create an applet, you do the following: • Write the applet in the java programming language, and save it with a .java file extension, just as when you write a java application. • Compile the applet into bytecode using the javac command, just as when you write a java application. • Write an HTML document that includes a statement to call your compiled java class. 4

  5. HTML • We don’t need to learn the entire HTML language; • We need to know two of the HTML commands, called tags . • The tag that begins every HTML document is <html> - is not case sensitive . • The tag that end every HTML document is </html> 5

  6. To run an Applet from within an HTML document • You add an <APPLET> and </APPLET> tag pair. • Usually you place three attributes within the <APPLET> tag: • CODE , WIDTH and HEIGHT , attributes or arguments – with them HTML tag can do something in a certain way. EXAMPLE: • <APPLET CODE = “ Aclass.class ” WIDTH = 300 HEIGHT = 200> • </APPLET> 6

  7. Understanding Simple Applets Structure of Applet and JApplet classes: • Java.lang.Object • Java.awt.Component • Java.awt.Container • Java.awt.Panel • Java.applet.Applet • Javax.swing.JApplet 7

  8. Four Methods included in every Applet(1) • In an application, the main() method calls other methods ( the following are included in every applet ) you write: • public void init() - Called once by the applet container when the applet is loaded for execution. - action performed here are initializing fields, creating GUI components, loading sounds to play, loading images to display. 8

  9. Four Methods included in every Applet(2) • public void start() - called by the applet container after method init completes execution. - in addition, if the user browses to another website and later returns to the applet’s HTML page, method start is called again. 9

  10. Four Methods included in every Applet(3) • public void paint( Graphics g ) - Called by the applet container after methods init and start . - method paint is also called when the applet needs to be repainted. 10

  11. Four Methods included in every Applet(4) • public void stop() - This method is called by the applet container when the user leaves the applet’s web page by browsing to another web page . • public void destroy() - this method is called by the applet container when the applet is being removed from memory. 11

  12. Using Labels with Simple AWT Applet • Label is a built-in class that holds text that you can display within an applet. • Calling a label constructor without any arguments, example: Label greetings = new Label • You can assign some text to the label with the: setText() method • Example: greetings. setText(“Hi There”) ; or greetings = new Label(“Hello . Who are You”) ; 12

  13. • To add a component to an applet window, you use the add() method . • Example: if a Label is defined as Label greeting = new Label(“Hello . Who are You”) ; • You can place a greeting within an applet using the command: add(greeting); 13

  14. To create and run the Greet Applet • Open a new text file in your text editor. • Enter the code shown on the next slide. 14

  15. Program to create an applet that displays “Hello. Who are You” import java.applet.*; import java.awt.*; public class Greet extends Applet { Label greeting = new Label(“Hello. Who are You”); public void init() { add(greeting); } } • Save the file as Greet.java. • Compile the program. 15

  16. To create a simple HTML document to run Greet Open a new file in your text editor, and then type the HTML document that run the Applet. • Open a new file in your text editor. • Type the opening HTML tag, <HTML> . • On the next line, type the opening APPLET tag that contains the applet’s name and dimensions: <APPLET CODE = “Greet . class” WIDTH = 450 HEIGHT = 200> • On the next line, type the applet’s closing tag :</APPLET> • On the next line, type the closing HTML tag: </HTML> • Save the file as TestGreet.html 16

  17. Write a simple Swing Applet and using a JLabel • The counterpart to the AWT Label is a JLabel . • JLabel is a built-in class that holds text that you can display within an applet. 17

  18. Structure of JLabel class • Available constructors for the JLabel class. • JLabel() creates a JLabel instance with no image and with an empty string for the title. • JLabel(icon image) creates a JLabel instance with the specified image. 18

  19. Content Pane • It is an object of the container class from the Java.awt package. • A container object can be created using the getContentPane() method . • To create a container object named con , the syntax to use is: Container con = getContentPane(); • Then the stmt: JLabel greeting = new JLabel(); • Adds the greeting object to the content pane with the stmt: con.add(greeting); 19

  20. Creating JGreet • Open a new text file in your text editor. • Enter the code on the next slide. • Save the file as JGreet.java 20

  21. Creating JGreet import javax.swing.*; import java.awt.*; public class JGreet extends JApplet { JLabel greeting = new JLabel("Hello. Who are you?"); public void init() { Container con = getContentPane(); con.add(greeting); } } 21

  22. Creating JGreet • Compile the program. • Open a new file in your text editor, and then type the html document that will run the JApplet. • Save the file as TestJGreet.html. 22

  23. Changing a JLabel’s Font • You use: setFont() method . • To construct a font object , you need three arguments : typeface , style , and point size. • The typeface is a string representing a font. 23

  24. Changing a JLabel’s Font • To create font object, use the synthax: Font headlineFont = new Font(“Tahoma”, Font.BOLD, 36) • Then you use the setFont method to assign the font to a Label with the stmt: greeting.setFont(headlineFont); 24

  25. To change the appearance of the greeting in the JGreet Applet • Open the Jgreet.java file in your text editor and change the class name to JGreet2 . • Position the insertion point at the end of the line that declares the greeting Label, and then press enter to start a new line of text. • Declare a Font object named bigFont by typing the following: Font bigFont = new Font(“Arial Black”, Font.ITALIC, 24); • Place the insertion point to the right of the opening curly brace of the init() method, and then press Enter to start a new line. • Set the greeting font to bigFont by typing greeting.setFont(bigFont); • Save the file using the filename JGreet2.java . • Compile the program • Run the applet, changing the TestJGreet.html document to TestJGreet2.html . 25

  26. Adding JTextField and JButton Components to Swing Applets • You can construct a JTextField object using one of several constructor: • Public JTextField() : constructs a new JTextField . • Public JTextField(int numcolumns) : constructs a new empty JTextField with a specified number of columns. • Public JTextField(String text) : constructs a new JTextField initialized with the specified text. • Public JTextField(String text int columns) : constructs a new JTextField initialized with the specified text and columns. 26

  27. • To provide a JTextField for a user to answer the “Who are you?” question, you can code: JTextField answer = new TextField(10) • To add the JTextField named answer to an applet, you write con.add(answer); where con is container object declared as Container con = getContentPane(); • The setText() method; allows you to change the text in a JTextField that has already been created as in answer. setText(“Thank you”) ; • The getText() method, allows you to retrieve the string of text in a JTextField • requestFocus() method, allows the insertion point to appears within the JTextField. 27

  28. Jbutton Constructors • Public Jbutton() creates a button with no set text. • Public Jbutton(Icon, icon) creates a button with an icon of type Icon or ImageIcon. • Public Jbutton(String text) creates a button with text. • Public Jbutton(String text, Icon icon) creates a button with initial text and an icon of type Icon or ImageIcon. 28

Recommend


More recommend