Menus Using Frames: Source Code
Menus Using Frames: Source Code
SOURCE CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class menu extends Frame implements ActionListener
{
String msg="";
int x=0,x1=0;
Menu sh=new Menu("Shapes");
Menu f=new Menu("Fill color");
Menu c=new Menu("Color");
Menu ex=new Menu("Exit");
public menu()
{
MenuBar mb=new MenuBar();
setMenuBar(mb);
sh.add(cr);
sh.add(s);
sh.add(l);
sh.add(r);
f.add(fc);
f.add(fs);
f.add(fr);
c.add(R);
c.add(b);
c.add(m);
c.add(p);
ex.add(e);
mb.add(sh);
mb.add(f);
mb.add(c);
mb.add(ex);
cr.addActionListener(this);
s.addActionListener(this);
l.addActionListener(this);
r.addActionListener(this);
fc.addActionListener(this);
fs.addActionListener(this);
fr.addActionListener(this);
R.addActionListener(this);
b.addActionListener(this);
m.addActionListener(this);
p.addActionListener(this);
e.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Circle"))
x=1;
if(e.getActionCommand().equals("Square"))
x=2;
if(e.getActionCommand().equals("line"))
x=3;
if(e.getActionCommand().equals("oval"))
x=4;
if(e.getActionCommand().equals("Rectangle"))
x=5;
if(e.getActionCommand().equals("fCircle"))
x=6;
if(e.getActionCommand().equals("fSquare"))
x=7;
if(e.getActionCommand().equals("foval"))
x=8;
if(e.getActionCommand().equals("fRectangle"))
x=9;
if(e.getActionCommand().equals("Red"))
x1=10;
if(e.getActionCommand().equals("Blue"))
x1=11;
if(e.getActionCommand().equals("green"))
x1=12;
if(e.getActionCommand().equals("Pink"))
x1=13;
if(e.getActionCommand().equals("Exit"))
{
System.exit(0);
}
repaint();
}
class WindowClose extends WindowAdapter
{
public void WindowClose(WindowEvent e)
{
System.exit(0);
}
}
public void paint(Graphics g)
{
if(x1==10)
g.setColor(Color.red);
if(x1==11)
g.setColor(Color.blue);
if(x1==12)
g.setColor(Color.green);
if(x1==13)
g.setColor(Color.pink);
if(x==1)
g.drawOval(50,100,120,120);
if(x==2)
g.drawRect(100,100,150,150);
if(x==3)
g.drawLine(30,100,200,100);
if(x==4)
g.drawOval(150,200,100,50);
if(x==5)
g.drawRect(200,300,100,60);
if(x==6)
g.fillOval(50,100,120,120);
if(x==7)
g.fillRect(100,100,150,150);
if(x==8)
g.fillOval(150,200,100,50);
if(x==9)
g.fillRect(200,300,100,60);
}
}
class menucolor
{
public static void main(String args[])
{
Frame f=new menu();
f.setSize(5000,5000);
f.setTitle("menu");
f.setVisible(true);
}
}
OUTPUT: