/* * File: PopUpFrame.java * * This applet shows how to create and show a separate frame with * control buttons. * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class PopUpFrame extends Applet implements ActionListener { Color backcolor = Color.magenta; NewFrame myframe; Canvas justcolor; Button getframe; public void init() { setLayout(new BorderLayout()); myframe = new NewFrame(this); // see class definition below getframe = new Button("Get Control Frame"); getframe.addActionListener(this); add(getframe, BorderLayout.SOUTH); justcolor = new Canvas(); justcolor.setBackground(backcolor); add(justcolor, BorderLayout.CENTER); } // this class is the listener for the button in the applet as // well as the three buttons on the separate frame. public void actionPerformed(ActionEvent evt) { if (evt.getSource() == getframe) { myframe.setVisible(true); } // shows the frame else if (evt.getActionCommand().equals ("Quit")) { myframe.setVisible(false); // hides the frame justcolor.setBackground (backcolor); } else if (evt.getActionCommand().equals ("Change to Blue")) { justcolor.setBackground (Color.blue); } else if (evt.getActionCommand().equals ("Change to Cyan")) { justcolor.setBackground (Color.cyan); } } } class NewFrame extends Frame { Button closewindow = new Button("Quit"); Button bluebutton = new Button("Change to Blue"); Button cyanbutton = new Button("Change to Cyan"); // many Java programmers call the pointer to the calling class // the "outerparent" - here it is the "caller" NewFrame(ActionListener caller) { setTitle("Change Color"); setSize(300,300); setLayout(new GridLayout(3,1,2,2)); bluebutton.addActionListener(caller); add(bluebutton); cyanbutton.addActionListener(caller); add(cyanbutton); closewindow.addActionListener(caller); add(closewindow); // all components can generate WindowEvents addWindowListener (new CloseWindow()); } } // responds to an event generated by the close window button class CloseWindow extends WindowAdapter { public void windowClosing (WindowEvent e) { e.getWindow().setVisible (false); } }