1 /* 2 * File: PopUpFrame.java 3 * 4 * This applet shows how to create and show a separate frame with 5 * control buttons. 6 * 7 */ 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.applet.*; 12 13 public class PopUpFrame extends Applet 14 implements ActionListener 15 { 16 Color backcolor = Color.magenta; 17 NewFrame myframe; 18 Canvas justcolor; 19 Button getframe; 20 21 public void init() 22 { 23 setLayout(new BorderLayout()); 24 myframe = new NewFrame(this); // see class definition below 25 26 getframe = new Button("Get Control Frame"); 27 getframe.addActionListener(this); 28 add(getframe, BorderLayout.SOUTH); 29 30 justcolor = new Canvas(); 31 justcolor.setBackground(backcolor); 32 add(justcolor, BorderLayout.CENTER); 33 } 34 35 // this class is the listener for the button in the applet as 36 // well as the three buttons on the separate frame. 37 38 public void actionPerformed(ActionEvent evt) 39 { 40 if (evt.getSource() == getframe) 41 { myframe.setVisible(true); } // shows the frame 42 43 else if (evt.getActionCommand().equals ("Quit")) 44 { myframe.setVisible(false); // hides the frame 45 justcolor.setBackground (backcolor); } 46 47 else if (evt.getActionCommand().equals ("Change to Blue")) 48 { justcolor.setBackground (Color.blue); } 49 50 else if (evt.getActionCommand().equals ("Change to Cyan")) 51 { justcolor.setBackground (Color.cyan); } 52 } 53 } 54 55 class NewFrame extends Frame 56 { 57 Button closewindow = new Button("Quit"); 58 Button bluebutton = new Button("Change to Blue"); 59 Button cyanbutton = new Button("Change to Cyan"); 60 61 // many Java programmers call the pointer to the calling class 62 // the "outerparent" - here it is the "caller" 63 64 NewFrame(ActionListener caller) 65 { 66 setTitle("Change Color"); 67 setSize(300,300); 68 setLayout(new GridLayout(3,1,2,2)); 69 70 bluebutton.addActionListener(caller); 71 add(bluebutton); 72 cyanbutton.addActionListener(caller); 73 add(cyanbutton); 74 closewindow.addActionListener(caller); 75 add(closewindow); 76 77 // all components can generate WindowEvents 78 addWindowListener (new CloseWindow()); 79 } 80 } 81 82 // responds to an event generated by the close window button 83 84 class CloseWindow extends WindowAdapter 85 { 86 public void windowClosing (WindowEvent e) 87 { 88 e.getWindow().setVisible (false); 89 } 90 }