import java.awt.*;
import java.awt.event.*;
class MyMenu extends Frame implements ActionListener
{
private Menu file;
private MenuBar mb=new MenuBar();
private MenuItem New,Open,Save,SaveAs,Exit;
public MyMenu()
{
New=new MenuItem("New");
Open=new MenuItem("Open");
Save=new MenuItem("Save");
SaveAs=new MenuItem("SaveAs");
Exit=new MenuItem("Exit");
file=new Menu("File");
file.add(New);
file.add(Open);
file.add(Save);
file.add(SaveAs);
file.add(Exit);
mb.add(file);
setMenuBar(mb);
setLocation(100,100);
setSize(400,400);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
New.addActionListener(this);
Open.addActionListener(this);
Save.addActionListener(this);
SaveAs.addActionListener(this);
Exit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==New)
{
System.out.println("new selected");
}
else if(ae.getSource()==Open)
{
System.out.println("open selected");
}
else if(ae.getSource()==Save)
{
System.out.println("Save is selected");
}
else if(ae.getSource()==SaveAs)
{
System.out.println("SaveAs is selected");
}
else if(ae.getSource()==Exit)
{
System.out.println("Exit is selected");
System.exit(0);
}
}
}
class MenuDemo
{
public static void main(String args[])
{
new MyMenu().setVisible(true);
}
}
No comments:
Post a Comment