//Loops.java //tests the prompt class //Adds menus to the frame //Andy Harris, 05/00 import java.awt.*; import java.applet.*; import java.awt.event.*; import Prompt; public class Loops extends Applet implements ActionListener{ Frame theFrame = new Frame("This is a frame"); Button btnShowDialog = new Button("Show dialog"); Button btnCloseFrame = new Button("Close frame"); Button btnForLoop = new Button("Count"); Button btnAskName = new Button("Ask name"); Button btnPassword = new Button("Password"); MenuBar mb = new MenuBar(); Menu mnuFile = new Menu("File"); MenuItem mniClose = new MenuItem("Close frame"); Menu mnuExamples = new Menu("Examples"); Menu mnuSay = new Menu ("Say"); Menu mnuAsk = new Menu ("Ask"); MenuItem mniShowDialog = new MenuItem("Show dialog"); MenuItem mniCount = new MenuItem("Count"); MenuItem mniAskName = new MenuItem("Ask name"); MenuItem mniPassword = new MenuItem("Password"); public void init(){ add(new Label("Loop , menu, and dialog")); add(new Label("Demonstration")); add(new Label("Look at the frame")); theFrame.setVisible(true); theFrame.setLayout(new FlowLayout()); theFrame.add(btnShowDialog); theFrame.add(btnCloseFrame); theFrame.add(btnForLoop); theFrame.add(btnAskName); theFrame.add(btnPassword); theFrame.setSize(200,200); //set up menus theFrame.setMenuBar(mb); mb.add(mnuFile); mnuFile.add(mniClose); mb.add(mnuExamples); mnuExamples.add(mnuSay); mnuSay.add(mniShowDialog); mnuSay.add(mniCount); mnuExamples.add(mnuAsk); mnuAsk.add(mniAskName); mnuAsk.add(mniPassword); mniClose.addActionListener(this); mniShowDialog.addActionListener(this); mniCount.addActionListener(this); mniAskName.addActionListener(this); mniPassword.addActionListener(this); btnShowDialog.addActionListener(this); btnCloseFrame.addActionListener(this); btnForLoop.addActionListener(this); btnAskName.addActionListener(this); btnPassword.addActionListener(this); } // end init public void actionPerformed(ActionEvent e){ Prompt p = new Prompt(theFrame); if(e.getActionCommand().equals("Show dialog")){ p.say ("Hello!"); p.say ("Goodbye"); } else if (e.getActionCommand().equals("Count")){ for (int i = 1; i <= 5; i++){ p.say("Now on Lap: " + String.valueOf(i)); } // end for loop } else if (e.getActionCommand().equals("Ask name")){ p.ask("What is your name?"); p.say("Hi, " + p.getResponse()); } else if (e.getActionCommand().equals("Password")){ p.ask("What's the password?"); while (!(p.getResponse().equals("java"))){ p.say("Hint: it is 'java'"); p.ask("What's the password?"); } // end while p.say("You got it!!"); } else { theFrame.setVisible(false); theFrame.dispose(); } // end 'which button' if } // end actionPerformed } // end class def