home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / Version / TestApplet.java < prev    next >
Encoding:
Java Source  |  1996-10-10  |  1.7 KB  |  85 lines

  1. // TestApplet.java
  2. //
  3. // Created 09/25/96
  4. //
  5. // (C)Copyright 1996 Microsoft Corporation, All rights reserved.
  6. //
  7.  
  8. import java.applet.*;
  9. import java.awt.*;
  10. import java.io.PrintStream;
  11.  
  12.  
  13. public
  14. class TestApplet extends Applet
  15. {
  16.     protected GridBagLayout grid;
  17.     protected GridBagConstraints c;
  18.     protected TextArea ta;
  19.     protected PrintStream out;
  20.     boolean redirect = true;
  21.  
  22.  
  23.     // By default, all applet output will be redirected into a text area.
  24.  
  25.     public void init ()
  26.     {
  27.         TextArea ta = new TextArea();
  28.         ta.setEditable(false);
  29.         add(ta);
  30.         out = new PrintStream(new AppletOutputStream(ta), true);
  31.         
  32.         if (redirect)
  33.         {
  34.             System.out = System.err = out;
  35.         }
  36.     }
  37.     
  38.  
  39.     public TestApplet ()
  40.     {
  41.         grid = new GridBagLayout();
  42.         setLayout(grid);
  43.         c = new GridBagConstraints();
  44.         c.fill = GridBagConstraints.BOTH;
  45.         c.weightx = 1;
  46.         c.weighty = 1;
  47.     }
  48.  
  49.  
  50.     public TestApplet (boolean i_redirect)
  51.     {
  52.         this();
  53.         redirect = i_redirect;
  54.     }
  55.  
  56.  
  57.     // Make sure all components are added with proper constraints.
  58.  
  59.     public Component add (Component comp)
  60.     {
  61.         grid.setConstraints(comp,c);
  62.         return super.add(comp);
  63.     }
  64.  
  65.  
  66.     public static void main (TestApplet applet, String title, String[] args)
  67.     {
  68.         try
  69.         {
  70.             Frame f = new TestFrame(title);
  71.             f.add("Center",applet);
  72.             applet.init();
  73.             f.pack();
  74.             f.show();
  75.             applet.start();
  76.         }
  77.         catch (Throwable e)
  78.         {
  79.             System.err.println("main: error: "+e);
  80.             e.printStackTrace();
  81.         }
  82.     }
  83. }
  84.  
  85.