import java.awt.*; import java.applet.Applet; public class MenuAndButton extends java.applet.Applet { Frame f; TextArea myText; public MenuAndButton() { setLayout(new BorderLayout()); Panel myPanel = new Panel(); myPanel.setLayout(new FlowLayout()); myText = new TextArea("Textarea",5,20); myPanel.add(new Button("Show")); myPanel.add(new Button("Hide")); myPanel.add(new Button("Exit")); add("North",myPanel); add("Center",myText); } public void init() { f = new MyWindow("Sample Frame"); resize(preferredSize()); } public boolean handleEvent(Event e) { switch(e.id) { case Event.ACTION_EVENT: { if (e.arg == "Show") { f.show(); myText.setText("Showing Window" + "\n" + myText.getText()); return true; } else if (e.arg == "Hide") { f.hide(); myText.setText("Hiding Window" + "\n" + myText.getText()); return true; } else if (e.arg == "Exit") { System.exit(0); return true; } else return false; } default: return false; } } } class MyCanvas extends Canvas { public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,size().width, size().height); g.setColor(Color.black); g.draw3DRect(0,0,size().width-2, size().height-2,true); } } class MyWindow extends Frame { Canvas c; public MyWindow(String Title) { super("Sample Frame"); setLayout(new BorderLayout()); Panel myPanel = new Panel(); c = new MyCanvas(); c.resize(100,100); add("North",myPanel); add("Center",c); resize(preferredSize()); myPanel.add(new Button("Yahoo")); myPanel.add(new Button("InfoSeek")); myPanel.add(new Button("WebCrawler")); MenuBar mybar = new MenuBar(); Menu m = new Menu("Help"); m.add(new MenuItem("Menu Item 1")); m.add(new MenuItem("-")); m.add(new MenuItem("Menu Item 2")); m.add(new MenuItem("Menu Item 3")); m.add(new MenuItem("-")); m.add(new MenuItem("Exit")); mybar.add(m); setMenuBar(mybar); } public boolean handleEvent(Event menEvt) { switch(menEvt.id) { case Event.ACTION_EVENT: { if (menEvt.arg == "Menu Item 1") { System.out.println("Menu 1"); return true; } else if (menEvt.arg == "Menu Item 2") { System.out.println("Menu 2"); return true; } else if (menEvt.arg == "Menu Item 3") { System.out.println("Menu 3"); return true; } else if (menEvt.arg == "Exit") { this.hide(); return true; } else if (menEvt.arg == "Yahoo") { System.out.println("Yahoo"); return true; } else if (menEvt.arg == "InfoSeek") { System.out.println("InfoSeek"); return true; } else if (menEvt.arg == "WebCrawler") { System.out.println("WebCrawler"); return true; } else return false; } default: return false; } } }