You are on page 1of 5

/**@author : Edward King Ed //Permisssion to use the content of this document // Is granted to anyone who will acknowledge me in writing

// Date: 5-SEP-2011 */

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*;

public class MyMenu extends JApplet{ //Create menu items private JMenuItem jmiNew, jmiOpen, jmiPrint, jmiExit; //Create buttons to be placed in a toolbar private JLabel jlblStatus = new JLabel(); //Create a JFileChooser with the current directory private JFileChooser jFileChooser1 = new JFileChooser(new File(".")); //Create a text area private JTextArea jta = new JTextArea();

public static void main(String[] args){ JFrame frame = new JFrame(); frame.setTitle("Simple Menu"); frame.setSize(500, 400); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);

}//end of main function

public MyMenu(){ //Create menu bar JMenuBar jmb = new JMenuBar(); setJMenuBar(jmb);

//Create menus JMenu fileMenu = new JMenu("File"); JMenu helpMenu = new JMenu("Help");

//Add menus to JMenuBar jmb.add(fileMenu); jmb.add(helpMenu);

//Create menu items and add them to the fileMenu fileMenu.add(jmiNew = new JMenuItem("New")); fileMenu.addSeparator(); fileMenu.add(jmiOpen = new JMenuItem("Open")); fileMenu.addSeparator(); fileMenu.add(jmiPrint = new JMenuItem("Print")); fileMenu.addSeparator(); fileMenu.add(jmiExit = new JMenuItem("Exit"));

//Create sub-menus items and add them to the menu

JMenu softwareHelpSubMenu = new JMenu("Software"); JMenu hardwareHelpSubMenu = new JMenu("Hardware"); helpMenu.add(softwareHelpSubMenu); helpMenu.add(hardwareHelpSubMenu); softwareHelpSubMenu.add(new JMenuItem("Unix")); softwareHelpSubMenu.add(new JMenuItem("Win 7")); softwareHelpSubMenu.add(new JMenuItem("Mac OS")); hardwareHelpSubMenu.add(new JMenuItem("Intel Corp.")); hardwareHelpSubMenu.add(new JMenuItem("Foxconn")); hardwareHelpSubMenu.add(new JMenuItem("Dell Corp."));

//Register listeners jmiNew.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("Process New"); } });//end of jmiNew actionListener jmiOpen.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ open(); } });//end of jmiOpen actionListener jmiPrint.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("Process Print");

} });//end of jmiNew actionListener jmiExit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } });//end of jmiNew actionListener }//end of constructor

//Open file private void open(){ if(jFileChooser1.showOpenDialog(this)== jFileChooser1.APPROVE_OPTION){ open(jFileChooser1.getSelectedFile()); }//end if }//end of open file function

//Open file with the specified File instance private void open(File file){ try{ //Read from specified file and store it in jta BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[in.available()]; in.read(b, 0, b.length); jta.append(new String(b, 0, b.length)); in.close();

//Display the status of the Open file operation in jlblStatus jlblStatus.setText(file.getName() + "Opened" ); } catch(IOException ex){ jlblStatus.setText("Error opening " + file.getName()); }//end try/catch statement }//end of open specified file function }//end of main class MyMenu

You might also like