How do i add a file menu to my java program?
Solution
Here goes the step by step guide to add a file menu to your program
*Your main method which initializes another method 'showMenuDemo' should look like this
public static void main(String[] args){
SwingMenuDemo swingMenuDemo = new SwingMenuDemo();
swingMenuDemo.showMenuDemo();
}
*The showMenuDemo will create a menubar with menu ‘file’ and one of the file item observed
from your code is 'exit' is also implemented.
private void showMenuDemo(){
//create a menu bar
menuBar = new JMenuBar();
//create menus
fileMenu = new JMenu("File");
//create menu items
exitItem = new JMenuItem("Exit");
exitItem.setActionCommand("Exit");
MenuItemListener menuItemListener = new MenuItemListener();
exitMenuItem.addActionListener(menuItemListener);
//add menu items to menus
fileMenu.add(exitMenuItem);
//add menu to menubar
menuBar.add(fileMenu);
//add menubar to the frame
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
}
*we will initialize the variables since all the variables required for implementing this
functionality are already declared in the class 'TheaterWindow '
*You can implement the action event on clicking the exit menu using the below function
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand()
+ " JMenuItem clicked.");
}
}

How do i add a file menu to my java programSolutionHere goes .pdf

  • 1.
    How do iadd a file menu to my java program? Solution Here goes the step by step guide to add a file menu to your program *Your main method which initializes another method 'showMenuDemo' should look like this public static void main(String[] args){ SwingMenuDemo swingMenuDemo = new SwingMenuDemo(); swingMenuDemo.showMenuDemo(); } *The showMenuDemo will create a menubar with menu ‘file’ and one of the file item observed from your code is 'exit' is also implemented. private void showMenuDemo(){ //create a menu bar menuBar = new JMenuBar(); //create menus fileMenu = new JMenu("File"); //create menu items exitItem = new JMenuItem("Exit"); exitItem.setActionCommand("Exit"); MenuItemListener menuItemListener = new MenuItemListener(); exitMenuItem.addActionListener(menuItemListener); //add menu items to menus fileMenu.add(exitMenuItem); //add menu to menubar menuBar.add(fileMenu); //add menubar to the frame mainFrame.setJMenuBar(menuBar); mainFrame.setVisible(true); } *we will initialize the variables since all the variables required for implementing this functionality are already declared in the class 'TheaterWindow ' *You can implement the action event on clicking the exit menu using the below function
  • 2.
    class MenuItemListener implementsActionListener { public void actionPerformed(ActionEvent e) { statusLabel.setText(e.getActionCommand() + " JMenuItem clicked."); } }