home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch06 / SpreadSheet.java < prev   
Encoding:
Java Source  |  1998-12-14  |  1.7 KB  |  87 lines

  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5. import javax.swing.*;
  6. import javax.swing.text.*;
  7.  
  8. /*
  9.  * the applet class
  10.  */
  11. public class SpreadSheet extends Applet implements KeyListener{
  12.  
  13. /*
  14.  * the text entry field
  15.  */
  16. TextField textField;
  17.  
  18. /*
  19.  * instance of the sheet panel
  20.  */
  21. Sheet sheet;
  22.  
  23. /*
  24.  * initialize the applet
  25.  */
  26. public void init () {
  27.  
  28.         setBackground(Color.lightGray);
  29.         setLayout(new BorderLayout());
  30.  
  31.        textField = new TextField ("", 80);
  32.        sheet = new Sheet (10, 5, 400, 200, textField);
  33.        add ("North", textField);
  34.        add ("Center", sheet);
  35.        textField.addKeyListener(this);
  36. }
  37.  
  38. /*
  39.  * check for return keypresses
  40.  */
  41. public void keyTyped(KeyEvent e){}
  42. public void keyReleased(KeyEvent e){}
  43. public void keyPressed(KeyEvent e)
  44. {
  45.     char character;
  46.     int x;
  47.     
  48.  
  49.     character = e.getKeyChar();
  50.     x=e.getKeyCode();
  51.  
  52.     //Check whether the user pressed Return
  53.     if (x == 10)
  54.     {   
  55.            sheet.enter (textField.getText ());
  56.     }
  57. }
  58. /*
  59.  * application entry point
  60.  * not used when run as an applet
  61.  * @param args - command-line arguments
  62.  */
  63. public static void main (String args[]) {
  64.  
  65.        Frame f = new Frame ("SpreadSheet");
  66.        SpreadSheet spreadSheet = new SpreadSheet ();
  67.  
  68.        spreadSheet.init ();
  69.  
  70.        f.addWindowListener(new WindowCloser());
  71.        f.setSize (440, 330);
  72.        f.add ("Center", spreadSheet);
  73.        f.show ();
  74. }
  75. }
  76.  
  77. class WindowCloser extends WindowAdapter
  78. {
  79.     public void windowClosing(WindowEvent e)
  80.     {
  81.         Window win = e.getWindow();
  82.         win.setVisible(false);
  83.         win.dispose();
  84.         System.exit(0);
  85.     }
  86. }
  87.