home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / tree / Simple.java < prev    next >
Text File  |  1996-12-17  |  6KB  |  276 lines

  1. /*
  2.     This class is an extension of the Frame class for use as the
  3.     main window of an application.
  4.  
  5.     You can add controls or menus to Simple with Cafe Studio.
  6.  */
  7.  
  8. import java.awt.*;
  9.  
  10. public class Simple extends Frame {
  11.  
  12.     public Simple() {
  13.  
  14.     super("Simple window");
  15.  
  16.     //{{INIT_MENUS
  17.     MenuBar mb = new MenuBar();
  18.     fileMenu = new Menu("&File");
  19.     fileMenu.add(new MenuItem("&New"));
  20.     fileMenu.add(new MenuItem("&Open..."));
  21.     fileMenu.add(new MenuItem("&Save"));
  22.     fileMenu.add(new MenuItem("Save &As..."));
  23.     menu1 = new Menu("New Menu");
  24.     menu1.add(new MenuItem("New MenuItem"));
  25.     menu1.add(new MenuItem("New MenuItem"));
  26.     menu1.add(new MenuItem("New MenuItem"));
  27.     menu2 = new Menu("New Menu");
  28.     menu2.add(new MenuItem("New MenuItem"));
  29.     menu1.add(menu2);
  30.     fileMenu.add(menu1);
  31.     fileMenu.addSeparator();
  32.     fileMenu.add(new MenuItem("E&xit"));
  33.     mb.add(fileMenu);
  34.     editMenu = new Menu("&Edit");
  35.     editMenu.add(new MenuItem("&Undo"));
  36.     editMenu.addSeparator();
  37.     editMenu.add(new MenuItem("Cu&t"));
  38.     editMenu.add(new MenuItem("&Copy"));
  39.     editMenu.add(new MenuItem("&Paste"));
  40.     mb.add(editMenu);
  41.     helpMenu = new Menu("&Help");
  42.     helpMenu.add(new MenuItem("&About..."));
  43.     mb.add(helpMenu);
  44.     setMenuBar(mb);
  45.     //}}
  46.  
  47.     //{{INIT_CONTROLS
  48.     setLayout(null);
  49.     addNotify();
  50.     resize(insets().left + insets().right + 352, insets().top + insets().bottom + 254);
  51.     //}}
  52.  
  53.     show();
  54.     }
  55.  
  56.     public synchronized void show() {
  57.     move(50, 50);
  58.     super.show();
  59.     }
  60.  
  61.     public boolean handleEvent(Event event) {
  62.  
  63.     if (event.id == Event.WINDOW_DESTROY) {
  64.         hide();         // hide the Frame
  65.         dispose();      // tell windowing system to free resources
  66.         System.exit(0); // exit
  67.         return true;
  68.     }
  69.     return super.handleEvent(event);
  70.     }
  71.  
  72.     public boolean action(Event event, Object arg) {
  73.     if (event.target instanceof MenuItem) {
  74.         String label = (String) arg;
  75.         if (label.equalsIgnoreCase("&About...")) {
  76.         selectedAbout();
  77.         return true;
  78.         } else if (label.equalsIgnoreCase("E&xit")) {
  79.         selectedExit();
  80.         return true;
  81.         } else if (label.equalsIgnoreCase("&Open...")) {
  82.         selectedOpen();
  83.         return true;
  84.         }
  85.     }
  86.     return super.action(event, arg);
  87.     }
  88.  
  89.     public static void main(String args[]) {
  90.     new Simple();
  91.     }
  92.  
  93.     //{{DECLARE_MENUS
  94.     Menu fileMenu;
  95.     Menu menu1;
  96.     Menu menu2;
  97.     Menu editMenu;
  98.     Menu helpMenu;
  99.     //}}
  100.  
  101.     //{{DECLARE_CONTROLS
  102.     //}}
  103.  
  104.     public void selectedOpen() {
  105.     (new FileDialog(this, "Open...")).show();
  106.     }
  107.     public void selectedExit() {
  108.     QuitBox theQuitBox;
  109.     theQuitBox = new QuitBox(this);
  110.     theQuitBox.show();
  111.     }
  112.     public void selectedAbout() {
  113.     AboutBox theAboutBox;
  114.     theAboutBox = new AboutBox(this);
  115.     theAboutBox.show();
  116.     }
  117. }
  118.  
  119.  
  120. /*
  121.     This class is a basic extension of the Dialog class.  It can be used
  122.     by subclasses of Frame.  To use it, create a reference to the class,
  123.     then instantiate an object of the class (pass 'this' in the constructor),
  124.     and call the show() method.
  125.  
  126.     example:
  127.  
  128.     AboutBox theAboutBox;
  129.     theAboutBox = new AboutBox(this);
  130.     theAboutBox.show();
  131.  
  132.     You can add controls to AboutBox with Cafe Studio.
  133.     (Menus can be added only to subclasses of Frame.)
  134.  */
  135.  
  136. class AboutBox extends Dialog {
  137.  
  138.     public AboutBox(Frame parent) {
  139.  
  140.         super(parent, "About", true);
  141.     setResizable(false);
  142.  
  143.     //{{INIT_CONTROLS
  144.     setLayout(null);
  145.     addNotify();
  146.     resize(insets().left + insets().right + 292, insets().top + insets().bottom + 80);
  147.     label1=new Label("Simple Java SDI Application");
  148.     add(label1);
  149.     label1.reshape(insets().left + 12,insets().top + 18,174,16);
  150.     OKButton=new Button("OK");
  151.     add(OKButton);
  152.     OKButton.reshape(insets().left + 204,insets().top + 11,72,26);
  153.     //}}
  154.     }
  155.  
  156.     public synchronized void show() {
  157.     Rectangle bounds = getParent().bounds();
  158.     Rectangle abounds = bounds();
  159.  
  160.     move(bounds.x + (bounds.width - abounds.width)/ 2,
  161.          bounds.y + (bounds.height - abounds.height)/2);
  162.  
  163.     super.show();
  164.     }
  165.  
  166.     public synchronized void wakeUp() {
  167.     notify();
  168.     }
  169.  
  170.     public boolean handleEvent(Event event) {
  171.     if (event.id == Event.ACTION_EVENT && event.target == OKButton) {
  172.         clickedOKButton();
  173.         return true;
  174.     }
  175.     else
  176.  
  177.     if (event.id == Event.WINDOW_DESTROY) {
  178.         hide();
  179.         return true;
  180.     }
  181.     return super.handleEvent(event);
  182.     }
  183.  
  184.     //{{DECLARE_CONTROLS
  185.     Label label1;
  186.     Button OKButton;
  187.     //}}
  188.  
  189.     public void clickedOKButton() {
  190.     handleEvent(new Event(this, Event.WINDOW_DESTROY, null));
  191.     }
  192. }
  193.  
  194.  
  195. /*
  196.     This class is a basic extension of the Dialog class.  It can be used
  197.     by subclasses of Frame.  To use it, create a reference to the class,
  198.     then instantiate an object of the class (pass 'this' in the constructor),
  199.     and call the show() method.
  200.  
  201.     example:
  202.  
  203.     QuitBox theQuitBox;
  204.     theQuitBox = new QuitBox(this);
  205.     theQuitBox.show();
  206.  
  207.     You can add controls, but not menus, to QuitBox with Cafe Studio.
  208.     (Menus can be added only to subclasses of Frame.)
  209.  */
  210.  
  211. class QuitBox extends Dialog {
  212.  
  213.     public QuitBox(Frame parent) {
  214.  
  215.         super(parent, "Quit Application?", true);
  216.     setResizable(false);
  217.  
  218.     //{{INIT_CONTROLS
  219.     setLayout(null);
  220.     addNotify();
  221.     resize(insets().left + insets().right + 261, insets().top + insets().bottom + 72);
  222.     yesButton=new Button("Yes");
  223.     add(yesButton);
  224.     yesButton.reshape(insets().left + 68,insets().top + 10,46,23);
  225.     noButton=new Button("No");
  226.     add(noButton);
  227.     noButton.reshape(insets().left + 135,insets().top + 10,47,23);
  228.     //}}
  229.     }
  230.  
  231.     public synchronized void show() {
  232.     Rectangle bounds = getParent().bounds();
  233.     Rectangle abounds = bounds();
  234.  
  235.     move(bounds.x + (bounds.width - abounds.width)/ 2,
  236.          bounds.y + (bounds.height - abounds.height)/2);
  237.  
  238.     super.show();
  239.     }
  240.  
  241.     public synchronized void wakeUp() {
  242.     notify();
  243.     }
  244.  
  245.     public boolean handleEvent(Event event) {
  246.     if (event.id == Event.ACTION_EVENT && event.target == noButton) {
  247.         clickedNoButton();
  248.         return true;
  249.     }
  250.     else
  251.     if (event.id == Event.ACTION_EVENT && event.target == yesButton) {
  252.         clickedYesButton();
  253.         return true;
  254.     }
  255.     else
  256.  
  257.     if (event.id == Event.WINDOW_DESTROY) {
  258.         hide();
  259.         return true;
  260.     }
  261.     return super.handleEvent(event);
  262.     }
  263.  
  264.     //{{DECLARE_CONTROLS
  265.     Button yesButton;
  266.     Button noButton;
  267.     //}}
  268.  
  269.     public void clickedYesButton() {
  270.     System.exit(0);
  271.     }
  272.     public void clickedNoButton() {
  273.     handleEvent(new Event(this, Event.WINDOW_DESTROY, null));
  274.     }
  275. }
  276.