Menus & Menubars
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="MenuBarDemo" width=300 height=300></applet>*/
class MenuFrame extends Frame implements ActionListener, ItemListener, WindowListener
{
MenuBarDemo mbd;
MenuFrame(String title, MenuBarDemo mbd)
{
super(title);
this.mbd=mbd;
addWindowListener(this);
MenuBar mb=new MenuBar();
setMenuBar(mb);
Menu file=new Menu("File");
mb.add(file);
MenuItem open=new MenuItem("Open");
file.add(open);
MenuItem save=new MenuItem("Save");
file.add(save);
Menu edit=new Menu("Edit");
mb.add(edit);
MenuItem cut=new MenuItem("Cut");
edit.add(cut);
MenuItem copy=new MenuItem("Copy");
edit.add(copy);
MenuItem paste=new MenuItem("Paste");
edit.add(paste);
Menu option =new Menu("Option");
mb.add(option);
CheckboxMenuItem zoom=new CheckboxMenuItem("Zoom");
zoom.addItemListener(this);
option.add(zoom);
CheckboxMenuItem view=new CheckboxMenuItem("View");
view.addItemListener(this);
option.add(view);
}
public void actionPerformed(ActionEvent ae)
{
mbd.ta.append("ActionEvent:"+ae.getActionCommand()+"\n");
}
public void itemStateChanged(ItemEvent ie)
{
CheckboxMenuItem cmi=(CheckboxMenuItem)ie.getSource();
mbd.ta.append("ItemEvent:"+cmi.getLabel()+"\n");
}
public void windowActivated(WindowEvent we)
{
}
public void windowClosed(WindowEvent we)
{
}
public void windowClosing(WindowEvent we)
{
dispose();
}
public void windowDeactivated(WindowEvent we)
{
}
public void windowDeiconified(WindowEvent we)
{
}
public void windowIconified(WindowEvent we)
{
}
public void windowOpened(WindowEvent we)
{
}
}
public class MenuBarDemo extends Applet
{
TextArea ta;
public void init()
{
MenuFrame mf=new MenuFrame("My Frame",this);
mf.show();
mf.setSize(600,600);
ta=new TextArea(30,30);
add(ta);
}
}
Comments
Post a Comment