/* The HighLowLauncher applet displays just one button. When the user clicks that button, a window belonging to the class HighLowFrame is opened. The name of the button changes from "Launch HighLow" to "Close HighLow". The name changes back when the window is closed. If the window is still open when the applet is destroyed, then the window will be closed automatically. This applet depends on the classes defined in the file HighLowFrame.java. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HighLowLauncher extends JApplet implements ActionListener { HighLowFrame highLow; // the HighLow window; // this is null if the window is not open JButton launchButton; // a button for opening the window public void init() { // initialize the applet launchButton = new JButton("Launch HighLow"); getContentPane().add(launchButton); launchButton.addActionListener(this); } // end init(); public void actionPerformed(ActionEvent evt) { // respond to a click on the button; if no window // is open, open one; if a window is open, close it // by calling its dispose() method. if (highLow == null) { Image cards = getImage(getCodeBase(),"smallcards.gif"); // The image for the cards must be provided as a // parameter to the constructor. This is done because // only an applet has a getImage() method. highLow = new HighLowFrame(cards); launchButton.setText("Close HighLow"); highLow.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent evt) { launchButton.setText("Launch HighLow"); launchButton.setEnabled(true); highLow = null; } }); } else { launchButton.setEnabled(false); // disable button while waiting // for window to close highLow.dispose(); } } public void destroy() { // when the applet is destroyed, check if there is // a window open; if so, close it. if (highLow != null) { highLow.dispose(); highLow = null; } } }