15 June, 2012

Creating Manu Bar in Java

Creating Manu Bar in Java


import javax.swing.*;
import java.awt.*;
import java.awt.Event;
public class MenuTest extends JFrame
{

public MenuTest()
{
Toolkit tk=Toolkit.getDefaultToolkit();

setTitle("Menu Bar & Menu Item");
setSize(500,350);
setLocation(200,100);
Image titleicon=tk.getImage("Notepad.png");
setIconImage(titleicon);
//setLayout(new FlowLayout());
JTextArea jta=new JTextArea();
add(jta);

JMenuBar mb =new JMenuBar();
setJMenuBar(mb);

//*****************************************
JMenu filemenu = new JMenu("File");
filemenu.setMnemonic('F');
mb.add(filemenu);

JMenuItem newmenu = new JMenuItem("New");
filemenu.add(newmenu);
//ImageIcon newmenuicon=new ImageIcon("Notepad.png");
//newmenu.setIcon(newmenuicon);
newmenu.setMnemonic('N');
//newmenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));

JMenuItem openmenu = new JMenuItem("Open");
filemenu.add(openmenu);
openmenu.setMnemonic('O');

JMenuItem exitmenu = new JMenuItem("Exit");
filemenu.add(exitmenu);
exitmenu.setMnemonic('X');

//*****************************************
JMenu editmenu = new JMenu("Edit");
editmenu.setMnemonic('E');
mb.add(editmenu);

JMenuItem cutmenu = new JMenuItem("Cut");
editmenu.add(cutmenu);

JMenuItem copymenu = new JMenuItem("Copy");
editmenu.add(copymenu);

JMenuItem pastemenu = new JMenuItem("Paste");
editmenu.add(pastemenu);

//*****************************************
JMenu helpmenu = new JMenu("Help");
helpmenu.setMnemonic('H');
mb.add(helpmenu);

JMenuItem aboutmenu = new JMenuItem("About");
helpmenu.add(aboutmenu);
aboutmenu.setMnemonic('A');

//*****************************************
}

public static void main(String[] args)
{
MenuTest f=new MenuTest();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

}



No comments:

Post a Comment

Thank You !