Creating a JMenubar in java swing

By | March 4, 2011

Creating a JMenubar is simple.

        JMenuBar  menubar = new JMenuBar();
	JMenu menu1 = new JMenu("File");
	JMenu menu2= new JMenu("Help");
	file = new JMenuItem("Exit to menu");
	edit = new JMenuItem("Exit");

	JMenuItem about = new JMenuItem("About us");
	f.setJMenuBar(menubar);
	menu1.add(file);
	menu1.add(edit);
	menu2.add(about);
	menubar.add(menu1);
	menubar.add(menu2);

	file.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action......
		}
	});
	edit.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action.....
		}
	});
	about.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action....
		}
	});

Leave a Reply

Your email address will not be published. Required fields are marked *