// creating menus

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

//menu bar and menu items

public class AMenu extends JFrame implements ActionListener
{
	private JButton button[];
	private JMenuBar menuBar;
	private JRadioButtonMenuItem colourItem[];
	private ButtonGroup colourGroup;
	private TextField text1,text2;
	private JMenuItem pasteItem, cutItem, copyItem,exitItem;
	private JMenuItem ft,fh,fc;
	private Panel panel;
	private Font roman,courier,helvetica,labelFont;
	private JLabel label;
	
	public AMenu(String title)
	{
		menuBar=new JMenuBar();
		setJMenuBar(menuBar);
		//file menu
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		menuBar.add(fileMenu);
		exitItem = new JMenuItem("Exit");
		fileMenu.add(exitItem);
		exitItem.addActionListener(this);
		
		// edit menu
		
		JMenu editMenu = new JMenu("Edit");
		menuBar.add(editMenu);
		
		// add items to the edit menu
		
		pasteItem = new JMenuItem("Paste");
		cutItem = new JMenuItem ("Cut");
		copyItem = new JMenuItem("Copy");
		
		// add action
		pasteItem.addActionListener(this);
		cutItem.addActionListener(this);
		copyItem.addActionListener(this);
		
		// add items to edit menu
		
		editMenu.add(cutItem);
		editMenu.add(copyItem);
		editMenu.add(pasteItem);
		
		//Font menu
		
		JMenu fontMenu = new JMenu("Font");
		menuBar.add(fontMenu);
		ft = new JMenuItem("Times Roman");
		fc = new JMenuItem("Couries");
		fh = new JMenuItem("Helvetica");
		
		JMenu colourMenu = new JMenu("Colour");
		
		// colours
		
		String colourName[] = {"Red","Blue","Green"};
		//create colourItem object
		colourItem= new JRadioButtonMenuItem[3];
		// create colourGroup object
		
		colourGroup = new ButtonGroup();
		
		for (int i=0;i<colourItem.length;i++)
		{
			colourItem[i] = new JRadioButtonMenuItem(colourName[i]);
			colourMenu.add(colourItem[i]);
			colourGroup.add(colourItem[i]);
			colourItem[i].addActionListener // add action to the colour menu
			(
				new ActionListener()// create listener handler object
				{
					public void actionPerformed(ActionEvent e)
					{
						for(int i=0;i<colourItem.length;i++)
						{
							if (e.getSource()==colourItem[i])	
							{
								if(colourItem[i].isSelected())
								{
									//compare menu text with the selection to set text colour
									if(colourItem[i].getText().equals("Red"))
									text1.setForeground(Color.red);
									if(colourItem[i].getText().equals("Blue"))
									text1.setForeground(Color.blue);
									if(colourItem[i].getText().equals("Green"))
									text1.setForeground(Color.green);
								}	
							else 
							text1.setForeground(Color.black);
							}
						}
					}
				}
			);
		}
		fontMenu.add(colourMenu);
		// add to font menu
		fontMenu.add(ft);
		fontMenu.add(fh);
		fontMenu.add(fc);
		
		//add action 
		ft.addActionListener(this);
		fh.addActionListener(this);
		fc.addActionListener(this);
		
		// set fonts
		roman = new Font("TimesRoman",Font.BOLD,14);
		courier = new Font("Courier",Font.ITALIC,14);
		helvetica = new Font("Helvetica",Font.PLAIN,16);
		
		// text area
		text1 = new TextField(25);
		text2 = new TextField(25);
		label = new JLabel("Menu Test");
		
		labelFont = new Font("Courier",Font.BOLD,72);
		label.setFont(labelFont);
		
		panel = new Panel();
		//add panel to frame
		getContentPane().add(label,BorderLayout.NORTH);
		getContentPane().add(panel,BorderLayout.CENTER);
		
		panel.add(text1); // add area to panel
		panel.add(text2);
		
		setSize(400,300);
		show();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		// checks if the button is pressed
		if(e.getSource()==exitItem)
		{
			System.exit(0);
		}
		
		if(e.getSource()==copyItem)
		{
			text1.selectAll();
			text2.selectAll();
		}
		
		if(e.getSource()==pasteItem)
		{
			if(text1.getSelectionStart()!=text1.getSelectionEnd())
			{
				text2.setText(text1.getSelectedText());
			}
			if(text2.getSelectionStart()!=text2.getSelectionEnd())
			{
				text1.setText(text2.getSelectedText());
			}
		}
		if(e.getSource()==cutItem)
		{
			text1.setText(null);
			text2.setText(null);
		}
		if(e.getSource()==ft)
		{
			text1.setFont(roman);
		}
		if(e.getSource()==fc)
		{
			text1.setFont(courier);
		}
		if (e.getSource()==fh)
		{
			text1.setFont(helvetica);
		}
	}
	//main
	
	public static void main(String args[])
	{
		AMenu cm = new AMenu("Menus");
		//close window
		cm.addWindowListener(new WindowAdapter()
		{
			//action listener to close the window event
			public void windoeClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
}															