// Lars Sorensen TYJava in 21 days // excode from pages 282-287 // import java.awt.*; public class GUI extends java.applet.Applet { Frame window; Point d; public void init() { add(new Button("Open Window")); add(new Button("Close Window")); window = new MyFrame("A Popup Window"); window.resize(75,75); window.hide(); } // end of init public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { String label = (String)arg; if (label.equals("Open Window")) { if (!window.isShowing()) window.show(); } else if (label.equals("Close Window")) { if (window.isShowing()) window.hide(); } } return true; } // end of action } // end of class GUI class MyFrame extends Frame { Dialog dl; Label l; TextField tf; MyFrame(String title) // constructor { super(title); MenuBar mb = new MenuBar(); Menu m = new Menu("Colors"); m.add(new MenuItem("Red")); m.add(new MenuItem("Blue")); m.add(new MenuItem("Green")); m.add(new MenuItem("-")); m.add(new CheckboxMenuItem("Reverse Text")); m.add(new MenuItem("Set Text...")); mb.add(m); setMenuBar(mb); setLayout(new GridLayout(1,1)); l = new Label("Howdy Folks!", Label.CENTER); add(l); // make dialog for this window dl = new Dialog(this, "Enter Text",true); dl.setLayout(new GridLayout(2,1,30,30)); tf = new TextField(l.getText(),20); dl.add(tf); dl.add(new Button("Press Here")); dl.resize(75,75); } // end of constructor MyFrame public boolean action(Event evt, Object arg) { String label = (String)arg; if (evt.target instanceof MenuItem) { if (label.equals("Red")) { setBackground(Color.red); repaint(); } else if (label.equals("Blue")) { setBackground(Color.blue); repaint(); } else if (label.equals("Green")) { setBackground(Color.green); repaint(); } else if (label.equals("Set Text...")) dl.show(); } if (evt.target instanceof CheckboxMenuItem) { if (getForeground() == Color.black) setForeground(Color.white); else setForeground(Color.black); } if (evt.target instanceof Button) { if (label == "Press Here") { System.out.println(tf.getText()); l.setText(tf.getText()); dl.hide(); } } return true; } // end of action } // end of class MyFrame