import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
public class SampleFrame6
{
public static void main(String args[])
{
MyFrame F=new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener
{
JPanel p=new JPanel();
JMenuBar a=new JMenuBar();
JMenu file=new JMenu("file");
JMenuItem i=new JMenuItem("yellow");
JMenuItem i1=new JMenuItem("blue");
JMenuItem i2=new JMenuItem("green");
JMenuItem i3=new JMenuItem("red");
MyFrame()
{
p.add(a);
a.add(file);
file.add(i);
file.add(i1);
file.add(i2);
file.add(i3);
i.addActionListener(this);
i1.addActionListener(this);
i2.addActionListener(this);
i3.addActionListener(this);
add(p);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==i)
{
p.setBackground(Color.yellow);
}
if(e.getSource()==i1)
{
p.setBackground(Color.blue);
}
if(e.getSource()==i2)
{
p.setBackground(Color.red);
}
if(e.getSource()==i3)
{
p.setBackground(Color.green);
}
}
}
output:
import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
public class SampleFrame6
{
public static void main(String args[])
{
MyFrame F=new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener
{
JPanel p=new JPanel();
JMenuBar a=new JMenuBar();
JMenu file=new JMenu("file");
JMenuItem i=new JMenuItem("yellow");
JMenuItem i1=new JMenuItem("blue");
JMenuItem i2=new JMenuItem("green");
JMenuItem i3=new JMenuItem("red");
MyFrame()
{
p.add(a);
a.add(file);
file.add(i);
file.add(i1);
file.add(i2);
file.add(i3);
i.addActionListener(this);
i1.addActionListener(this);
i2.addActionListener(this);
i3.addActionListener(this);
add(p);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==i)
{
p.setBackground(Color.yellow);
}
if(e.getSource()==i1)
{
p.setBackground(Color.blue);
}
if(e.getSource()==i2)
{
p.setBackground(Color.red);
}
if(e.getSource()==i3)
{
p.setBackground(Color.green);
}
}
}
output:
No comments:
Post a Comment