Creating a frame within an applet

8,545 views

Published on

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
8,545
On SlideShare
0
From Embeds
0
Number of Embeds
69
Actions
Shares
0
Downloads
85
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Creating a frame within an applet

  1. 1. Creating a Frame within an Applet http://improvejava.blogspot.in/ 1
  2. 2. ObjectiveOn completion of this period, you would beable to know• Creating a Frame with in an Applet http://improvejava.blogspot.in/ 2
  3. 3. RecapIn the previous class, you have leant• Different methods of Frame class• They are necessary for working with Frames http://improvejava.blogspot.in/ 3
  4. 4. Creating a Frame in an Applet• Creating a new frame window from within an applet is actually quite easy• The following steps may be used to do it 1. Create a subclass of Frame 2. Override any of the standard window methods, such as init( ), start( ), stop( ), and paint( ) 3. Implement the windowClosing( ) method of the WindowListener interface, calling setVisible(false) when the window is closed http://improvejava.blogspot.in/ 4
  5. 5. Creating a Frame in an Applet contd..• Once you have defined a Frame subclass, you can create an object of that class• But it will not be initially visible• You make it visible by calling setVisible( )• When created, the window is given a default height and width• You can set the size of the window explicitly by calling the setSize( ) method http://improvejava.blogspot.in/ 5
  6. 6. An Example Program• The following applet creates a subclass of Frame called SampleFrame• A window of this subclass is instantiated within the init( ) method of AppletFrame• This example overrides the applet window’s start( ) and stop( ) methods http://improvejava.blogspot.in/ 6
  7. 7. An Example Program contd..// Create a child frame window from within an applet.import java.awt.*;import java.awt.event.*;import java.applet.*;/*<applet code="AppletFrame" width=300 height=50></applet>*/// Create a subclass of Frame.class SampleFrame extends Frame { SampleFrame(String title) { super(title); http://improvejava.blogspot.in/ 7
  8. 8. An Example Program contd.. // create an object to handle window events MyWindowAdapter adapter = new MyWindowAdapter(this); // register it to receive those events addWindowListener(adapter);}public void paint(Graphics g) { g.drawString("This is in frame window", 10, 40); }} http://improvejava.blogspot.in/ 8
  9. 9. An Example Program contd..// Create frame window.public class AppletFrame extends Applet { Frame f; public void init() { f = new SampleFrame("A Frame Window"); f.setSize(250, 250); f.setVisible(true); } http://improvejava.blogspot.in/ 9
  10. 10. An Example Program contd.. public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } public void paint(Graphics g) { g.drawString("This is in applet window", 10, 20); }} http://improvejava.blogspot.in/ 10
  11. 11. An Example Program contd..• Sample output from this program is shown here Fig. 66.1 Output of SampleFrame program http://improvejava.blogspot.in/ 11
  12. 12. Summary• The steps for creating a new frame window from within an applet are 1. Create a subclass of Frame 2. Override any of the standard window methods, such as init( ), start( ), stop( ), and paint( ) 3. Implement the windowClosing( ) method of the WindowListener interface, calling setVisible(false) when the window is closed http://improvejava.blogspot.in/ 12
  13. 13. Quiz1. Whether the Frame object is directly visible once it is created? a) No b) Yes http://improvejava.blogspot.in/ 13
  14. 14. Frequently Asked Questions1. Write the steps for creating a frame in an applet2. Write a program for creating a frame in an applet http://improvejava.blogspot.in/ 14

×